From 3964ec0fc2cef909372b2fc47ad935b532bb6355 Mon Sep 17 00:00:00 2001 From: phixxy Date: Wed, 4 Sep 2024 19:27:14 -0700 Subject: [PATCH] added a youtube-dl command --- cogs/ytdl.py | 26 ++++++++++++++++++++ data/ytdl/youtubedl.py | 54 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 cogs/ytdl.py create mode 100644 data/ytdl/youtubedl.py diff --git a/cogs/ytdl.py b/cogs/ytdl.py new file mode 100644 index 0000000..6322c2d --- /dev/null +++ b/cogs/ytdl.py @@ -0,0 +1,26 @@ +import os +import subprocess + +from discord.ext import commands +from cogs.base_cog.bot_base_cog import BotBaseCog + +class YoutubeDL(BotBaseCog): + + def __init__(self, bot): + super().__init__(bot) + self.setup(__class__.__name__) + + @commands.command() + async def youtubedl(self, ctx): + try: + url = f"\"{ctx.message.content.split(" ", 1)[1]}\"" + video_or_audio = ctx.message.content.split(" ", 2)[2] + process = subprocess.Popen(["python3", "data/ytdl/youtubedl.py", url, video_or_audio]) + process.wait() + output = process.returncode + await ctx.send(f"Downloaded {video_or_audio} from {url}, output: {output}") + except: + await ctx.send("Usage: !youtubedl ") + +async def setup(bot): + await bot.add_cog(YoutubeDL(bot)) \ No newline at end of file diff --git a/data/ytdl/youtubedl.py b/data/ytdl/youtubedl.py new file mode 100644 index 0000000..2769e2c --- /dev/null +++ b/data/ytdl/youtubedl.py @@ -0,0 +1,54 @@ +from sys import argv +import os +import time +import subprocess +#usage python3 youtubedl.py + +def download(url, video_or_audio): + if video_or_audio == "video": + process = subprocess.Popen(["yt-dlp", "--yes-playlist", f"{url}"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + print(process.stdout.read()) + process.wait() + return True + elif video_or_audio == "audio": + process = subprocess.Popen(["yt-dlp", "-x", "--yes-playlist", f"{url}"], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + print(process.stdout.read()) + process.wait() + return True + else: + print("Invalid argument") + return False + +def zip_all_files(): + #zip all files + current_epoch = time.time() + output_file = f"{current_epoch}.zip" + os.system(f"zip -r {output_file} *") + return output_file + +def upload_to_litterbox(input_file): + ''' If you want to make curl requests to the API, here is an example. Allowed values for "time" are 1h, 12h, 24h, and 72h. + curl -F "reqtype=fileupload" -F "time=1h" -F "fileToUpload=@cutie.png" https://litterbox.catbox.moe/resources/internals/api.php''' + command = f"curl -F 'reqtype=fileupload' -F 'time=1h' -F 'fileToUpload=@{input_file}' https://litterbox.catbox.moe/resources/internals/api.php" + output_url = os.popen(command).read() + #delete all files in current directory except this script + file_types = ["zip", "mp4", "mp3", "webm", "wav", "m4a", "flac", "ogg", "opus", "wma", "aac", "m4p", "m4b", "m4r", "m4v", "mp2", "mp3", "mp4", "mpa", "mpeg", "mpg", "mpv", "mxf", "ogg", "oga", "ogv", "ogx", "spx", "wav", "webm", "wma", "wv", "wvx", "weba", "webm", "webp", "wmv"] + for file in os.listdir(): + if file.split(".")[-1] in file_types: + os.remove(file) + return output_url + +def main(): + url, video_or_audio = argv[1], argv[2] + print(url, video_or_audio) + if download(url, video_or_audio): + zip_file = zip_all_files() + output_url = upload_to_litterbox(zip_file) + print(output_url) + return output_url + else: + print("Invalid argument") + return 1 + +if __name__ == "__main__": + main()