From c53afdeb167ec647a2ef840bcfc1b6127591707d Mon Sep 17 00:00:00 2001 From: salmonik Date: Wed, 8 Oct 2025 18:34:08 +0200 Subject: send to discord if size is sufficient for downloader.py --- cogs/downloader.py | 121 ++++++++++++++++++++++++++++------------------------- 1 file changed, 64 insertions(+), 57 deletions(-) (limited to 'cogs/downloader.py') diff --git a/cogs/downloader.py b/cogs/downloader.py index 251a9af..535615d 100644 --- a/cogs/downloader.py +++ b/cogs/downloader.py @@ -43,24 +43,25 @@ class Downloader(commands.Cog): self, ctx: discord.ApplicationContext, url: discord.Option(str, "URL of the video to download", required=True), - fformat: discord.Option(str, "Output format (mp4, mp3, wav, etc.)", - required=False, - default="mp4") + fformat: discord.Option(str, "Output format (mp4, mp3, wav, etc.)", + required=False, + default="mp4") ): """Download and convert media from a URL to the specified format""" await ctx.defer() try: logger.info(f"Downloading {url} as {fformat}") - + with tempfile.TemporaryDirectory() as temp_dir: # Configure yt-dlp to use the original filename ydl_opts = { 'outtmpl': os.path.join(temp_dir, '%(title)s.%(ext)s'), - 'format': 'bestaudio/best' if fformat in ['mp3', 'wav', 'm4a'] else 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', + 'format': 'bestaudio/best' if fformat in ['mp3', 'wav', + 'm4a'] else 'bestvideo[ext=mp4]+bestaudio[ext=m4a]/best[ext=mp4]/best', 'merge_output_format': 'mp4', 'postprocessors': [], } - + # Add post-processing for audio formats if fformat in ['mp3', 'wav', 'm4a']: ydl_opts['postprocessors'].append({ @@ -76,66 +77,72 @@ class Downloader(commands.Cog): with yt_dlp.YoutubeDL(ydl_opts) as ydl: ydl.download([url]) - + # Find the downloaded file (should be the only file in the temp directory) files = [f for f in os.listdir(temp_dir) if os.path.isfile(os.path.join(temp_dir, f))] if not files: raise Exception(f"No file was downloaded") - + actual_file = os.path.join(temp_dir, files[0]) if not os.path.exists(actual_file): raise Exception(f"Downloaded file not found: {actual_file}") - try: - # Get file info - filename = f"output.{fformat}" - - # Upload to Copyparty - with open(actual_file, 'rb') as f: - files = { - 'f': (filename, f, 'application/octet-stream') - } - headers = { - 'Cookie': f'cppwd={os.getenv("COPYPARTY_PASSWORD")}' - } - params = { - 'j': '1', # Return JSON response - 'ck': 'b2' # Use blake2b checksum - } - - # Ensure the URL ends with a single slash - base_url = "https://catbox.2137697.xyz/sperma/" - - response = requests.post( - base_url, - files=files, - headers=headers, - params=params - ) - - if response.status_code == 200 or response.status_code == 201: - try: - result = response.json() - if 'fileurl' in result: - dl_url = result['fileurl'] - await ctx.followup.send( - f"wysyłon\n" - f"{dl_url}\n" - ) - else: - await ctx.followup.send(f"Odpowiedź serwera nie zawiera wymaganych danych: {result}") - except ValueError: - await ctx.followup.send(f"Nieprawidłowa odpowiedź z serwera: {response.text}") - else: - error_msg = response.text if response.text else 'Unknown error' - await ctx.followup.send(f"nie udało się wysłać pliku (HTTP {response.status_code}): {error_msg}") - - except Exception as upload_error: - await ctx.followup.send(f"copyparty mnie nienawidzi :cccc\n {str(upload_error)}") - logger.error(f"copyparty upload error: {upload_error}", exc_info=True) - + if os.path.getsize(actual_file) > 1024*1024*10: + try: + # Get file info + filename = f"output.{fformat}" + + # Upload to Copyparty + with open(actual_file, 'rb') as f: + files = { + 'f': (filename, f, 'application/octet-stream') + } + headers = { + 'Cookie': f'cppwd={os.getenv("COPYPARTY_PASSWORD")}' + } + params = { + 'j': '1', # Return JSON response + 'ck': 'b2' # Use blake2b checksum + } + + # Ensure the URL ends with a single slash + base_url = "https://catbox.2137697.xyz/sperma/" + + response = requests.post( + base_url, + files=files, + headers=headers, + params=params + ) + + if response.status_code == 200 or response.status_code == 201: + try: + result = response.json() + if 'fileurl' in result: + dl_url = result['fileurl'] + await ctx.followup.send( + f"wysyłon\n" + f"{dl_url}\n" + ) + else: + await ctx.followup.send( + f"Odpowiedź serwera nie zawiera wymaganych danych: {result}") + except ValueError: + await ctx.followup.send(f"Nieprawidłowa odpowiedź z serwera: {response.text}") + else: + error_msg = response.text if response.text else 'Unknown error' + await ctx.followup.send( + f"nie udało się wysłać pliku (HTTP {response.status_code}): {error_msg}") + + except Exception as upload_error: + await ctx.followup.send(f"copyparty mnie nienawidzi :cccc\n {str(upload_error)}") + logger.error(f"copyparty upload error: {upload_error}", exc_info=True) + + else: + await ctx.followup.send("wysyłon na discord\n", file=discord.File(actual_file)) + logger.info(f"File processed: {actual_file}") - + except Exception as e: await ctx.followup.send(f":(\n{str(e)}") logger.error(f"Error in download command: {e}", exc_info=True) -- cgit v1.2.3