diff options
Diffstat (limited to '')
| -rw-r--r-- | cogs/downloader.py | 58 |
1 files changed, 5 insertions, 53 deletions
diff --git a/cogs/downloader.py b/cogs/downloader.py index c35373f..5fb3d1c 100644 --- a/cogs/downloader.py +++ b/cogs/downloader.py | |||
| @@ -1,34 +1,19 @@ | |||
| 1 | import base64 | ||
| 2 | import os | 1 | import os |
| 3 | import os.path | ||
| 4 | import tempfile | 2 | import tempfile |
| 3 | from pathlib import Path | ||
| 5 | 4 | ||
| 6 | import discord | 5 | import discord |
| 7 | import requests | ||
| 8 | import yt_dlp | 6 | import yt_dlp |
| 9 | from discord.ext import commands | 7 | from discord.ext import commands |
| 10 | from dotenv import load_dotenv | 8 | from dotenv import load_dotenv |
| 11 | 9 | ||
| 12 | # Load environment variables | 10 | # Load environment variables |
| 13 | load_dotenv() | 11 | load_dotenv() |
| 14 | PIXELDRAIN_API_KEY = os.getenv('PIXELDRAIN_API_KEY') | ||
| 15 | |||
| 16 | |||
| 17 | def _get_auth_headers(): | ||
| 18 | """Generate authentication headers for Pixeldrain API""" | ||
| 19 | if not PIXELDRAIN_API_KEY: | ||
| 20 | raise Exception("Pixeldrain API key not found in environment variables") | ||
| 21 | |||
| 22 | auth_string = f":{PIXELDRAIN_API_KEY}" | ||
| 23 | auth_bytes = auth_string.encode('ascii') | ||
| 24 | base64_auth = base64.b64encode(auth_bytes).decode('ascii') | ||
| 25 | return {"Authorization": f"Basic {base64_auth}"} | ||
| 26 | 12 | ||
| 27 | 13 | ||
| 28 | class Downloader(commands.Cog): | 14 | class Downloader(commands.Cog): |
| 29 | def __init__(self, bot): | 15 | def __init__(self, bot): |
| 30 | self.bot = bot | 16 | self.bot = bot |
| 31 | self.pixeldrain_url = "https://pixeldrain.com/api/file" | ||
| 32 | 17 | ||
| 33 | @commands.slash_command( | 18 | @commands.slash_command( |
| 34 | name="download", | 19 | name="download", |
| @@ -54,47 +39,14 @@ class Downloader(commands.Cog): | |||
| 54 | info = ydl.extract_info(url, download=True) | 39 | info = ydl.extract_info(url, download=True) |
| 55 | actual_filename = ydl.prepare_filename(info) | 40 | actual_filename = ydl.prepare_filename(info) |
| 56 | 41 | ||
| 57 | await ctx.followup.send("Video downloaded successfully, Uploading to pixeldrain...") | ||
| 58 | # upload to pixeldrain | ||
| 59 | try: | 42 | try: |
| 60 | with open(actual_filename, 'rb') as f: | 43 | file = discord.File(actual_filename) |
| 61 | files = {'file': f} | 44 | await ctx.followup.send("Pobran", file=file) |
| 62 | response = requests.post( | ||
| 63 | self.pixeldrain_url, | ||
| 64 | files=files, | ||
| 65 | headers={ | ||
| 66 | "Authorization": f"Basic {base64.b64encode((':' + PIXELDRAIN_API_KEY).encode()).decode()}"} | ||
| 67 | ) | ||
| 68 | |||
| 69 | try: | ||
| 70 | response_data = response.json() | ||
| 71 | print("Pixeldrain response:", response_data) # Debug log | ||
| 72 | |||
| 73 | if response.status_code == 201: # 201 means successful upload | ||
| 74 | file_id = response_data.get('id') | ||
| 75 | if file_id: | ||
| 76 | file_link = f"https://pixeldrain.com/u/{file_id}" | ||
| 77 | await ctx.followup.send(f"oto twoje videło\n {file_link}") | ||
| 78 | else: | ||
| 79 | await ctx.followup.send( | ||
| 80 | f"⚠️ Upload successful but no file ID returned. Response: {response_data}") | ||
| 81 | else: | ||
| 82 | error_msg = response_data.get('error', 'Unknown error') | ||
| 83 | if response.status_code == 401: | ||
| 84 | error_msg = "Invalid API key. Please check your PIXELDRAIN_API_KEY in .env file." | ||
| 85 | await ctx.followup.send(f"❌ Upload failed (Status {response.status_code}): {error_msg}") | ||
| 86 | except ValueError as e: | ||
| 87 | # If the response is not JSON | ||
| 88 | error_msg = f"Failed to parse server response: {str(e)}" | ||
| 89 | if response.text: | ||
| 90 | error_msg += f"\nResponse: {response.text[:200]}" | ||
| 91 | await ctx.followup.send(f"❌ {error_msg}") | ||
| 92 | except Exception as e: | 45 | except Exception as e: |
| 93 | await ctx.followup.send(f"Error: {str(e)}") | 46 | await ctx.followup.send(f":(\n{str(e)}") |
| 94 | # No need for explicit cleanup as a tempfile.TemporaryDirectory handles it automatically | ||
| 95 | 47 | ||
| 96 | except Exception as e: | 48 | except Exception as e: |
| 97 | await ctx.followup.send(f"Error: {str(e)}") | 49 | await ctx.followup.send(f":(\n{str(e)}") |
| 98 | 50 | ||
| 99 | 51 | ||
| 100 | def setup(bot): | 52 | def setup(bot): |
