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

This commit is contained in:
Tomáš Mládek 2019-05-20 11:54:10 +02:00
parent 48bb352d11
commit 4f0f6cfc59

View file

@ -3,6 +3,7 @@
import errno import errno
import logging import logging
import os import os
import pprint
import re import re
import shutil import shutil
import sys import sys
@ -288,17 +289,19 @@ class DelojzaBot:
filenames = download_fn(urls, out_path, message.date, message, audio=audio, filename=filename) filenames = download_fn(urls, out_path, message.date, message, audio=audio, filename=filename)
tumblr_ids = [] tumblr_ids = []
if hashtags[0] == 'TUMBLR' and self.tumblr_client: if hashtags[0] in ('TUMBLR', 'TUMBLR_NOW') and self.tumblr_client:
message.reply_text('(btw, queueing to tumblr)') 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: for filename in filenames:
response = self.tumblr_client.create_photo(self.tumblr_name, state="queue", data=filename) response = self.tumblr_client.create_photo(self.tumblr_name, data=filename,
tumblr_ids.append(response['id']) state="published" if now else "queue")
elif hashtags[0] == 'TUMBLR_NOW' and self.tumblr_client: if 'id' in response:
message.reply_text('(btw, ***FIRING TO TUMBLR RIGHT AWAY***)', tumblr_ids.append(response['id'])
parse_mode=telegram.ParseMode.MARKDOWN) else:
for filename in filenames: self.logger.warning("Did not receive 'id' in tumblr response: \n" + pprint.pformat(response))
response = self.tumblr_client.create_photo(self.tumblr_name, state="published", data=filename) message.reply_text('Something weird happened with the tumblrs, check it!')
tumblr_ids.append(response['id'])
self.last_downloaded = message.chat, filenames, hashtags, tumblr_ids self.last_downloaded = message.chat, filenames, hashtags, tumblr_ids
return filenames return filenames
except: except: