remember tags and last downloaded per each chat

neu
Tomáš Mládek 2019-05-29 13:43:08 +02:00
parent 194b59f89c
commit 4c585496a9
1 changed files with 42 additions and 44 deletions

View File

@ -75,8 +75,8 @@ class DelojzaBot:
self.protected_chats = protected_chats or [] self.protected_chats = protected_chats or []
self.protected_tags = protected_tags or [] self.protected_tags = protected_tags or []
self.last_downloaded = None self.last_downloaded = {}
self.last_hashtags = None self.last_hashtags = {}
@staticmethod @staticmethod
def ytdl_can(url): def ytdl_can(url):
@ -252,9 +252,9 @@ class DelojzaBot:
def _get_hashtags(self, message): def _get_hashtags(self, message):
hashtags = self.extract_hashtags(message) hashtags = self.extract_hashtags(message)
if len(hashtags) == 0 and self.last_hashtags is not None: if len(hashtags) == 0 and self.last_hashtags.get(message.chat.id) is not None:
user, chat, ts, last_hashtags = self.last_hashtags user, ts, last_hashtags = self.last_hashtags[message.chat.id]
if user == message.from_user and chat == message.chat and ts > datetime.now() - timedelta(hours=1): if user == message.from_user and ts > datetime.now() - timedelta(hours=1):
hashtags = last_hashtags hashtags = last_hashtags
return hashtags return hashtags
@ -291,7 +291,7 @@ class DelojzaBot:
.format(urls, message.chat.title, hashtags)) .format(urls, message.chat.title, hashtags))
hashtags.insert(0, "PUBLIC") hashtags.insert(0, "PUBLIC")
self.last_hashtags = None self.last_hashtags[message.chat.id] = None
self.logger.info("Downloading %s under '%s'" % (urls, "/".join(hashtags))) self.logger.info("Downloading %s under '%s'" % (urls, "/".join(hashtags)))
@ -323,7 +323,7 @@ class DelojzaBot:
self.logger.warning("Did not receive 'id' in tumblr response: \n" + pprint.pformat(response)) 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!') message.reply_text('Something weird happened with the tumblrs, check it!')
self.last_downloaded = message.chat, filenames, hashtags, tumblr_ids self.last_downloaded[message.chat.id] = filenames, hashtags, tumblr_ids
return filenames return filenames
except: except:
exc_type, exc_value, __ = sys.exc_info() exc_type, exc_value, __ = sys.exc_info()
@ -377,7 +377,7 @@ class DelojzaBot:
self.handle_tg_message(update.message.reply_to_message, bot, hashtags) self.handle_tg_message(update.message.reply_to_message, bot, hashtags)
self.handle_urls(update.message.reply_to_message, hashtags) self.handle_urls(update.message.reply_to_message, hashtags)
else: else:
self.last_hashtags = update.message.from_user, update.message.chat, datetime.now(), hashtags self.last_hashtags[update.message.chat.id] = update.message.from_user, datetime.now(), hashtags
else: else:
if self.markov: if self.markov:
self.markov.add_to_corpus(update.message.text) self.markov.add_to_corpus(update.message.text)
@ -450,45 +450,43 @@ class DelojzaBot:
def tg_retag(self, _, update): def tg_retag(self, _, update):
message_is_sensible = update.message.text.count("-") == 1 message_is_sensible = update.message.text.count("-") == 1
if self.last_downloaded is not None and message_is_sensible: if self.last_downloaded.get(update.message.chat.id) is not None and message_is_sensible:
chat, files, hashtags, tumblr_ids = self.last_downloaded files, hashtags, tumblr_ids = self.last_downloaded[update.message.chat.id]
if chat == update.message.chat: mp3s = [filename for filename in files if filename.endswith("mp3")]
mp3s = [filename for filename in files if filename.endswith("mp3")] if len(mp3s) > 0:
if len(mp3s) > 0: tagline = re.sub(r'^/[\w]+', '', update.message.text).split("-")
tagline = re.sub(r'^/[\w]+', '', update.message.text).split("-") artist = tagline[0].strip()
artist = tagline[0].strip() title = tagline[1].strip()
title = tagline[1].strip() for mp3 in mp3s:
for mp3 in mp3s: self._tag_file(mp3, artist, title)
self._tag_file(mp3, artist, title) update.message.reply_text("Tagging \"{}\" as \"{}\" by \"{}\"!"
update.message.reply_text("Tagging \"{}\" as \"{}\" by \"{}\"!" .format(mp3[len(self.out_dir) + 1:], title, artist))
.format(mp3[len(self.out_dir) + 1:], title, artist)) return
return
update.message.reply_text((self.markov.make_sentence() if self.markov and random() > .7 else "") + "???") update.message.reply_text((self.markov.make_sentence() if self.markov and random() > .7 else "") + "???")
def tg_delete(self, _, update): def tg_delete(self, _, update):
if self.last_downloaded is not None: if self.last_downloaded.get(update.message.chat.id) is not None:
chat, files, hashtags, tumblr_ids = self.last_downloaded files, hashtags, tumblr_ids = self.last_downloaded[update.message.chat.id]
if chat == update.message.chat: for file in files:
for file in files: update.message.reply_text("Removing \"{}\"!".format(file[len(self.out_dir) + 1:]))
update.message.reply_text("Removing \"{}\"!".format(file[len(self.out_dir) + 1:])) os.remove(file)
os.remove(file) parent_dir = os.path.dirname(file)
parent_dir = os.path.dirname(file) while True:
while True: if len(os.listdir(parent_dir)) == 0:
if len(os.listdir(parent_dir)) == 0: update.message.reply_text("Removing directory \"{}\" as it's empty..."
update.message.reply_text("Removing directory \"{}\" as it's empty..." .format(parent_dir[len(self.out_dir) + 1:]))
.format(parent_dir[len(self.out_dir) + 1:])) os.rmdir(parent_dir)
os.rmdir(parent_dir) parent_dir = os.path.dirname(parent_dir)
parent_dir = os.path.dirname(parent_dir) if parent_dir == self.out_dir:
if parent_dir == self.out_dir: break
break if len(tumblr_ids) > 0:
if len(tumblr_ids) > 0: plural = "s (all {} of them)".format(len(tumblr_ids)) if len(tumblr_ids) > 1 else ""
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))
update.message.reply_text("Also deleting tumblr post{}!".format(plural)) for tumblr_id in tumblr_ids:
for tumblr_id in tumblr_ids: if self.tumblr_client:
if self.tumblr_client: self.tumblr_client.delete_post(self.tumblr_name, tumblr_id)
self.tumblr_client.delete_post(self.tumblr_name, tumblr_id) self.last_downloaded[update.message.chat.id] = None
self.last_downloaded = None return
return
update.message.reply_text("Nothing to remove!") update.message.reply_text("Nothing to remove!")
# noinspection PyMethodMayBeStatic # noinspection PyMethodMayBeStatic