added a youtube-dl command

This commit is contained in:
phixxy 2024-09-04 19:27:14 -07:00
parent 8a3c18b857
commit 3964ec0fc2
2 changed files with 80 additions and 0 deletions

26
cogs/ytdl.py Normal file
View file

@ -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 <url> <video|audio>")
async def setup(bot):
await bot.add_cog(YoutubeDL(bot))

54
data/ytdl/youtubedl.py Normal file
View file

@ -0,0 +1,54 @@
from sys import argv
import os
import time
import subprocess
#usage python3 youtubedl.py <url> <video|audio>
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()