|
|
@ -1,6 +1,5 @@
|
|
|
|
#!/usr/bin/env python
|
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
|
|
from telegram.ext.updater import Updater
|
|
|
|
from telegram.ext.updater import Updater
|
|
|
|
from telegram.update import Update
|
|
|
|
from telegram.update import Update
|
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
|
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
|
|
|
@ -11,6 +10,7 @@ from telegram.ext.conversationhandler import ConversationHandler
|
|
|
|
from telegram.ext.callbackqueryhandler import CallbackQueryHandler
|
|
|
|
from telegram.ext.callbackqueryhandler import CallbackQueryHandler
|
|
|
|
from telegram.callbackquery import CallbackQuery
|
|
|
|
from telegram.callbackquery import CallbackQuery
|
|
|
|
from telegram.ext.filters import Filters
|
|
|
|
from telegram.ext.filters import Filters
|
|
|
|
|
|
|
|
from libreremo_conf import TOKEN, CALIBRE_DIR, MIN_QUERY_LENGTH
|
|
|
|
import glob
|
|
|
|
import glob
|
|
|
|
import database_lib as DB
|
|
|
|
import database_lib as DB
|
|
|
|
import logging
|
|
|
|
import logging
|
|
|
@ -18,14 +18,9 @@ import logging
|
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
level=logging.INFO)
|
|
|
|
level=logging.INFO)
|
|
|
|
|
|
|
|
|
|
|
|
MIN_QUERY_LENGTH = 3
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TOKEN="**********"
|
|
|
|
|
|
|
|
updater = Updater("**********************************************",
|
|
|
|
|
|
|
|
use_context=True, arbitrary_callback_data=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CALIBRE_LIBRARY="/home/archino/LIBREREMO/"
|
|
|
|
updater = Updater(TOKEN, use_context=True, arbitrary_callback_data=True)
|
|
|
|
db = DB.Database(CALIBRE_LIBRARY + "metadata.db")
|
|
|
|
db = DB.Database(CALIBRE_DIR + "metadata.db")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def start(update: Update, context: CallbackContext):
|
|
|
|
def start(update: Update, context: CallbackContext):
|
|
|
@ -107,7 +102,6 @@ def _get_resource(update, context):
|
|
|
|
update.message.reply_text(
|
|
|
|
update.message.reply_text(
|
|
|
|
text=text,
|
|
|
|
text=text,
|
|
|
|
reply_markup=reply_markup)
|
|
|
|
reply_markup=reply_markup)
|
|
|
|
|
|
|
|
|
|
|
|
return handler
|
|
|
|
return handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -143,10 +137,11 @@ def _get_book(update: Update, context: CallbackContext):
|
|
|
|
query.message.reply_text(
|
|
|
|
query.message.reply_text(
|
|
|
|
'perfetto ti invio immediatamente il file! :)')
|
|
|
|
'perfetto ti invio immediatamente il file! :)')
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
filepath = CALIBRE_LIBRARY + query.data + "/*.pdf"
|
|
|
|
filepath = CALIBRE_DIR + query.data + "/*.pdf"
|
|
|
|
for file in glob.glob(filepath):
|
|
|
|
for file in glob.glob(filepath):
|
|
|
|
with open(file, 'rb') as doc_pdf:
|
|
|
|
with open(file, 'rb') as doc_pdf:
|
|
|
|
context.bot.send_document(chat_id, doc_pdf)
|
|
|
|
context.bot.send_document(chat_id, doc_pdf)
|
|
|
|
|
|
|
|
logging.info("Sharing {}".format(file))
|
|
|
|
except FileNotFoundError:
|
|
|
|
except FileNotFoundError:
|
|
|
|
logging.info("cannot send {}".format(filepath))
|
|
|
|
logging.info("cannot send {}".format(filepath))
|
|
|
|
query.message.reply_text(
|
|
|
|
query.message.reply_text(
|
|
|
@ -164,13 +159,15 @@ def search_by_title(update: Update, context: CallbackContext):
|
|
|
|
context.user_data["resource"] = "titolo"
|
|
|
|
context.user_data["resource"] = "titolo"
|
|
|
|
return GET_RESOURCE
|
|
|
|
return GET_RESOURCE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def search_by_title_default(update: Update, context: CallbackContext):
|
|
|
|
|
|
|
|
context.user_data["resource"] = "titolo"
|
|
|
|
|
|
|
|
return GET_RESOURCE
|
|
|
|
|
|
|
|
|
|
|
|
def search_by_author(update: Update, context: CallbackContext):
|
|
|
|
def search_by_author(update: Update, context: CallbackContext):
|
|
|
|
update.message.reply_text("di quale autore stai cercando i libri?")
|
|
|
|
update.message.reply_text("di quale autore stai cercando i libri?")
|
|
|
|
context.user_data["resource"] = "autore"
|
|
|
|
context.user_data["resource"] = "autore"
|
|
|
|
return GET_RESOURCE
|
|
|
|
return GET_RESOURCE
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def search_by_class(update: Update, context: CallbackContext):
|
|
|
|
def search_by_class(update: Update, context: CallbackContext):
|
|
|
|
update.message.reply_text("di quale corso di studio stai cercando i libri?")
|
|
|
|
update.message.reply_text("di quale corso di studio stai cercando i libri?")
|
|
|
|
context.user_data["resource"] = "cds"
|
|
|
|
context.user_data["resource"] = "cds"
|
|
|
@ -182,10 +179,6 @@ def unknown(update: Update, context: CallbackContext):
|
|
|
|
"Sorry '%s' is not a valid command" % update.message.text)
|
|
|
|
"Sorry '%s' is not a valid command" % update.message.text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def unknown_text(update: Update, context: CallbackContext):
|
|
|
|
|
|
|
|
update.message.reply_text(
|
|
|
|
|
|
|
|
"Sorry I can't recognize you , you said '%s'" % update.message.text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __format_reply_markup(list_of_res):
|
|
|
|
def __format_reply_markup(list_of_res):
|
|
|
|
return InlineKeyboardMarkup([
|
|
|
|
return InlineKeyboardMarkup([
|
|
|
|
[InlineKeyboardButton(
|
|
|
|
[InlineKeyboardButton(
|
|
|
@ -193,13 +186,16 @@ def __format_reply_markup(list_of_res):
|
|
|
|
callback_data = res.format_data())]
|
|
|
|
callback_data = res.format_data())]
|
|
|
|
for res in list_of_res])
|
|
|
|
for res in list_of_res])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GET_RESOURCE_TYPE, ASK_RESOURCE, GET_RESOURCE, GET_BOOK, GET_AUTHOR, GET_CDS = range(6)
|
|
|
|
GET_RESOURCE_TYPE, ASK_RESOURCE, GET_RESOURCE, GET_BOOK, GET_AUTHOR, GET_CDS = range(6)
|
|
|
|
|
|
|
|
|
|
|
|
cnv_handler = ConversationHandler(
|
|
|
|
cnv_handler = ConversationHandler(
|
|
|
|
entry_points = [CommandHandler('search', _ask_resource_type),
|
|
|
|
entry_points = [CommandHandler('search', _ask_resource_type),
|
|
|
|
CommandHandler('search_by_title', search_by_title),
|
|
|
|
CommandHandler('search_by_title', search_by_title),
|
|
|
|
CommandHandler('search_by_author', search_by_author),
|
|
|
|
CommandHandler('search_by_author', search_by_author),
|
|
|
|
CommandHandler('search_by_class', search_by_class)],
|
|
|
|
CommandHandler('search_by_class', search_by_class),
|
|
|
|
|
|
|
|
MessageHandler(Filters.text, search_by_title_default)],
|
|
|
|
states = {
|
|
|
|
states = {
|
|
|
|
GET_RESOURCE_TYPE: [CallbackQueryHandler(_get_resource_type)],
|
|
|
|
GET_RESOURCE_TYPE: [CallbackQueryHandler(_get_resource_type)],
|
|
|
|
GET_RESOURCE: [MessageHandler(Filters.text, _get_resource)],
|
|
|
|
GET_RESOURCE: [MessageHandler(Filters.text, _get_resource)],
|
|
|
@ -215,13 +211,8 @@ cnv_handler = ConversationHandler(
|
|
|
|
updater.dispatcher.add_handler(cnv_handler)
|
|
|
|
updater.dispatcher.add_handler(cnv_handler)
|
|
|
|
updater.dispatcher.add_handler(CommandHandler('start', start))
|
|
|
|
updater.dispatcher.add_handler(CommandHandler('start', start))
|
|
|
|
updater.dispatcher.add_handler(CommandHandler('help', help))
|
|
|
|
updater.dispatcher.add_handler(CommandHandler('help', help))
|
|
|
|
#updater.dispatcher.add_handler(CommandHandler('search_by_author', search_by_author))
|
|
|
|
|
|
|
|
#updater.dispatcher.add_handler(CommandHandler('search_by_class', search_by_class))
|
|
|
|
|
|
|
|
updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown))
|
|
|
|
updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown))
|
|
|
|
updater.dispatcher.add_handler(MessageHandler(Filters.command, unknown)) # Filters out unknown commands
|
|
|
|
updater.dispatcher.add_handler(MessageHandler(Filters.command, unknown)) # Filters out unknown commands
|
|
|
|
|
|
|
|
|
|
|
|
# Filters out unknown messages.
|
|
|
|
|
|
|
|
updater.dispatcher.add_handler(MessageHandler(Filters.text, unknown_text))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updater.start_polling()
|
|
|
|
updater.start_polling()
|
|
|
|
updater.idle()
|
|
|
|
updater.idle()
|
|
|
|