Update upload to combine with youtube
This commit is contained in:
parent
c91590177c
commit
ea4d5365db
@ -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.
|
optionally convert videos to MP3 format before uploading. Files larger than 100MB are rejected.
|
||||||
|
|
||||||
Usage:
|
Usage:
|
||||||
!upload [--mp3] <url>
|
!upload [--mp3] <url_or_search_term>...
|
||||||
|
Example: !upload never gonna give you up
|
||||||
|
|
||||||
Dependencies:
|
Dependencies:
|
||||||
- aiohttp
|
- aiohttp
|
||||||
@ -33,6 +34,7 @@ import ircstyle
|
|||||||
import yt_dlp
|
import yt_dlp
|
||||||
from yt_dlp.utils import DownloadError
|
from yt_dlp.utils import DownloadError
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
import googleapiclient.discovery
|
||||||
|
|
||||||
# Global headers to mimic a real browser (ban evasion)
|
# Global headers to mimic a real browser (ban evasion)
|
||||||
HEADERS = {
|
HEADERS = {
|
||||||
@ -45,6 +47,16 @@ HEADERS = {
|
|||||||
"Upgrade-Insecure-Requests": "1"
|
"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
|
@irc3.plugin
|
||||||
class UploadPlugin:
|
class UploadPlugin:
|
||||||
"""IRC bot plugin for downloading files via yt-dlp and uploading them to hardfiles.org."""
|
"""IRC bot plugin for downloading files via yt-dlp and uploading them to hardfiles.org."""
|
||||||
@ -74,6 +86,31 @@ class UploadPlugin:
|
|||||||
return ''
|
return ''
|
||||||
return str(value)
|
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
|
@command
|
||||||
async def upload(self, mask, target, args):
|
async def upload(self, mask, target, args):
|
||||||
"""
|
"""
|
||||||
@ -85,18 +122,38 @@ class UploadPlugin:
|
|||||||
args (dict): Parsed command arguments.
|
args (dict): Parsed command arguments.
|
||||||
|
|
||||||
Usage:
|
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')
|
mp3 = args.get('--mp3')
|
||||||
|
|
||||||
if not url:
|
if not url_or_search_term:
|
||||||
self.bot.privmsg(
|
self.bot.privmsg(
|
||||||
target,
|
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
|
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:
|
try:
|
||||||
await self.do_upload(url, target, mp3)
|
await self.do_upload(url, target, mp3)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
|
Loading…
Reference in New Issue
Block a user