Compare commits
	
		
			No commits in common. "c86eecfad1ec904b7a1892c3300fb18e94a5d21a" and "11a639be5d6b3260aee80da5a8ecd44da29f397e" have entirely different histories.
		
	
	
		
			c86eecfad1
			...
			11a639be5d
		
	
		
@ -1,7 +0,0 @@
 | 
			
		||||
config {
 | 
			
		||||
        'server_url': 'https://example.com',
 | 
			
		||||
        'username': '',
 | 
			
		||||
        'password': '',
 | 
			
		||||
        'yt_dlp_format': 'bestvideo[ext=webm]+bestaudio[ext=webm]/best[ext=mp4]/best',
 | 
			
		||||
        'download_dir': './videos',
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										65
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										65
									
								
								main.py
									
									
									
									
									
								
							@ -1,9 +1,20 @@
 | 
			
		||||
from __future__ import unicode_literals
 | 
			
		||||
import simplematrixbotlib as botlib
 | 
			
		||||
from re import search
 | 
			
		||||
from urllib.parse import quote as sanitize_url
 | 
			
		||||
import yt_dlp
 | 
			
		||||
import urllib.parse
 | 
			
		||||
import youtube_dl
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
from config import config
 | 
			
		||||
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):
 | 
			
		||||
@ -16,46 +27,40 @@ class MyLogger(object):
 | 
			
		||||
        print(msg)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
creds = botlib.Creds(config['url'], config['username'], config['password'])
 | 
			
		||||
bot = botlib.Bot(creds)
 | 
			
		||||
 | 
			
		||||
def progress_hook(d):
 | 
			
		||||
    if d['status'] == 'started':
 | 
			
		||||
        print('Started downloading')
 | 
			
		||||
    elif d['status'] == 'finished':
 | 
			
		||||
def my_hook(d):
 | 
			
		||||
    if d['status'] == 'finished':
 | 
			
		||||
        print('Done downloading')
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
ydl_opts = {
 | 
			
		||||
    'format': config['yt_dlp_format'],
 | 
			
		||||
    'outtmpl': config['download_dir'] + '/%(title)s.%(ext)s',
 | 
			
		||||
    'restrictfilenames': True,
 | 
			
		||||
    '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': [progress_hook],
 | 
			
		||||
    'progress_hooks': [my_hook],
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
def get_yt_vid_id(word):
 | 
			
		||||
    if search('www.youtube.com/watch', word):
 | 
			
		||||
        return word
 | 
			
		||||
    elif search('youtu.be', word):
 | 
			
		||||
        return word.replace('https://youtu.be/', 'https://www.youtube.com/watch?v=')
 | 
			
		||||
def filterfunc(variable):
 | 
			
		||||
    if search('www.youtube.com/watch', variable):
 | 
			
		||||
        return True
 | 
			
		||||
    else:
 | 
			
		||||
        return False
 | 
			
		||||
 | 
			
		||||
@bot.listener.on_message_event
 | 
			
		||||
async def youtube(room, message):
 | 
			
		||||
    match = botlib.MessageMatch(room, message, bot)
 | 
			
		||||
    pieces = str(message).split()
 | 
			
		||||
    links = filter(filterfunc, pieces)
 | 
			
		||||
    try:
 | 
			
		||||
        for i in pieces:
 | 
			
		||||
            yt_url = get_yt_vid_id(i)
 | 
			
		||||
            if yt_url:
 | 
			
		||||
                with yt_dlp.YoutubeDL(ydl_opts) as ydl:
 | 
			
		||||
                    info_dict = ydl.extract_info(yt_url, download=False)
 | 
			
		||||
                    fname = sanitize_url(yt_dlp.utils.sanitize_filename(info_dict.get('title', None), restricted=True))
 | 
			
		||||
                    url = 'https://trygve.me/video_bot/' + fname + '.webm'
 | 
			
		||||
                    ydl.download([yt_url])
 | 
			
		||||
                    await bot.api.send_text_message(room.room_id, url)
 | 
			
		||||
        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('|', '_'))
 | 
			
		||||
                url = 'https://trygve.me/video_bot/' + fname + '.webm'
 | 
			
		||||
                ydl.download([i])
 | 
			
		||||
            await bot.api.send_text_message(room.room_id, url)
 | 
			
		||||
    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()
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user