Added multiuser support

This commit is contained in:
Hodza Alban 2017-08-14 20:42:37 +02:00 committed by GitHub
parent ad28329a3e
commit eeddd65c81

25
main.py
View File

@ -1,22 +1,27 @@
import logging import logging
from time import sleep
from telegram import InlineKeyboardMarkup from telegram import InlineKeyboardMarkup
from telegram.ext import Updater, CallbackQueryHandler, MessageHandler, Filters from telegram.ext import Updater, CallbackQueryHandler, MessageHandler, Filters
from vid_utils import Video from vid_utils import Video, VideoQueue, BadLink
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO) level=logging.INFO)
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
video = None VIDEOS = VideoQueue()
def get_format(bot, update): def get_format(bot, update):
logger.info(update.message.text) # "history" logger.info("from {}: {}".format(update.message.chat_id, update.message.text)) # "history"
global video
video = Video(update.message.text, update.message.chat_id)
try:
video = Video(update.message.text, update.message.chat_id)
except BadLink:
update.message.reply_text("Bad link")
else:
reply_markup = InlineKeyboardMarkup(video.keyboard) reply_markup = InlineKeyboardMarkup(video.keyboard)
VIDEOS.append(video)
update.message.reply_text('Choose format:', reply_markup=reply_markup) update.message.reply_text('Choose format:', reply_markup=reply_markup)
@ -25,12 +30,22 @@ def download_choosen_format(bot, update):
bot.edit_message_text(text="Downloading...", bot.edit_message_text(text="Downloading...",
chat_id=query.message.chat_id, chat_id=query.message.chat_id,
message_id=query.message.message_id) message_id=query.message.message_id)
while VIDEOS.lock: sleep(1) # finish old download
VIDEOS.lock = True # maybe we can use a contextmanager?
for i, video in enumerate(VIDEOS):
if video.chat_id == query.message.chat_id:
VIDEOS.pop(i)
video.download(query.data) video.download(query.data)
with video.send() as files: with video.send() as files:
for f in files: for f in files:
bot.send_document(chat_id=query.message.chat_id, document=open(f, 'rb')) bot.send_document(chat_id=query.message.chat_id, document=open(f, 'rb'))
VIDEOS.lock = False
updater = Updater(token=YOUR_TOKEN) updater = Updater(token=YOUR_TOKEN)