From 4f0f6cfc59b302d29c181977d84f87c63110477f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Mon, 20 May 2019 11:54:10 +0200 Subject: [PATCH] handle 'id' possibly missing in tumblr response, refactor tumblr upload --- delojza.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/delojza.py b/delojza.py index e4cab82..13608df 100755 --- a/delojza.py +++ b/delojza.py @@ -3,6 +3,7 @@ import errno import logging import os +import pprint import re import shutil import sys @@ -288,17 +289,19 @@ class DelojzaBot: filenames = download_fn(urls, out_path, message.date, message, audio=audio, filename=filename) tumblr_ids = [] - if hashtags[0] == 'TUMBLR' and self.tumblr_client: - message.reply_text('(btw, queueing to tumblr)') + if hashtags[0] in ('TUMBLR', 'TUMBLR_NOW') and self.tumblr_client: + now = hashtags[0] == 'TUMBLR_NOW' + reply = '(btw, {})'.format("***FIRING TO TUMBLR RIGHT AWAY***" if now else "queueing to tumblr") + message.reply_text(reply, parse_mode=telegram.ParseMode.MARKDOWN) for filename in filenames: - response = self.tumblr_client.create_photo(self.tumblr_name, state="queue", data=filename) - tumblr_ids.append(response['id']) - elif hashtags[0] == 'TUMBLR_NOW' and self.tumblr_client: - message.reply_text('(btw, ***FIRING TO TUMBLR RIGHT AWAY***)', - parse_mode=telegram.ParseMode.MARKDOWN) - for filename in filenames: - response = self.tumblr_client.create_photo(self.tumblr_name, state="published", data=filename) - tumblr_ids.append(response['id']) + response = self.tumblr_client.create_photo(self.tumblr_name, data=filename, + state="published" if now else "queue") + if 'id' in response: + tumblr_ids.append(response['id']) + else: + self.logger.warning("Did not receive 'id' in tumblr response: \n" + pprint.pformat(response)) + message.reply_text('Something weird happened with the tumblrs, check it!') + self.last_downloaded = message.chat, filenames, hashtags, tumblr_ids return filenames except: