deploy
campi 3 years ago
parent f921728e87
commit 00e016c968

@ -32,31 +32,32 @@ class Database:
return result_list return result_list
def select_books_by_title(self, query): def select_books_by_title(self, query):
query = self.__format_query(query) query = format_query(query)
sql = f""" sql = f"""
SELECT title, author_sort, path FROM books SELECT title, author_sort, path FROM books
WHERE title LIKE '{query}'; WHERE title LIKE "{query}";
""" """
return self._execute(sql, res.book_factory) return self._execute(sql, res.book_factory)
def select_books_by_author(self, query): def select_books_by_author(self, query):
query = self.__format_query(query) query = format_query(query)
sql = f""" sql = f"""
SELECT title, author_sort, path FROM books SELECT title, author_sort, path FROM books
WHERE books.author_sort LIKE '%' || WHERE books.author_sort LIKE '%' ||
(SELECT sort FROM authors (SELECT sort FROM authors
WHERE sort LIKE '%{query}%') || '%'; WHERE sort LIKE "{query}") || '%';
""" """
return self._execute(sql, res.book_factory) return self._execute(sql, res.book_factory)
def select_authors(self, query): def select_authors(self, query):
query = self.__format_query(query) query = format_query(query)
sql = f""" sql = f"""
SELECT name, sort FROM authors SELECT name, sort FROM authors
WHERE name LIKE '{query}'; WHERE name LIKE "{query}";
""" """
return self._execute(sql, res.author_factory) return self._execute(sql, res.author_factory)
def __format_query(self, query):
formatted_query = "%" + query.replace(" ", "%") + "%" def format_query(query):
return formatted_query formatted_query = "%" + query.replace(" ", "%") + "%"
return formatted_query

@ -2,6 +2,9 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import glob import glob
import logging import logging
import os.path
import urllib3
from telegram import InlineKeyboardButton, InlineKeyboardMarkup from telegram import InlineKeyboardButton, InlineKeyboardMarkup
from telegram import ParseMode from telegram import ParseMode
@ -128,22 +131,29 @@ def _get_book(update: Update, context: CallbackContext):
parse_mode=ParseMode.HTML parse_mode=ParseMode.HTML
) )
query.message.reply_text( query.message.reply_text(
'Ti invio immediatamente il file, sii paziente se il tuo libro non arriva') 'Stiamo caricando il file, sii paziente se il tuo libro non arriva subito')
filepath = CALIBRE_DIR + query.data + "/*.pdf" filepath = CALIBRE_DIR + query.data
if os.path.isdir(filepath + "/splitted"):
filepath = filepath + "/splitted"
try: try:
for file in glob.glob(filepath): for file in glob.glob(filepath + "/*.pdf"):
with open(file, 'rb') as doc_pdf: with open(file, 'rb') as doc:
context.bot.send_document(chat_id, doc_pdf) context.bot.send_document(chat_id, doc, timeout=120)
logging.info("Sharing {}".format(file)) 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.info("cannot send {}".format(filepath))
logging.error(e)
query.message.reply_text( 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 return ConversationHandler.END
def _cnv_stop(update: Update, context: CallbackContext): def _cnv_stop(update: Update, context: CallbackContext):
update.message.reply_text("ciao") # context.bot.edit_message_reply_markup()
return ConversationHandler.END return ConversationHandler.END
@ -178,9 +188,12 @@ def __format_reply_markup(list_of_res):
return InlineKeyboardMarkup(inline_keyboard) return InlineKeyboardMarkup(inline_keyboard)
# def handle_error(update: Update, context: CallbackContext): def handle_error(update: Update, context: CallbackContext):
# context.bot.send_message(chat_id=update.callback_query.message.chat_id, context.bot.send_message(
# text="Oh no, qualcosa è andato storto, riprova più tardi...") 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) 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), CommandHandler('search_by_author', search_by_author),
MessageHandler(Filters.text, search_by_title_default)], MessageHandler(Filters.text, search_by_title_default)],
states={ 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_RESOURCE: [MessageHandler(Filters.text, _get_resource)],
GET_AUTHOR: [CallbackQueryHandler(_get_author)], GET_AUTHOR: [CallbackQueryHandler(_get_author)],
GET_BOOK: [CallbackQueryHandler(_get_book)], GET_BOOK: [CallbackQueryHandler(_get_book)],
@ -209,7 +222,7 @@ def main():
updater.dispatcher.add_handler(cnv_handler) updater.dispatcher.add_handler(cnv_handler)
updater.dispatcher.add_handler(MessageHandler(Filters.command, unknown)) # Filters out unknown commands 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.start_polling()
updater.idle() updater.idle()

Loading…
Cancel
Save