From 148286d6aac1c703cfdbe3bff4827c81c8a92ce9 Mon Sep 17 00:00:00 2001 From: Hodza Alban Date: Sun, 23 Jul 2017 10:17:25 +0200 Subject: [PATCH] Create main.py --- main.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..0650450 --- /dev/null +++ b/main.py @@ -0,0 +1,36 @@ +import os +import logging +from glob import glob + +import youtube_dl +from telegram.ext import Updater, MessageHandler, Filters + +logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.INFO) +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'): + 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'): # TODO this way for find the file(s) IMHO is not elegant + bot.send_document(chat_id=update.message.chat_id, document=open(f, 'rb')) + except Exception as e: + bot.sendMessage(chat_id=update.message.chat_id, text='Error') + logger.info(e) + +download_handler = MessageHandler(Filters.text, download) +dispatcher.add_handler(download_handler) + +updater.start_polling() +updater.idle()