aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/downloader.py
diff options
context:
space:
mode:
Diffstat (limited to 'cogs/downloader.py')
-rw-r--r--cogs/downloader.py32
1 files changed, 18 insertions, 14 deletions
diff --git a/cogs/downloader.py b/cogs/downloader.py
index 7f48e57..c35373f 100644
--- a/cogs/downloader.py
+++ b/cogs/downloader.py
@@ -1,12 +1,13 @@
1import base64 1import base64
2import os 2import os
3import os.path
4import tempfile
5
3import discord 6import discord
4from discord.ext import commands
5import requests 7import requests
6import yt_dlp 8import yt_dlp
9from discord.ext import commands
7from dotenv import load_dotenv 10from dotenv import load_dotenv
8import tempfile
9import os.path
10 11
11# Load environment variables 12# Load environment variables
12load_dotenv() 13load_dotenv()
@@ -34,9 +35,9 @@ class Downloader(commands.Cog):
34 description="download video from internet", 35 description="download video from internet",
35 ) 36 )
36 async def download( 37 async def download(
37 self, 38 self,
38 ctx: discord.ApplicationContext, 39 ctx: discord.ApplicationContext,
39 url: discord.Option(str, "url", required=True) 40 url: discord.Option(str, "url", required=True)
40 ): 41 ):
41 await ctx.defer() 42 await ctx.defer()
42 try: 43 try:
@@ -52,7 +53,7 @@ class Downloader(commands.Cog):
52 with yt_dlp.YoutubeDL(ydl_opts) as ydl: 53 with yt_dlp.YoutubeDL(ydl_opts) as ydl:
53 info = ydl.extract_info(url, download=True) 54 info = ydl.extract_info(url, download=True)
54 actual_filename = ydl.prepare_filename(info) 55 actual_filename = ydl.prepare_filename(info)
55 56
56 await ctx.followup.send("Video downloaded successfully, Uploading to pixeldrain...") 57 await ctx.followup.send("Video downloaded successfully, Uploading to pixeldrain...")
57 # upload to pixeldrain 58 # upload to pixeldrain
58 try: 59 try:
@@ -61,37 +62,40 @@ class Downloader(commands.Cog):
61 response = requests.post( 62 response = requests.post(
62 self.pixeldrain_url, 63 self.pixeldrain_url,
63 files=files, 64 files=files,
64 headers={"Authorization": f"Basic {base64.b64encode((':' + PIXELDRAIN_API_KEY).encode()).decode()}"} 65 headers={
66 "Authorization": f"Basic {base64.b64encode((':' + PIXELDRAIN_API_KEY).encode()).decode()}"}
65 ) 67 )
66 68
67 try: 69 try:
68 response_data = response.json() 70 response_data = response.json()
69 print("Pixeldrain response:", response_data) # Debug log 71 print("Pixeldrain response:", response_data) # Debug log
70 72
71 if response.status_code == 201: # 201 means successful upload 73 if response.status_code == 201: # 201 means successful upload
72 file_id = response_data.get('id') 74 file_id = response_data.get('id')
73 if file_id: 75 if file_id:
74 file_link = f"https://pixeldrain.com/u/{file_id}" 76 file_link = f"https://pixeldrain.com/u/{file_id}"
75 await ctx.followup.send(f"oto twoje videło\n {file_link}") 77 await ctx.followup.send(f"oto twoje videło\n {file_link}")
76 else: 78 else:
77 await ctx.followup.send(f"⚠️ Upload successful but no file ID returned. Response: {response_data}") 79 await ctx.followup.send(
80 f"⚠️ Upload successful but no file ID returned. Response: {response_data}")
78 else: 81 else:
79 error_msg = response_data.get('error', 'Unknown error') 82 error_msg = response_data.get('error', 'Unknown error')
80 if response.status_code == 401: 83 if response.status_code == 401:
81 error_msg = "Invalid API key. Please check your PIXELDRAIN_API_KEY in .env file." 84 error_msg = "Invalid API key. Please check your PIXELDRAIN_API_KEY in .env file."
82 await ctx.followup.send(f"❌ Upload failed (Status {response.status_code}): {error_msg}") 85 await ctx.followup.send(f"❌ Upload failed (Status {response.status_code}): {error_msg}")
83 except ValueError as e: 86 except ValueError as e:
84 # If response is not JSON 87 # If the response is not JSON
85 error_msg = f"Failed to parse server response: {str(e)}" 88 error_msg = f"Failed to parse server response: {str(e)}"
86 if response.text: 89 if response.text:
87 error_msg += f"\nResponse: {response.text[:200]}" 90 error_msg += f"\nResponse: {response.text[:200]}"
88 await ctx.followup.send(f"❌ {error_msg}") 91 await ctx.followup.send(f"❌ {error_msg}")
89 except Exception as e: 92 except Exception as e:
90 await ctx.followup.send(f"Error: {str(e)}") 93 await ctx.followup.send(f"Error: {str(e)}")
91 # No need for explicit cleanup as tempfile.TemporaryDirectory handles it automatically 94 # No need for explicit cleanup as a tempfile.TemporaryDirectory handles it automatically
92 95
93 except Exception as e: 96 except Exception as e:
94 await ctx.followup.send(f"Error: {str(e)}") 97 await ctx.followup.send(f"Error: {str(e)}")
95 98
99
96def setup(bot): 100def setup(bot):
97 bot.add_cog(Downloader(bot)) \ No newline at end of file 101 bot.add_cog(Downloader(bot))