handle 'id' possibly missing in tumblr response, refactor tumblr upload

feature-unify_handlers
Tomáš Mládek 2019-05-20 11:54:10 +02:00
parent 48bb352d11
commit 4f0f6cfc59
1 changed files with 13 additions and 10 deletions

View File

@ -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: