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
86
delojza.py
86
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,45 +450,43 @@ 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:
|
||||
mp3s = [filename for filename in files if filename.endswith("mp3")]
|
||||
if len(mp3s) > 0:
|
||||
tagline = re.sub(r'^/[\w]+', '', update.message.text).split("-")
|
||||
artist = tagline[0].strip()
|
||||
title = tagline[1].strip()
|
||||
for mp3 in mp3s:
|
||||
self._tag_file(mp3, artist, title)
|
||||
update.message.reply_text("Tagging \"{}\" as \"{}\" by \"{}\"!"
|
||||
.format(mp3[len(self.out_dir) + 1:], title, artist))
|
||||
return
|
||||
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("-")
|
||||
artist = tagline[0].strip()
|
||||
title = tagline[1].strip()
|
||||
for mp3 in mp3s:
|
||||
self._tag_file(mp3, artist, title)
|
||||
update.message.reply_text("Tagging \"{}\" as \"{}\" by \"{}\"!"
|
||||
.format(mp3[len(self.out_dir) + 1:], title, artist))
|
||||
return
|
||||
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:
|
||||
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:
|
||||
if len(os.listdir(parent_dir)) == 0:
|
||||
update.message.reply_text("Removing directory \"{}\" as it's empty..."
|
||||
.format(parent_dir[len(self.out_dir) + 1:]))
|
||||
os.rmdir(parent_dir)
|
||||
parent_dir = os.path.dirname(parent_dir)
|
||||
if parent_dir == self.out_dir:
|
||||
break
|
||||
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
|
||||
return
|
||||
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)
|
||||
parent_dir = os.path.dirname(file)
|
||||
while True:
|
||||
if len(os.listdir(parent_dir)) == 0:
|
||||
update.message.reply_text("Removing directory \"{}\" as it's empty..."
|
||||
.format(parent_dir[len(self.out_dir) + 1:]))
|
||||
os.rmdir(parent_dir)
|
||||
parent_dir = os.path.dirname(parent_dir)
|
||||
if parent_dir == self.out_dir:
|
||||
break
|
||||
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[update.message.chat.id] = None
|
||||
return
|
||||
update.message.reply_text("Nothing to remove!")
|
||||
|
||||
# noinspection PyMethodMayBeStatic
|
||||
|
|
Loading…
Reference in a new issue