diff --git a/main.py b/main.py new file mode 100644 index 0000000..5b635bb --- /dev/null +++ b/main.py @@ -0,0 +1,64 @@ +from __future__ import unicode_literals +import simplematrixbotlib as botlib +from re import search +import urllib.parse +import youtube_dl +import os + +try: + passfile = open("pass.txt", "r") + passw = passfile.read().translate({ord(c): None for c in '\n'}) +except: + print("Put the password in pass.txt in this directory") + +creds = botlib.Creds("https://chat.trygve.me", "video_bot", passw) +bot = botlib.Bot(creds) + +prefix = 'https://' + +class MyLogger(object): + def debug(self, msg): + pass + + def warning(self, msg): + pass + + def error(self, msg): + print(msg) + + +def my_hook(d): + if d['status'] == 'finished': + print('Done downloading') + + +ydl_opts = { + 'format': 'bestvideo[ext=webm]+bestaudio[ext=webm]/best[ext=mp4]/best', + 'outtmpl': '/var/www/html/video_bot/%(title)s.%(ext)s', + 'restrictfilenames': False, + 'logger': MyLogger(), + 'progress_hooks': [my_hook], +} + +def filterfunc(variable): + if search('www.youtube.com/watch', variable): + return True + else: + return False + +async def youtube(room, message): + match = botlib.MessageMatch(room, message, bot) + pieces = str(message).split() + links = filter(filterfunc, pieces) + for i in links: + with youtube_dl.YoutubeDL(ydl_opts) as ydl: + info_dict = ydl.extract_info(i, download=False) + fname = urllib.parse.quote(info_dict.get('title', None).replace('|', '_')) + #fname = fname.translate({ord(c): None for c in ',&!|'}) + url = 'https://trygve.me/video_bot/' + fname + '.webm' + ydl.download([i]) + await bot.api.send_text_message(room.room_id, url) + +bot.add_message_listener(youtube) + +bot.run()