diff options
Diffstat (limited to '')
| -rw-r--r-- | cogs/downloader.py | 90 | ||||
| -rw-r--r-- | main.py | 1 |
2 files changed, 91 insertions, 0 deletions
diff --git a/cogs/downloader.py b/cogs/downloader.py new file mode 100644 index 0000000..b992e82 --- /dev/null +++ b/cogs/downloader.py | |||
| @@ -0,0 +1,90 @@ | |||
| 1 | import base64 | ||
| 2 | import os | ||
| 3 | import discord | ||
| 4 | from discord.ext import commands | ||
| 5 | import requests | ||
| 6 | import yt_dlp | ||
| 7 | from dotenv import load_dotenv | ||
| 8 | |||
| 9 | # Load environment variables | ||
| 10 | load_dotenv() | ||
| 11 | PIXELDRAIN_API_KEY = os.getenv('PIXELDRAIN_API_KEY') | ||
| 12 | |||
| 13 | class Downloader(commands.Cog): | ||
| 14 | def __init__(self, bot): | ||
| 15 | self.bot = bot | ||
| 16 | self.pixeldrain_url = "https://pixeldrain.com/api/file" | ||
| 17 | |||
| 18 | def _get_auth_headers(self): | ||
| 19 | """Generate authentication headers for Pixeldrain API""" | ||
| 20 | if not PIXELDRAIN_API_KEY: | ||
| 21 | raise Exception("Pixeldrain API key not found in environment variables") | ||
| 22 | |||
| 23 | auth_string = f":{PIXELDRAIN_API_KEY}" | ||
| 24 | auth_bytes = auth_string.encode('ascii') | ||
| 25 | base64_auth = base64.b64encode(auth_bytes).decode('ascii') | ||
| 26 | return {"Authorization": f"Basic {base64_auth}"} | ||
| 27 | |||
| 28 | @commands.slash_command( | ||
| 29 | name="download", | ||
| 30 | description="download video from internet", | ||
| 31 | ) | ||
| 32 | async def download( | ||
| 33 | self, | ||
| 34 | ctx: discord.ApplicationContext, | ||
| 35 | url: discord.Option(str, "url", required=True) | ||
| 36 | ): | ||
| 37 | await ctx.defer() | ||
| 38 | try: | ||
| 39 | # pass link to yt-dlp | ||
| 40 | outtmpl = '%(title)s.%(ext)s' | ||
| 41 | ydl_opts = { | ||
| 42 | 'outtmpl': outtmpl, | ||
| 43 | 'merge_output_format': 'mp4', | ||
| 44 | } | ||
| 45 | with yt_dlp.YoutubeDL(ydl_opts) as ydl: | ||
| 46 | info = ydl.extract_info(url, download=True) | ||
| 47 | actual_filename = ydl.prepare_filename(info) | ||
| 48 | |||
| 49 | await ctx.followup.send("Video downloaded successfully, Uploading to pixeldrain...") | ||
| 50 | # upload to pixeldrain | ||
| 51 | try: | ||
| 52 | with open(actual_filename, 'rb') as f: | ||
| 53 | files = {'file': f} | ||
| 54 | response = requests.post( | ||
| 55 | self.pixeldrain_url, | ||
| 56 | files=files, | ||
| 57 | headers={"Authorization": f"Basic {base64.b64encode((':' + PIXELDRAIN_API_KEY).encode()).decode()}"} | ||
| 58 | ) | ||
| 59 | |||
| 60 | try: | ||
| 61 | response_data = response.json() | ||
| 62 | print("Pixeldrain response:", response_data) # Debug log | ||
| 63 | |||
| 64 | if response.status_code == 201: # 201 means successful upload | ||
| 65 | file_id = response_data.get('id') | ||
| 66 | if file_id: | ||
| 67 | file_link = f"https://pixeldrain.com/u/{file_id}" | ||
| 68 | await ctx.followup.send(f"oto twoje videło\n {file_link}") | ||
| 69 | else: | ||
| 70 | await ctx.followup.send(f"⚠️ Upload successful but no file ID returned. Response: {response_data}") | ||
| 71 | else: | ||
| 72 | error_msg = response_data.get('error', 'Unknown error') | ||
| 73 | if response.status_code == 401: | ||
| 74 | error_msg = "Invalid API key. Please check your PIXELDRAIN_API_KEY in .env file." | ||
| 75 | await ctx.followup.send(f"❌ Upload failed (Status {response.status_code}): {error_msg}") | ||
| 76 | except ValueError as e: | ||
| 77 | # If response is not JSON | ||
| 78 | error_msg = f"Failed to parse server response: {str(e)}" | ||
| 79 | if response.text: | ||
| 80 | error_msg += f"\nResponse: {response.text[:200]}" | ||
| 81 | await ctx.followup.send(f"❌ {error_msg}") | ||
| 82 | finally: | ||
| 83 | # Clean up the downloaded file | ||
| 84 | if os.path.exists(actual_filename): | ||
| 85 | os.remove(actual_filename) | ||
| 86 | except Exception as e: | ||
| 87 | await ctx.followup.send(f"Error: {str(e)}") | ||
| 88 | |||
| 89 | def setup(bot): | ||
| 90 | bot.add_cog(Downloader(bot)) \ No newline at end of file | ||
| @@ -21,6 +21,7 @@ if __name__ == "__main__": | |||
| 21 | 'cogs.tts', | 21 | 'cogs.tts', |
| 22 | 'cogs.web', | 22 | 'cogs.web', |
| 23 | 'cogs.ai', | 23 | 'cogs.ai', |
| 24 | 'cogs.downloader', | ||
| 24 | 'cogs.music.play', | 25 | 'cogs.music.play', |
| 25 | 'cogs.music.play_compressed', | 26 | 'cogs.music.play_compressed', |
| 26 | 'cogs.music.skip', | 27 | 'cogs.music.skip', |
