remember tags and last downloaded per each chat

This commit is contained in:
Tomáš Mládek 2019-05-29 13:43:08 +02:00
parent 194b59f89c
commit 4c585496a9

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,9 +450,8 @@ 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("-")
@ -466,9 +465,8 @@ class DelojzaBot:
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)
@ -487,7 +485,7 @@ class DelojzaBot:
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 = None self.last_downloaded[update.message.chat.id] = None
return return
update.message.reply_text("Nothing to remove!") update.message.reply_text("Nothing to remove!")