remember tags and last downloaded per each chat
This commit is contained in:
parent
194b59f89c
commit
4c585496a9
1 changed files with 42 additions and 44 deletions
28
delojza.py
28
delojza.py
|
@ -75,8 +75,8 @@ class DelojzaBot:
|
|||
self.protected_chats = protected_chats or []
|
||||
self.protected_tags = protected_tags or []
|
||||
|
||||
self.last_downloaded = None
|
||||
self.last_hashtags = None
|
||||
self.last_downloaded = {}
|
||||
self.last_hashtags = {}
|
||||
|
||||
@staticmethod
|
||||
def ytdl_can(url):
|
||||
|
@ -252,9 +252,9 @@ class DelojzaBot:
|
|||
|
||||
def _get_hashtags(self, message):
|
||||
hashtags = self.extract_hashtags(message)
|
||||
if len(hashtags) == 0 and self.last_hashtags is not None:
|
||||
user, chat, ts, last_hashtags = self.last_hashtags
|
||||
if user == message.from_user and chat == message.chat and ts > datetime.now() - timedelta(hours=1):
|
||||
if len(hashtags) == 0 and self.last_hashtags.get(message.chat.id) is not None:
|
||||
user, ts, last_hashtags = self.last_hashtags[message.chat.id]
|
||||
if user == message.from_user and ts > datetime.now() - timedelta(hours=1):
|
||||
hashtags = last_hashtags
|
||||
return hashtags
|
||||
|
||||
|
@ -291,7 +291,7 @@ class DelojzaBot:
|
|||
.format(urls, message.chat.title, hashtags))
|
||||
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)))
|
||||
|
||||
|
@ -323,7 +323,7 @@ class DelojzaBot:
|
|||
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!')
|
||||
|
||||
self.last_downloaded = message.chat, filenames, hashtags, tumblr_ids
|
||||
self.last_downloaded[message.chat.id] = filenames, hashtags, tumblr_ids
|
||||
return filenames
|
||||
except:
|
||||
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_urls(update.message.reply_to_message, hashtags)
|
||||
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:
|
||||
if self.markov:
|
||||
self.markov.add_to_corpus(update.message.text)
|
||||
|
@ -450,9 +450,8 @@ class DelojzaBot:
|
|||
|
||||
def tg_retag(self, _, update):
|
||||
message_is_sensible = update.message.text.count("-") == 1
|
||||
if self.last_downloaded is not None and message_is_sensible:
|
||||
chat, files, hashtags, tumblr_ids = self.last_downloaded
|
||||
if chat == update.message.chat:
|
||||
if self.last_downloaded.get(update.message.chat.id) is not None and message_is_sensible:
|
||||
files, hashtags, tumblr_ids = self.last_downloaded[update.message.chat.id]
|
||||
mp3s = [filename for filename in files if filename.endswith("mp3")]
|
||||
if len(mp3s) > 0:
|
||||
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 "") + "???")
|
||||
|
||||
def tg_delete(self, _, update):
|
||||
if self.last_downloaded is not None:
|
||||
chat, files, hashtags, tumblr_ids = self.last_downloaded
|
||||
if chat == update.message.chat:
|
||||
if self.last_downloaded.get(update.message.chat.id) is not None:
|
||||
files, hashtags, tumblr_ids = self.last_downloaded[update.message.chat.id]
|
||||
for file in files:
|
||||
update.message.reply_text("Removing \"{}\"!".format(file[len(self.out_dir) + 1:]))
|
||||
os.remove(file)
|
||||
|
@ -487,7 +485,7 @@ class DelojzaBot:
|
|||
for tumblr_id in tumblr_ids:
|
||||
if self.tumblr_client:
|
||||
self.tumblr_client.delete_post(self.tumblr_name, tumblr_id)
|
||||
self.last_downloaded = None
|
||||
self.last_downloaded[update.message.chat.id] = None
|
||||
return
|
||||
update.message.reply_text("Nothing to remove!")
|
||||
|
||||
|
|
Loading…
Reference in a new issue