From 9ce44d1e5254f899a7db543b01e324d3603d2185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Wed, 31 Jan 2018 12:30:08 +0100 Subject: [PATCH] attempt at error reporting numero deux --- robot.py | 61 +++++++++++++++++++++++++++----------------------------- 1 file changed, 29 insertions(+), 32 deletions(-) diff --git a/robot.py b/robot.py index d81778c..1a03172 100644 --- a/robot.py +++ b/robot.py @@ -1,5 +1,6 @@ import logging import os +import sys from datetime import datetime import requests @@ -51,6 +52,7 @@ def download_raw(url): f.write(chunk) +# noinspection PyBroadException def handle_url(bot, update): ytdl_urls = list(filter(ytdl_has, map(lambda e: update.message.text[e.offset:e.offset + e.length], @@ -59,36 +61,34 @@ def handle_url(bot, update): if len(ytdl_urls) > 0: logger.info("Downloading %s" % ytdl_urls) update.message.reply_text('Downloading now...') - download_ydl(ytdl_urls) + try: + download_ydl(ytdl_urls) + except: + type, value, _ = sys.exc_info() + update.message.reply_text("Something is FUCKED: %s, %s" % (type, value)) -def handle_photo(bot, update): - photo = max(update.message.photo, key=lambda p: p.width) - url = bot.getFile(photo.file_id).file_path - update.message.reply_text('Downloading now...') - logger.info("Downloading '%s'" % url) - download_raw(url) - - -def handle_audio(bot, update): - url = bot.getFile(update.message.audio.file_id).file_path - update.message.reply_text('Downloading now...') - logger.info("Downloading '%s'" % url) - download_raw(url) - - -def handle_video(bot, update): - url = bot.getFile(update.message.video.file_id).file_path - update.message.reply_text('Downloading now...') - logger.info("Downloading '%s'" % url) - download_raw(url) - - -def handle_document(bot, update): - url = bot.getFile(update.message.document.file_id).file_path - update.message.reply_text('Downloading now...') - logger.info("Downloading '%s'" % url) - download_raw(url) +# noinspection PyBroadException +def handle_rest(bot, update): + file = None + if update.message.photo is not None: + photo = max(update.message.photo, key=lambda p: p.width) + file = photo.file_id + elif update.message.document is not None: + file = update.message.document.file_id + elif update.message.audio is not None: + file = update.message.audio.file_id + elif update.message.video is not None: + file = update.message.video.file_id + if file is not None: + url = bot.getFile(file).file_path + update.message.reply_text('Downloading now...') + logger.info("Downloading '%s'" % url) + try: + download_raw(url) + except: + type, value, _ = sys.exc_info() + update.message.reply_text("Something is FUCKED: %s, %s" % (type, value)) def start(bot, update): @@ -111,10 +111,7 @@ def main(): dp.add_error_handler(error) dp.add_handler(MessageHandler(Filters.entity(MessageEntity.URL), handle_url)) - dp.add_handler(MessageHandler(Filters.photo, handle_photo)) - dp.add_handler(MessageHandler(Filters.audio, handle_audio)) - dp.add_handler(MessageHandler(Filters.video, handle_video)) - dp.add_handler(MessageHandler(Filters.document, handle_document)) + dp.add_handler(MessageHandler(Filters.photo | Filters.video | Filters.audio | Filters.document, handle_rest)) updater.start_polling()