telegram-video-downloader/main.py

43 lines
1.3 KiB
Python
Raw Normal View History

2017-07-23 10:17:25 +02:00
import os
import logging
from glob import glob
import youtube_dl
from telegram.ext import Updater, MessageHandler, Filters
2017-08-06 15:49:22 +02:00
from vid_utils import check_dimension
2017-07-23 10:17:25 +02:00
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
2017-07-23 10:17:25 +02:00
logger = logging.getLogger(__name__)
updater = Updater(token='TOKEN') # put here the bot's token
dispatcher = updater.dispatcher
ydl_opts = {
'restrictfilenames': True,
}
def download(bot, update):
for f in glob('*.mp4*') + glob('*.webm*'): # with glob it isn't possible to check multiple extension in one regex
2017-07-23 10:17:25 +02:00
os.remove(f) # remove old video(s)
try:
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
ydl.download([update.message.text])
for f in glob('*.mp4*') + glob('*.webm*'): # if the video is bigger than 50MB split it
check_dimension(f)
break # check first file
for f in glob('*.mp4*') + glob('*.webm*'): # send document(s)
2017-07-23 10:17:25 +02:00
bot.send_document(chat_id=update.message.chat_id, document=open(f, 'rb'))
2017-07-23 10:17:25 +02:00
except Exception as e:
bot.sendMessage(chat_id=update.message.chat_id, text='Error: {}'.format(e))
2017-07-23 10:17:25 +02:00
logger.info(e)
download_handler = MessageHandler(Filters.text, download)
dispatcher.add_handler(download_handler)
updater.start_polling()
updater.idle()