telegram-video-downloader/main.py

56 lines
1.7 KiB
Python
Raw Normal View History

2017-07-23 10:17:25 +02:00
import logging
2021-06-19 20:40:49 +02:00
import os
2017-07-23 10:17:25 +02:00
2017-08-09 14:49:25 +02:00
from telegram import InlineKeyboardMarkup
from telegram.ext import Updater, CallbackQueryHandler, MessageHandler, Filters
2017-08-06 15:49:22 +02:00
2017-08-19 18:41:50 +02:00
from vid_utils import Video, BadLink
2017-07-23 10:17:25 +02:00
2017-08-19 18:41:50 +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__)
2021-06-19 20:35:07 +02:00
def get_format(update, context):
logger.info("from {}: {}".format(update.message.chat_id, update.message.text)) # "history"
2017-08-14 20:42:37 +02:00
try:
2021-06-19 20:35:07 +02:00
video = Video(link=update.message.text, init_keyboard=True)
except BadLink as e:
update.message.reply_text("Bad link: {}".format(e))
2017-08-14 20:42:37 +02:00
else:
reply_markup = InlineKeyboardMarkup(video.keyboard)
update.message.reply_text('Choose format:', reply_markup=reply_markup)
2021-06-19 20:35:07 +02:00
def download_choosen_format(update, context):
query = update.callback_query
2021-06-19 20:35:07 +02:00
video_index = query.data
context.bot.edit_message_text(text="Downloading...",
chat_id=query.message.chat_id,
message_id=query.message.message_id)
video = Video(vid=video_index)
video.download()
2017-08-09 14:49:25 +02:00
with video.send() as files:
for f in files:
2021-06-19 20:35:07 +02:00
logger.log("Sending... {} ".format(f))
context.bot.send_document(chat_id=query.message.chat_id, document=open(f, 'rb'))
context.bot.delete_message(chat_id=query.message.chat_id,
message_id=query.message.message_id)
2021-06-19 20:40:49 +02:00
TOKEN = None
with open('bot.token') as f:
TOKEN = f.read().strip()
2021-06-19 20:40:49 +02:00
updater = Updater(token=TOKEN)
2017-07-23 10:17:25 +02:00
updater.dispatcher.add_handler(MessageHandler(Filters.text, get_format))
updater.dispatcher.add_handler(CallbackQueryHandler(download_choosen_format))
2017-07-23 10:17:25 +02:00
2017-08-19 18:41:50 +02:00
2017-07-23 10:17:25 +02:00
updater.start_polling()
updater.idle()