attempt at error reporting numero deux

feature-unify_handlers
Tomáš Mládek 2018-01-31 12:30:08 +01:00 committed by Tomáš Mládek
parent 9dd00e7d03
commit 9ce44d1e52
1 changed files with 29 additions and 32 deletions

View File

@ -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()