From 00e016c96858ddd74ed73b566017d37d2f7782eb Mon Sep 17 00:00:00 2001 From: campi Date: Thu, 17 Mar 2022 23:09:08 +0100 Subject: [PATCH] bug --- database_lib.py | 19 ++++++++++--------- libreremo_bot.py | 39 ++++++++++++++++++++++++++------------- 2 files changed, 36 insertions(+), 22 deletions(-) diff --git a/database_lib.py b/database_lib.py index 080a856..c7cbb81 100644 --- a/database_lib.py +++ b/database_lib.py @@ -32,31 +32,32 @@ class Database: return result_list def select_books_by_title(self, query): - query = self.__format_query(query) + query = format_query(query) sql = f""" SELECT title, author_sort, path FROM books - WHERE title LIKE '{query}'; + WHERE title LIKE "{query}"; """ return self._execute(sql, res.book_factory) def select_books_by_author(self, query): - query = self.__format_query(query) + query = format_query(query) sql = f""" SELECT title, author_sort, path FROM books WHERE books.author_sort LIKE '%' || (SELECT sort FROM authors - WHERE sort LIKE '%{query}%') || '%'; + WHERE sort LIKE "{query}") || '%'; """ return self._execute(sql, res.book_factory) def select_authors(self, query): - query = self.__format_query(query) + query = format_query(query) sql = f""" SELECT name, sort FROM authors - WHERE name LIKE '{query}'; + WHERE name LIKE "{query}"; """ return self._execute(sql, res.author_factory) - def __format_query(self, query): - formatted_query = "%" + query.replace(" ", "%") + "%" - return formatted_query + +def format_query(query): + formatted_query = "%" + query.replace(" ", "%") + "%" + return formatted_query diff --git a/libreremo_bot.py b/libreremo_bot.py index 95ae5c7..2429502 100644 --- a/libreremo_bot.py +++ b/libreremo_bot.py @@ -2,6 +2,9 @@ # -*- coding: utf-8 -*- import glob import logging +import os.path + +import urllib3 from telegram import InlineKeyboardButton, InlineKeyboardMarkup from telegram import ParseMode @@ -128,22 +131,29 @@ def _get_book(update: Update, context: CallbackContext): parse_mode=ParseMode.HTML ) query.message.reply_text( - 'Ti invio immediatamente il file, sii paziente se il tuo libro non arriva') - filepath = CALIBRE_DIR + query.data + "/*.pdf" + 'Stiamo caricando il file, sii paziente se il tuo libro non arriva subito') + filepath = CALIBRE_DIR + query.data + if os.path.isdir(filepath + "/splitted"): + filepath = filepath + "/splitted" try: - for file in glob.glob(filepath): - with open(file, 'rb') as doc_pdf: - context.bot.send_document(chat_id, doc_pdf) + for file in glob.glob(filepath + "/*.pdf"): + with open(file, 'rb') as doc: + context.bot.send_document(chat_id, doc, timeout=120) logging.info("Sharing {}".format(file)) - except FileNotFoundError: + except urllib3.exceptions.HTTPError as e: # cannot handle exception + logging.error(e) + query.message.reply_text( + "Abbiamo qualche problema di connessione, ritenta più tardi") + except Exception as e: logging.info("cannot send {}".format(filepath)) + logging.error(e) query.message.reply_text( - "Mi dispiace non rieco a trovare il file, prova piu' tardi") + "Non riesco a mandarti il file, riprova più tardi") return ConversationHandler.END def _cnv_stop(update: Update, context: CallbackContext): - update.message.reply_text("ciao") + # context.bot.edit_message_reply_markup() return ConversationHandler.END @@ -178,9 +188,12 @@ def __format_reply_markup(list_of_res): return InlineKeyboardMarkup(inline_keyboard) -# def handle_error(update: Update, context: CallbackContext): -# context.bot.send_message(chat_id=update.callback_query.message.chat_id, -# text="Oh no, qualcosa è andato storto, riprova più tardi...") +def handle_error(update: Update, context: CallbackContext): + context.bot.send_message( + chat_id=update.callback_query.message.chat_id, + text="Oh no, qualcosa è andato storto, riprova più tardi...") + logging.error(context.error) + return ConversationHandler.END GET_RESOURCE_TYPE, ASK_RESOURCE, GET_RESOURCE, GET_BOOK, GET_AUTHOR = range(5) @@ -191,7 +204,7 @@ cnv_handler = ConversationHandler( CommandHandler('search_by_author', search_by_author), MessageHandler(Filters.text, search_by_title_default)], states={ - GET_RESOURCE_TYPE: [CallbackQueryHandler(_get_resource_type)], + GET_RESOURCE_TYPE: [CallbackQueryHandler(_get_resource_type), MessageHandler(Filters.text, _cnv_stop)], GET_RESOURCE: [MessageHandler(Filters.text, _get_resource)], GET_AUTHOR: [CallbackQueryHandler(_get_author)], GET_BOOK: [CallbackQueryHandler(_get_book)], @@ -209,7 +222,7 @@ def main(): updater.dispatcher.add_handler(cnv_handler) updater.dispatcher.add_handler(MessageHandler(Filters.command, unknown)) # Filters out unknown commands - # updater.dispatcher.add_error_handler(handle_error) + updater.dispatcher.add_error_handler(handle_error) updater.start_polling() updater.idle()