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
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.
Usage:
@ -134,7 +134,6 @@ class UploadPlugin:
"facebook.com",
"dailymotion.com",
)
should_check_headers = not any(domain.endswith(d) for d in skip_check_domains)
if should_check_headers:
@ -190,28 +189,45 @@ 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: {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
except UnicodeDecodeError:
else:
self.bot.privmsg(
target,
ircstyle.style(
"Error: Received non-UTF-8 output during info extraction",
f"Info extraction failed: {err_msg}",
fg="red",
bold=True,
reset=True,
@ -233,13 +249,17 @@ 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
@ -255,7 +275,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"))
@ -366,7 +386,6 @@ 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)