This commit is contained in:
Zodiac 2025-02-13 03:43:09 -08:00
parent 1f706878c2
commit c292fb2d1c

View File

@ -3,7 +3,7 @@
IRC Bot Plugin for Uploading Files to hardfiles.org IRC Bot Plugin for Uploading Files to hardfiles.org
This plugin allows users to upload files to hardfiles.org using yt-dlp for downloads. This plugin allows users to upload files to hardfiles.org using yt-dlp for downloads.
It supports downloading files from various sources (YouTube, Instagram, etc.) and can It supports downloading files from various sources (YouTube, Instagram, Reddit, etc.) and can
optionally convert videos to MP3 format before uploading. Files larger than 100MB are rejected. optionally convert videos to MP3 format before uploading. Files larger than 100MB are rejected.
Usage: Usage:
@ -134,7 +134,6 @@ class UploadPlugin:
"facebook.com", "facebook.com",
"dailymotion.com", "dailymotion.com",
) )
should_check_headers = not any(domain.endswith(d) for d in skip_check_domains) should_check_headers = not any(domain.endswith(d) for d in skip_check_domains)
if should_check_headers: if should_check_headers:
@ -190,28 +189,45 @@ class UploadPlugin:
'preferredcodec': 'mp3', 'preferredcodec': 'mp3',
'preferredquality': '192', 'preferredquality': '192',
} }
] ] if mp3 else [],
if mp3
else [],
} }
with yt_dlp.YoutubeDL(ydl_opts) as ydl: with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try: try:
# Try to extract info without downloading
info = await asyncio.to_thread(ydl.extract_info, url, download=False) info = await asyncio.to_thread(ydl.extract_info, url, download=False)
except DownloadError as e: except DownloadError as e:
err_msg = self._ensure_str(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( self.bot.privmsg(
target, target,
ircstyle.style( ircstyle.style(
f"Info extraction failed: {err_msg}", fg="red", bold=True, reset=True 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 return
except UnicodeDecodeError: else:
self.bot.privmsg( self.bot.privmsg(
target, target,
ircstyle.style( ircstyle.style(
"Error: Received non-UTF-8 output during info extraction", f"Info extraction failed: {err_msg}",
fg="red", fg="red",
bold=True, bold=True,
reset=True, reset=True,
@ -233,13 +249,17 @@ class UploadPlugin:
return return
try: try:
# Proceed with the download (this may overwrite info if not already downloaded)
info = await asyncio.to_thread(ydl.extract_info, url, download=True) info = await asyncio.to_thread(ydl.extract_info, url, download=True)
except DownloadError as e: except DownloadError as e:
err_msg = self._ensure_str(e) err_msg = self._ensure_str(e)
self.bot.privmsg( self.bot.privmsg(
target, target,
ircstyle.style( 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 return
@ -255,7 +275,7 @@ class UploadPlugin:
) )
return return
# Safely convert metadata to strings. # Safely convert metadata to strings
metadata_parts = [] metadata_parts = []
title = self._ensure_str(info.get("title")) title = self._ensure_str(info.get("title"))
uploader = self._ensure_str(info.get("uploader")) uploader = self._ensure_str(info.get("uploader"))
@ -366,7 +386,6 @@ class UploadPlugin:
) )
return return
raw_response = await resp.read() 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') response_text = raw_response.decode('utf-8', errors='replace')
upload_url = self.extract_url_from_response(response_text) or "Unknown URL" upload_url = self.extract_url_from_response(response_text) or "Unknown URL"
upload_url = self._ensure_str(upload_url) upload_url = self._ensure_str(upload_url)