tumblr updates (see details); fixed initial "/" in /delete

- /delete also cancels queued or deletes published tumblr posts
- tumblr blog name in config
This commit is contained in:
Tomáš Mládek 2019-05-15 11:18:11 +02:00
parent 6ad4eadd79
commit 949008cb05

View file

@ -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'),