From 0c45a0a5da51f1cfd64895fe75c5baf955a9efe7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Wed, 12 Jun 2019 16:23:36 +0200 Subject: [PATCH] add retval to handle() fn, stop processing message on first success --- delojza.py | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/delojza.py b/delojza.py index 4f6a9bf..b21b414 100755 --- a/delojza.py +++ b/delojza.py @@ -294,7 +294,7 @@ class DelojzaBot: try: if len(hashtags) == 0: self.logger.info("Ignoring %s due to no hashtag present..." % urls) - return + return False if any(hashtag in self.protected_tags for hashtag in hashtags): if message.chat.title not in self.protected_chats: @@ -335,11 +335,12 @@ class DelojzaBot: message.reply_text('Something weird happened with the tumblrs, check it!') self.last_downloaded[message.chat.id] = filenames, hashtags, tumblr_ids - return filenames + return True except: exc_type, exc_value, __ = sys.exc_info() if "Timed out" not in str(exc_value): message.reply_text("Something is FUCKED: [{}] {}".format(exc_type, exc_value)) + return False def handle_tg_message(self, message, bot, hashtag): file, filetitle, tumblr = None, None, False @@ -361,24 +362,37 @@ class DelojzaBot: if file is not None: url = bot.getFile(file).file_path - self.handle([url], message, hashtag, self.download_raw, filetitle=filetitle) + return self.handle([url], message, hashtag, self.download_raw, filetitle=filetitle) + else: + return False def handle_urls(self, message, hashtags): urls = list(map(lambda e: message.parse_entity(e), filter(lambda e: e.type == 'url', message.entities))) + + ytdl_res = False ytdl_urls = [url for url in urls if self.ytdl_can(url)] - normal_urls = [url for url in urls if not self.ytdl_can(url)] if len(ytdl_urls) > 0: - self.handle(ytdl_urls, message, hashtags, self.download_ytdl) + ytdl_res = self.handle(ytdl_urls, message, hashtags, self.download_ytdl) + + raw_res = False + normal_urls = [url for url in urls if not self.ytdl_can(url)] if len(normal_urls) > 0: file_urls = [url for url in normal_urls if "text" not in requests.head(url).headers.get("Content-Type", "text")] if len(file_urls) > 0: - self.handle(file_urls, message, hashtags, self.download_raw) + raw_res = self.handle(file_urls, message, hashtags, self.download_raw) + + return ytdl_res or raw_res def tg_handle(self, bot, update): - self.handle_urls(update.message, self._get_hashtags(update.message)) - self.handle_tg_message(update.message, bot, self._get_hashtags(update.message)) + url_res = self.handle_urls(update.message, self._get_hashtags(update.message)) + if url_res: + return + + msg_res = self.handle_tg_message(update.message, bot, self._get_hashtags(update.message)) + if msg_res: + return hashtags = self.extract_hashtags(update.message) if len(hashtags) > 0: