This commit is contained in:
Trygve 2022-07-31 21:38:51 +02:00
parent 307e80c952
commit c86eecfad1

65
main.py
View File

@ -1,20 +1,9 @@
from __future__ import unicode_literals
import simplematrixbotlib as botlib import simplematrixbotlib as botlib
from re import search from re import search
import urllib.parse from urllib.parse import quote as sanitize_url
import youtube_dl import yt_dlp
import os
try: from config import config
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): class MyLogger(object):
def debug(self, msg): def debug(self, msg):
@ -27,40 +16,46 @@ class MyLogger(object):
print(msg) print(msg)
def my_hook(d): creds = botlib.Creds(config['url'], config['username'], config['password'])
if d['status'] == 'finished': bot = botlib.Bot(creds)
def progress_hook(d):
if d['status'] == 'started':
print('Started downloading')
elif d['status'] == 'finished':
print('Done downloading') print('Done downloading')
ydl_opts = { ydl_opts = {
'format': 'bestvideo[ext=webm]+bestaudio[ext=webm]/best[ext=mp4]/best', 'format': config['yt_dlp_format'],
'outtmpl': '/var/www/html/video_bot/%(title)s.%(ext)s', 'outtmpl': config['download_dir'] + '/%(title)s.%(ext)s',
'restrictfilenames': False, 'restrictfilenames': True,
'logger': MyLogger(), 'logger': MyLogger(),
'progress_hooks': [my_hook], 'progress_hooks': [progress_hook],
} }
def filterfunc(variable): def get_yt_vid_id(word):
if search('www.youtube.com/watch', variable): if search('www.youtube.com/watch', word):
return True return word
elif search('youtu.be', word):
return word.replace('https://youtu.be/', 'https://www.youtube.com/watch?v=')
else: else:
return False return False
@bot.listener.on_message_event
async def youtube(room, message): async def youtube(room, message):
match = botlib.MessageMatch(room, message, bot) match = botlib.MessageMatch(room, message, bot)
pieces = str(message).split() pieces = str(message).split()
links = filter(filterfunc, pieces)
try: try:
for i in links: for i in pieces:
with youtube_dl.YoutubeDL(ydl_opts) as ydl: yt_url = get_yt_vid_id(i)
info_dict = ydl.extract_info(i, download=False) if yt_url:
fname = urllib.parse.quote(info_dict.get('title', None).replace('|', '_')) with yt_dlp.YoutubeDL(ydl_opts) as ydl:
url = 'https://trygve.me/video_bot/' + fname + '.webm' info_dict = ydl.extract_info(yt_url, download=False)
ydl.download([i]) fname = sanitize_url(yt_dlp.utils.sanitize_filename(info_dict.get('title', None), restricted=True))
await bot.api.send_text_message(room.room_id, url) url = 'https://trygve.me/video_bot/' + fname + '.webm'
ydl.download([yt_url])
await bot.api.send_text_message(room.room_id, url)
except Exception as e: except Exception as e:
await bot.api.send_text_message(room.room_id, str(e)) await bot.api.send_text_message(room.room_id, str(e))
bot.add_message_listener(youtube)
bot.run() bot.run()