Initial commit

This commit is contained in:
Matthew Adams
2021-03-02 18:24:24 +10:00
commit b7d47701bf
3 changed files with 81 additions and 0 deletions

26
downloader.py Normal file
View File

@@ -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"

51
main.py Normal file
View File

@@ -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'))

4
requirements.txt Normal file
View File

@@ -0,0 +1,4 @@
youtube_dl
discord
python-dotenv
ffmpeg-python