aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/downloader.py
diff options
context:
space:
mode:
Diffstat (limited to 'cogs/downloader.py')
-rw-r--r--cogs/downloader.py82
1 files changed, 66 insertions, 16 deletions
diff --git a/cogs/downloader.py b/cogs/downloader.py
index 7925f2d..42acc1d 100644
--- a/cogs/downloader.py
+++ b/cogs/downloader.py
@@ -5,6 +5,7 @@ import requests
5import discord 5import discord
6import re 6import re
7import yt_dlp 7import yt_dlp
8import json
8from discord.ext import commands 9from discord.ext import commands
9from dotenv import load_dotenv 10from dotenv import load_dotenv
10 11
@@ -49,6 +50,23 @@ class Downloader(commands.Cog):
49 ): 50 ):
50 """Download and convert media from a URL to the specified format""" 51 """Download and convert media from a URL to the specified format"""
51 await ctx.defer() 52 await ctx.defer()
53 # Call the function and get the response
54 response = await self.download_standalone(url=url, catbox=catbox, fformat=fformat, filename_user=filename_user)
55 if isinstance(response, dict):
56 if response["status"] == "success":
57 if response["type"] == "url":
58 await ctx.respond(f"pobioron i wysyłon: {response['content']}")
59 elif response["type"] == "file":
60 file_data = response["content"]
61 await ctx.respond(file=discord.File(file_data))
62 else:
63 logger.error(f"Error: {response['content']}")
64 if 'details' in response:
65 logger.error(f"Details: {response['details']}")
66
67
68 @staticmethod
69 async def download_standalone(url: str, catbox: bool = False, fformat: str = "mp4", filename_user: str = None):
52 try: 70 try:
53 logger.info(f"Downloading {url} as {fformat}") 71 logger.info(f"Downloading {url} as {fformat}")
54 72
@@ -93,6 +111,7 @@ class Downloader(commands.Cog):
93 filename = os.path.basename(actual_file) 111 filename = os.path.basename(actual_file)
94 else: 112 else:
95 filename = filename_user + '.' + extension 113 filename = filename_user + '.' + extension
114 actual_file = os.path.join(temp_dir, filename)
96 115
97 if not os.path.exists(actual_file): 116 if not os.path.exists(actual_file):
98 raise Exception(f"Downloaded file not found: {actual_file}") 117 raise Exception(f"Downloaded file not found: {actual_file}")
@@ -128,32 +147,63 @@ class Downloader(commands.Cog):
128 result = response.json() 147 result = response.json()
129 if 'fileurl' in result: 148 if 'fileurl' in result:
130 dl_url = result['fileurl'] 149 dl_url = result['fileurl']
131 await ctx.followup.send( 150 return {
132 f"wysyłon\n" 151 "status": "success",
133 f"{dl_url}\n" 152 "type": "url",
134 ) 153 "content": dl_url,
154 "format": fformat,
155 "filename": filename
156 }
135 else: 157 else:
136 await ctx.followup.send( 158 return {
137 f"Odpowiedź serwera nie zawiera wymaganych danych: {result}") 159 "status": "error",
160 "type": "error",
161 "content": "Invalid response from file server",
162 "details": str(result)
163 }
138 except ValueError: 164 except ValueError:
139 await ctx.followup.send(f"Nieprawidłowa odpowiedź z serwera: {response.text}") 165 return {
166 "status": "error",
167 "type": "error",
168 "content": "Failed to parse server response",
169 "details": response.text[:200] # First 200 chars of response
170 }
140 else: 171 else:
141 error_msg = response.text if response.text else 'Unknown error' 172 error_msg = response.text if response.text else 'Unknown error'
142 await ctx.followup.send( 173 return {
143 f"nie udało się wysłać pliku (HTTP {response.status_code}): {error_msg}") 174 "status": "error",
175 "type": "error",
176 "content": f"Server returned {response.status_code}",
177 "details": error_msg
178 }
144 179
145 except Exception as upload_error: 180 except Exception as upload_error:
146 await ctx.followup.send(f"copyparty mnie nienawidzi :cccc\n {str(upload_error)}") 181 return {
147 logger.error(f"copyparty upload error: {upload_error}", exc_info=True) 182 "status": "error",
183 "type": "error",
184 "content": "Upload failed",
185 "details": str(upload_error)
186 }
148 187
149 else: 188 else:
150 await ctx.followup.send("wysyłon na discord\n", file=discord.File(filename)) 189 # If we have a file to return directly
151 190 return {
152 logger.info(f"File processed: {actual_file}") 191 "status": "success",
192 "type": "file",
193 "content": actual_file,
194 "filename": filename
195 }
153 196
154 except Exception as e: 197 except Exception as e:
155 await ctx.followup.send(f":(\n{str(e)}") 198 error_msg = f"Error in download command: {e}"
156 logger.error(f"Error in download command: {e}", exc_info=True) 199 logger.error(error_msg, exc_info=True)
200 return {
201 "status": "error",
202 "type": "error",
203 "content": "Download failed",
204 "details": str(e)
205 }
206
157 207
158 208
159def setup(bot): 209def setup(bot):