From 949008cb0554c9640de0db2cd45cbe32b95c030c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Wed, 15 May 2019 11:18:11 +0200 Subject: [PATCH] tumblr updates (see details); fixed initial "/" in /delete - /delete also cancels queued or deletes published tumblr posts - tumblr blog name in config --- delojza.py | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/delojza.py b/delojza.py index 50d2ab8..50fa677 100755 --- a/delojza.py +++ b/delojza.py @@ -40,7 +40,8 @@ def datestr(date): class DelojzaBot: - def __init__(self, tg_api_key, out_dir, tmp_dir=None, acoustid_key=None, tumblr_keys=None, markov=None): + def __init__(self, tg_api_key, out_dir, tmp_dir=None, + acoustid_key=None, tumblr_name=None, tumblr_keys=None, markov=None): self.logger = logging.getLogger("delojza") self.out_dir = os.path.abspath(out_dir) @@ -69,10 +70,11 @@ class DelojzaBot: self.acoustid_key = acoustid_key - if tumblr_keys: - self.client = pytumblr.TumblrRestClient(*tumblr_keys) + if tumblr_name and tumblr_keys: + self.tumblr_name = tumblr_name + self.tumblr_client = pytumblr.TumblrRestClient(*tumblr_keys) - self.last_downloaded = [] + self.last_downloaded = None self.last_hashtags = None @staticmethod @@ -263,20 +265,23 @@ class DelojzaBot: audio = any([any([tag in hashtag for tag in ('AUDIO', 'RADIO')]) for hashtag in hashtags]) if audio and download_fn != self.download_raw: reply += ' (And also guessing you want to extract the audio)' - message.reply_text(reply) filenames = download_fn(urls, out_path, message.date, message, audio=audio, filename=filename) - if hashtags[0] == 'TUMBLR' and self.client: + tumblr_ids = [] + + if hashtags[0] == 'TUMBLR' and self.tumblr_client: message.reply_text('(btw, queueing to tumblr)') for filename in filenames: - self.client.create_photo('kunsaxan', state="queue", data=filename) - elif hashtags[0] == 'TUMBLR_NOW' and self.client: + 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: - self.client.create_photo('kunsaxan', state="published", data=filename) - self.last_downloaded = filenames + response = self.tumblr_client.create_photo(self.tumblr_name, state="published", data=filename) + tumblr_ids.append(response['id']) + self.last_downloaded = filenames, hashtags, tumblr_ids return filenames except: _, exc_value, __ = sys.exc_info() @@ -397,9 +402,10 @@ class DelojzaBot: update.message.reply_text(tmp_reply) def tg_delete(self, _, update): - if len(self.last_downloaded) > 0: - for file in self.last_downloaded: - update.message.reply_text("Removing \"{}\"!".format(file[len(self.out_dir):])) + if self.last_downloaded is not None: + files, hashtags, tumblr_ids = self.last_downloaded + for file in files: + update.message.reply_text("Removing \"{}\"!".format(file[len(self.out_dir) + 1:])) os.remove(file) parent_dir = os.path.dirname(file) while True: @@ -410,7 +416,13 @@ class DelojzaBot: parent_dir = os.path.dirname(parent_dir) if parent_dir == self.out_dir: break - self.last_downloaded.clear() + if len(tumblr_ids) > 0: + plural = "s (all {} of them)".format(len(tumblr_ids)) if len(tumblr_ids) > 1 else "" + update.message.reply_text("Also deleting tumblr post{}!".format(plural)) + for tumblr_id in tumblr_ids: + if self.tumblr_client: + self.tumblr_client.delete_post(self.tumblr_name, tumblr_id) + self.last_downloaded = None else: update.message.reply_text("Nothing to remove!") @@ -486,6 +498,7 @@ if __name__ == '__main__': config.get('delojza', 'OUT_DIR', fallback=os.path.join(_DIR_, "out")), tmp_dir=config.get('delojza', 'tmp_dir', fallback=tempfile.gettempdir()), acoustid_key=config.get('delojza', 'acoustid_api_key'), + tumblr_name=config.get('tumblr', 'blog_name'), tumblr_keys=(config.get('tumblr', 'consumer_key'), config.get('tumblr', 'consumer_secret'), config.get('tumblr', 'oauth_key'),