From 56bfaa7bc5779bf709a3e70968cbc6a650ccc56a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Tue, 28 May 2019 12:27:08 +0200 Subject: [PATCH] add a handling function for #text samples (has issues) specifically, any message with links gets still handled by tg_handle_url instead. --- delojza.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/delojza.py b/delojza.py index a75d37c..6bc850b 100755 --- a/delojza.py +++ b/delojza.py @@ -262,12 +262,34 @@ class DelojzaBot: def tg_handle_hashtag(self, bot, update): hashtags = self.extract_hashtags(update.message) + self.handle_text(update.message.reply_to_message or update.message, hashtags) + if update.message.reply_to_message: 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, 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 def handle(self, urls, message, hashtags, download_fn, filetitle=None): try: