From fbb78e43167e82807b81dafd6de382eac03da2c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Thu, 16 May 2019 15:16:41 +0200 Subject: [PATCH] add 1hr timeout to hashtags; forget last_hashtag in handle() --- delojza.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/delojza.py b/delojza.py index 5b6cd7c..dca4157 100755 --- a/delojza.py +++ b/delojza.py @@ -8,7 +8,7 @@ import shutil import sys import tempfile from configparser import ConfigParser -from datetime import datetime +from datetime import datetime, timedelta from glob import glob from operator import itemgetter @@ -234,10 +234,10 @@ class DelojzaBot: def get_hashtags(self, message): hashtags = self.extract_hashtags(message) - if len(hashtags) == 0: - if self.last_hashtags is not None and self.last_hashtags[0] == message.from_user: + if len(hashtags) == 0 and self.last_hashtags is not None: + user, ts, hashtags = self.last_hashtags + if user == message.from_user and ts > datetime.now() - timedelta(hours=1): hashtags = self.last_hashtags[1] - self.last_hashtags = None return hashtags def tg_handle_hashtag(self, bot, update): @@ -247,7 +247,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, hashtags) + self.last_hashtags = (update.message.from_user, datetime.now(), hashtags) # noinspection PyBroadException def handle(self, urls, message, hashtags, download_fn, filename=None): @@ -256,6 +256,8 @@ class DelojzaBot: self.logger.info("Ignoring %s due to no hashtag present..." % urls) return + self.last_hashtags = None + self.logger.info("Downloading %s under '%s'" % (urls, "/".join(hashtags))) out_path = os.path.join(self.out_dir, *hashtags)