Update upload to combine with youtube

This commit is contained in:
Zodiac 2025-02-13 19:24:52 -08:00
parent c91590177c
commit ea4d5365db

View File

@ -7,7 +7,8 @@ It supports downloading files from various sources (YouTube, Instagram, etc.) an
optionally convert videos to MP3 format before uploading. Files larger than 100MB are rejected.
Usage:
!upload [--mp3] <url>
!upload [--mp3] <url_or_search_term>...
Example: !upload never gonna give you up
Dependencies:
- aiohttp
@ -33,6 +34,7 @@ import ircstyle
import yt_dlp
from yt_dlp.utils import DownloadError
from urllib.parse import urlparse
import googleapiclient.discovery
# Global headers to mimic a real browser (ban evasion)
HEADERS = {
@ -45,6 +47,16 @@ HEADERS = {
"Upgrade-Insecure-Requests": "1"
}
# Constants for YouTube API
API_SERVICE_NAME = "youtube"
API_VERSION = "v3"
DEVELOPER_KEY = "AIzaSyBNrqOA0ZIziUVLYm0K5W76n9ndqz6zTxI"
# Initialize YouTube API client
youtube = googleapiclient.discovery.build(
API_SERVICE_NAME, API_VERSION, developerKey=DEVELOPER_KEY
)
@irc3.plugin
class UploadPlugin:
"""IRC bot plugin for downloading files via yt-dlp and uploading them to hardfiles.org."""
@ -74,6 +86,31 @@ class UploadPlugin:
return ''
return str(value)
async def search_youtube(self, query):
"""
Search YouTube for the given query and return the URL of the first result.
Args:
query (str): The search query.
Returns:
str: The URL of the first search result.
"""
try:
request = youtube.search().list(
part="id",
q=query,
type="video",
maxResults=1
)
result = await self.bot.loop.run_in_executor(None, request.execute)
if result.get("items"):
video_id = result["items"][0]["id"]["videoId"]
return f"https://www.youtube.com/watch?v={video_id}"
except Exception as e:
self.bot.log.error(f"YouTube search error: {e}")
return None
@command
async def upload(self, mask, target, args):
"""
@ -85,18 +122,38 @@ class UploadPlugin:
args (dict): Parsed command arguments.
Usage:
%%upload [--mp3] <url>
%%upload [--mp3] <url_or_search_term>...
Example: !upload never gonna give you up
"""
url = args.get('<url>')
url_or_search_term = args.get('<url_or_search_term>')
# If multiple words are provided, join them into a single string.
if isinstance(url_or_search_term, list):
url_or_search_term = " ".join(url_or_search_term)
mp3 = args.get('--mp3')
if not url:
if not url_or_search_term:
self.bot.privmsg(
target,
ircstyle.style("Usage: !upload [--mp3] <url>", fg="red", bold=True, reset=True),
ircstyle.style("Usage: !upload [--mp3] <url_or_search_term>...", fg="red", bold=True, reset=True),
)
return
# If the input is not a URL, treat it as a search term
if not re.match(r'^https?://', url_or_search_term):
self.bot.privmsg(
target,
ircstyle.style(f"Searching YouTube for: {url_or_search_term}", fg="blue", bold=True, reset=True),
)
url = await self.search_youtube(url_or_search_term)
if not url:
self.bot.privmsg(
target,
ircstyle.style("No results found on YouTube.", fg="red", bold=True, reset=True),
)
return
else:
url = url_or_search_term
try:
await self.do_upload(url, target, mp3)
except Exception as exc: