From 3893b2a12e63c6947514635d368548cc2e9ff0f6 Mon Sep 17 00:00:00 2001 From: Zodiac Date: Thu, 13 Feb 2025 03:45:02 -0800 Subject: [PATCH] reset --- plugins/upload.py | 71 +++++++++++++++++------------------------------ 1 file changed, 26 insertions(+), 45 deletions(-) diff --git a/plugins/upload.py b/plugins/upload.py index fd5f2a1..9db29ff 100644 --- a/plugins/upload.py +++ b/plugins/upload.py @@ -3,7 +3,7 @@ IRC Bot Plugin for Uploading Files to hardfiles.org This plugin allows users to upload files to hardfiles.org using yt-dlp for downloads. -It supports downloading files from various sources (YouTube, Instagram, Reddit, etc.) and can +It supports downloading files from various sources (YouTube, Instagram, etc.) and can optionally convert videos to MP3 format before uploading. Files larger than 100MB are rejected. Usage: @@ -134,6 +134,7 @@ class UploadPlugin: "facebook.com", "dailymotion.com", ) + should_check_headers = not any(domain.endswith(d) for d in skip_check_domains) if should_check_headers: @@ -189,51 +190,34 @@ class UploadPlugin: 'preferredcodec': 'mp3', 'preferredquality': '192', } - ] if mp3 else [], + ] + if mp3 + else [], } with yt_dlp.YoutubeDL(ydl_opts) as ydl: try: - # Try to extract info without downloading info = await asyncio.to_thread(ydl.extract_info, url, download=False) except DownloadError as e: err_msg = self._ensure_str(e) - # If authentication is required (e.g. for Reddit), skip info extraction - if "Account authentication is required" in err_msg or "[Reddit]" in err_msg: - self.bot.privmsg( - target, - ircstyle.style( - f"Info extraction failed (auth error): {err_msg}. Skipping info extraction and proceeding with download.", - fg="yellow", - bold=True, - reset=True, - ), - ) - try: - info = await asyncio.to_thread(ydl.extract_info, url, download=True) - except Exception as e2: - err_msg2 = self._ensure_str(e2) - self.bot.privmsg( - target, - ircstyle.style( - f"Download failed: {err_msg2}", - fg="red", - bold=True, - reset=True, - ), - ) - return - else: - self.bot.privmsg( - target, - ircstyle.style( - f"Info extraction failed: {err_msg}", - fg="red", - bold=True, - reset=True, - ), - ) - return + self.bot.privmsg( + target, + ircstyle.style( + f"Info extraction failed: {err_msg}", fg="red", bold=True, reset=True + ), + ) + return + except UnicodeDecodeError: + self.bot.privmsg( + target, + ircstyle.style( + "Error: Received non-UTF-8 output during info extraction", + fg="red", + bold=True, + reset=True, + ), + ) + return estimated_size = info.get('filesize') or info.get('filesize_approx') if estimated_size and estimated_size > max_size: @@ -249,17 +233,13 @@ class UploadPlugin: return try: - # Proceed with the download (this may overwrite info if not already downloaded) info = await asyncio.to_thread(ydl.extract_info, url, download=True) except DownloadError as e: err_msg = self._ensure_str(e) self.bot.privmsg( target, ircstyle.style( - f"Download failed: {err_msg}", - fg="red", - bold=True, - reset=True, + f"Download failed: {err_msg}", fg="red", bold=True, reset=True ), ) return @@ -275,7 +255,7 @@ class UploadPlugin: ) return - # Safely convert metadata to strings + # Safely convert metadata to strings. metadata_parts = [] title = self._ensure_str(info.get("title")) uploader = self._ensure_str(info.get("uploader")) @@ -386,6 +366,7 @@ class UploadPlugin: ) return raw_response = await resp.read() + # Decode the response safely even if non-UTF-8 bytes are present. response_text = raw_response.decode('utf-8', errors='replace') upload_url = self.extract_url_from_response(response_text) or "Unknown URL" upload_url = self._ensure_str(upload_url)