respect chat for last_hashtag and /delete command

This commit is contained in:
Tomáš Mládek 2019-05-16 15:33:38 +02:00
parent fbb78e4316
commit a9d5967996

View file

@ -235,8 +235,8 @@ 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 is not None:
user, ts, hashtags = self.last_hashtags user, chat, ts, hashtags = self.last_hashtags
if user == message.from_user and ts > datetime.now() - timedelta(hours=1): if user == message.from_user and chat == message.chat and ts > datetime.now() - timedelta(hours=1):
hashtags = self.last_hashtags[1] hashtags = self.last_hashtags[1]
return hashtags return hashtags
@ -247,7 +247,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, datetime.now(), hashtags) self.last_hashtags = update.message.from_user, update.message.chat, datetime.now(), hashtags
# noinspection PyBroadException # noinspection PyBroadException
def handle(self, urls, message, hashtags, download_fn, filename=None): def handle(self, urls, message, hashtags, download_fn, filename=None):
@ -284,7 +284,7 @@ class DelojzaBot:
for filename in filenames: for filename in filenames:
response = self.tumblr_client.create_photo(self.tumblr_name, state="published", data=filename) response = self.tumblr_client.create_photo(self.tumblr_name, state="published", data=filename)
tumblr_ids.append(response['id']) tumblr_ids.append(response['id'])
self.last_downloaded = filenames, hashtags, tumblr_ids self.last_downloaded = message.chat, filenames, hashtags, tumblr_ids
return filenames return filenames
except: except:
exc_type, exc_value, __ = sys.exc_info() exc_type, exc_value, __ = sys.exc_info()
@ -406,28 +406,30 @@ class DelojzaBot:
def tg_delete(self, _, update): def tg_delete(self, _, update):
if self.last_downloaded is not None: if self.last_downloaded is not None:
files, hashtags, tumblr_ids = self.last_downloaded chat, files, hashtags, tumblr_ids = self.last_downloaded
for file in files: if chat == update.message.chat:
update.message.reply_text("Removing \"{}\"!".format(file[len(self.out_dir) + 1:])) for file in files:
os.remove(file) update.message.reply_text("Removing \"{}\"!".format(file[len(self.out_dir) + 1:]))
parent_dir = os.path.dirname(file) os.remove(file)
while True: parent_dir = os.path.dirname(file)
if len(os.listdir(parent_dir)) == 0: while True:
update.message.reply_text("Removing directory \"{}\" as it's empty..." if len(os.listdir(parent_dir)) == 0:
.format(parent_dir[len(self.out_dir) + 1:])) update.message.reply_text("Removing directory \"{}\" as it's empty..."
os.rmdir(parent_dir) .format(parent_dir[len(self.out_dir) + 1:]))
parent_dir = os.path.dirname(parent_dir) os.rmdir(parent_dir)
if parent_dir == self.out_dir: parent_dir = os.path.dirname(parent_dir)
break if parent_dir == self.out_dir:
if len(tumblr_ids) > 0: break
plural = "s (all {} of them)".format(len(tumblr_ids)) if len(tumblr_ids) > 1 else "" if len(tumblr_ids) > 0:
update.message.reply_text("Also deleting tumblr post{}!".format(plural)) plural = "s (all {} of them)".format(len(tumblr_ids)) if len(tumblr_ids) > 1 else ""
for tumblr_id in tumblr_ids: update.message.reply_text("Also deleting tumblr post{}!".format(plural))
if self.tumblr_client: for tumblr_id in tumblr_ids:
self.tumblr_client.delete_post(self.tumblr_name, tumblr_id) if self.tumblr_client:
self.last_downloaded = None self.tumblr_client.delete_post(self.tumblr_name, tumblr_id)
else: self.last_downloaded = None
update.message.reply_text("Nothing to remove!") return
update.message.reply_text("Nothing to remove!")
# noinspection PyMethodMayBeStatic # noinspection PyMethodMayBeStatic
def tg_version(self, _, update): def tg_version(self, _, update):