add a handling function for #text samples (has issues)

specifically, any message with links gets still handled by tg_handle_url
instead.
This commit is contained in:
Tomáš Mládek 2019-05-28 12:27:08 +02:00
parent d782ce8c48
commit 56bfaa7bc5

View file

@ -262,12 +262,34 @@ class DelojzaBot:
def tg_handle_hashtag(self, bot, update): def tg_handle_hashtag(self, bot, update):
hashtags = self.extract_hashtags(update.message) hashtags = self.extract_hashtags(update.message)
self.handle_text(update.message.reply_to_message or update.message, hashtags)
if update.message.reply_to_message: if update.message.reply_to_message:
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, update.message.chat, datetime.now(), hashtags self.last_hashtags = update.message.from_user, update.message.chat, datetime.now(), hashtags
def handle_text(self, message, hashtags):
if len(hashtags) == 0 or hashtags[0] not in ('TEXT', 'TXT'):
return
info_line = self.sanitize("-".join(re.sub(r'#[\w]+', '', message.text).strip().split()[:7]))
if len(info_line) > 64:
info_line = info_line[:64]
filename = '{}__{}.txt'.format(datestr(message.date), info_line)
out_path = os.path.join(self.out_dir, *hashtags[1:] or ['TEXT'])
file_path = os.path.join(out_path, filename)
mkdir_p(out_path)
with open(file_path, 'w') as out_file:
out_file.write(message.text)
message.reply_text("Saved text to \"{}\"...".format(os.path.join(*hashtags[1:], filename)))
# noinspection PyBroadException # noinspection PyBroadException
def handle(self, urls, message, hashtags, download_fn, filetitle=None): def handle(self, urls, message, hashtags, download_fn, filetitle=None):
try: try: