commit b7d47701bfa00cf8a7bf4db14b41e0e3f04580a3 Author: Matthew Adams Date: Tue Mar 2 18:24:24 2021 +1000 Initial commit diff --git a/downloader.py b/downloader.py new file mode 100644 index 0000000..a1396da --- /dev/null +++ b/downloader.py @@ -0,0 +1,26 @@ +import youtube_dl + +def download(videoUrl): + ydl = youtube_dl.YoutubeDL({'outtmpl': '%(id)s.mp4'}) + + ydl_opts = {} + + with ydl: + result = ydl.extract_info( + videoUrl, + download=True + ) + + if 'entries' in result: + # Can be a playlist or a list of videos + video = result['entries'][0] + return "Error: More than 1 result found" + else: + # Just a video + video = result + + print(video) + video_url = video['title'] + videoId = video['id'] + + return videoId + ".mp4" diff --git a/main.py b/main.py new file mode 100644 index 0000000..77c2ede --- /dev/null +++ b/main.py @@ -0,0 +1,51 @@ +import discord +import os +import ffmpeg +from dotenv import load_dotenv +from downloader import download + +load_dotenv() + +client = discord.Client() + +@client.event +async def on_ready(): + print('We have logged in as {0.user}'.format(client)) + +@client.event +async def on_message(message): + if message.author == client.user: + return + + if message.content.startswith('$hello'): + await message.channel.send('Hello!') + + if message.content.startswith('https'): + await message.channel.send('TikBot downloading video now!') + downloadResult = download(message.content) + print(downloadResult) + + if(downloadResult.startswith("Error")): + await message.channel.send('TikBot has failed you. Consider berating my human if this was not expected.') + return + + # Check file size, if it's small enough just send it! + fileSize = os.stat(downloadResult).st_size + + if(fileSize < 8000000): + with open(downloadResult, 'rb') as fp: + await message.channel.send(file=discord.File(fp, str(downloadResult))) + os.remove(downloadResult) + + else: + # We need to compress the file below 8MB or discord will make a sad + await message.channel.send('TikBot will compress your video for you as it is too chonky, please give me a sec.') + ffmpeg.input(downloadResult).output("small_" + downloadResult, **{'b:v': '1000k'}).run() + with open("small_" + downloadResult, 'rb') as fp: + await message.channel.send(file=discord.File(fp, str("small_" + downloadResult))) + # Delete the compressed and original file + os.remove(downloadResult) + os.remove("small_" + downloadResult) + + +client.run(os.getenv('TOKEN')) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ce972f9 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +youtube_dl +discord +python-dotenv +ffmpeg-python \ No newline at end of file