add retval to handle() fn, stop processing message on first success
This commit is contained in:
parent
ae09a1d981
commit
0c45a0a5da
1 changed files with 22 additions and 8 deletions
30
delojza.py
30
delojza.py
|
@ -294,7 +294,7 @@ class DelojzaBot:
|
||||||
try:
|
try:
|
||||||
if len(hashtags) == 0:
|
if len(hashtags) == 0:
|
||||||
self.logger.info("Ignoring %s due to no hashtag present..." % urls)
|
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 any(hashtag in self.protected_tags for hashtag in hashtags):
|
||||||
if message.chat.title not in self.protected_chats:
|
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!')
|
message.reply_text('Something weird happened with the tumblrs, check it!')
|
||||||
|
|
||||||
self.last_downloaded[message.chat.id] = filenames, hashtags, tumblr_ids
|
self.last_downloaded[message.chat.id] = filenames, hashtags, tumblr_ids
|
||||||
return filenames
|
return True
|
||||||
except:
|
except:
|
||||||
exc_type, exc_value, __ = sys.exc_info()
|
exc_type, exc_value, __ = sys.exc_info()
|
||||||
if "Timed out" not in str(exc_value):
|
if "Timed out" not in str(exc_value):
|
||||||
message.reply_text("Something is FUCKED: [{}] {}".format(exc_type, exc_value))
|
message.reply_text("Something is FUCKED: [{}] {}".format(exc_type, exc_value))
|
||||||
|
return False
|
||||||
|
|
||||||
def handle_tg_message(self, message, bot, hashtag):
|
def handle_tg_message(self, message, bot, hashtag):
|
||||||
file, filetitle, tumblr = None, None, False
|
file, filetitle, tumblr = None, None, False
|
||||||
|
@ -361,24 +362,37 @@ class DelojzaBot:
|
||||||
|
|
||||||
if file is not None:
|
if file is not None:
|
||||||
url = bot.getFile(file).file_path
|
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):
|
def handle_urls(self, message, hashtags):
|
||||||
urls = list(map(lambda e: message.parse_entity(e),
|
urls = list(map(lambda e: message.parse_entity(e),
|
||||||
filter(lambda e: e.type == 'url', message.entities)))
|
filter(lambda e: e.type == 'url', message.entities)))
|
||||||
|
|
||||||
|
ytdl_res = False
|
||||||
ytdl_urls = [url for url in urls if self.ytdl_can(url)]
|
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:
|
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:
|
if len(normal_urls) > 0:
|
||||||
file_urls = [url for url in normal_urls if
|
file_urls = [url for url in normal_urls if
|
||||||
"text" not in requests.head(url).headers.get("Content-Type", "text")]
|
"text" not in requests.head(url).headers.get("Content-Type", "text")]
|
||||||
if len(file_urls) > 0:
|
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):
|
def tg_handle(self, bot, update):
|
||||||
self.handle_urls(update.message, self._get_hashtags(update.message))
|
url_res = self.handle_urls(update.message, self._get_hashtags(update.message))
|
||||||
self.handle_tg_message(update.message, bot, 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)
|
hashtags = self.extract_hashtags(update.message)
|
||||||
if len(hashtags) > 0:
|
if len(hashtags) > 0:
|
||||||
|
|
Loading…
Reference in a new issue