2021-07-05 18:55:54 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
# Copyright (C) 2021 OzzieIsaacs
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2021-12-13 02:14:53 +01:00
|
|
|
import concurrent.futures
|
2021-07-07 21:24:29 +02:00
|
|
|
import importlib
|
|
|
|
import inspect
|
2021-12-13 02:14:53 +01:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
2022-01-27 18:37:02 +01:00
|
|
|
# from time import time
|
2022-03-24 18:19:41 +01:00
|
|
|
|
2021-07-05 18:55:54 +02:00
|
|
|
|
2021-12-13 02:14:53 +01:00
|
|
|
from flask import Blueprint, Response, request, url_for
|
2021-07-08 19:14:38 +02:00
|
|
|
from flask_login import current_user
|
2021-07-05 18:55:54 +02:00
|
|
|
from flask_login import login_required
|
2021-12-13 02:14:53 +01:00
|
|
|
from sqlalchemy.exc import InvalidRequestError, OperationalError
|
2021-08-01 13:50:17 +02:00
|
|
|
from sqlalchemy.orm.attributes import flag_modified
|
2021-07-05 18:55:54 +02:00
|
|
|
|
2021-07-07 21:24:29 +02:00
|
|
|
from cps.services.Metadata import Metadata
|
2022-03-24 18:19:41 +01:00
|
|
|
from . import constants, get_locale, logger, ub, web_server
|
2021-07-05 18:55:54 +02:00
|
|
|
|
2022-01-27 18:37:02 +01:00
|
|
|
# current_milli_time = lambda: int(round(time() * 1000))
|
2021-08-29 14:36:05 +02:00
|
|
|
|
2021-12-13 02:14:53 +01:00
|
|
|
meta = Blueprint("metadata", __name__)
|
2021-07-05 18:55:54 +02:00
|
|
|
|
|
|
|
log = logger.create()
|
|
|
|
|
2022-03-24 18:19:41 +01:00
|
|
|
try:
|
|
|
|
from dataclasses import asdict
|
|
|
|
except ImportError:
|
|
|
|
log.info('*** "dataclasses" is needed for calibre-web to run. Please install it using pip: "pip install dataclasses" ***')
|
|
|
|
print('*** "dataclasses" is needed for calibre-web to run. Please install it using pip: "pip install dataclasses" ***')
|
|
|
|
web_server.stop(True)
|
|
|
|
sys.exit(6)
|
|
|
|
|
2021-07-06 20:24:27 +02:00
|
|
|
new_list = list()
|
2021-07-05 18:55:54 +02:00
|
|
|
meta_dir = os.path.join(constants.BASE_DIR, "cps", "metadata_provider")
|
2021-08-18 21:38:20 +02:00
|
|
|
modules = os.listdir(os.path.join(constants.BASE_DIR, "cps", "metadata_provider"))
|
2021-07-06 20:24:27 +02:00
|
|
|
for f in modules:
|
2021-12-13 02:14:53 +01:00
|
|
|
if os.path.isfile(os.path.join(meta_dir, f)) and not f.endswith("__init__.py"):
|
2021-07-07 21:24:29 +02:00
|
|
|
a = os.path.basename(f)[:-3]
|
2021-07-06 20:24:27 +02:00
|
|
|
try:
|
|
|
|
importlib.import_module("cps.metadata_provider." + a)
|
|
|
|
new_list.append(a)
|
2022-01-29 21:02:56 +01:00
|
|
|
except ImportError as e:
|
|
|
|
log.error("Import error for metadata source: {} - {}".format(a, e))
|
2021-07-06 20:24:27 +02:00
|
|
|
pass
|
2021-07-05 18:55:54 +02:00
|
|
|
|
2021-12-13 02:14:53 +01:00
|
|
|
|
2021-07-06 20:24:27 +02:00
|
|
|
def list_classes(provider_list):
|
|
|
|
classes = list()
|
|
|
|
for element in provider_list:
|
2021-12-13 02:14:53 +01:00
|
|
|
for name, obj in inspect.getmembers(
|
|
|
|
sys.modules["cps.metadata_provider." + element]
|
|
|
|
):
|
|
|
|
if (
|
|
|
|
inspect.isclass(obj)
|
|
|
|
and name != "Metadata"
|
|
|
|
and issubclass(obj, Metadata)
|
|
|
|
):
|
2021-07-06 20:24:27 +02:00
|
|
|
classes.append(obj())
|
|
|
|
return classes
|
2021-07-05 18:55:54 +02:00
|
|
|
|
2021-12-13 02:14:53 +01:00
|
|
|
|
2021-07-06 20:24:27 +02:00
|
|
|
cl = list_classes(new_list)
|
2021-07-05 18:55:54 +02:00
|
|
|
|
2021-12-13 02:14:53 +01:00
|
|
|
|
2021-07-07 21:10:38 +02:00
|
|
|
@meta.route("/metadata/provider")
|
2021-07-05 18:55:54 +02:00
|
|
|
@login_required
|
|
|
|
def metadata_provider():
|
2021-12-13 02:14:53 +01:00
|
|
|
active = current_user.view_settings.get("metadata", {})
|
2021-07-08 19:14:38 +02:00
|
|
|
provider = list()
|
|
|
|
for c in cl:
|
2021-08-18 21:38:20 +02:00
|
|
|
ac = active.get(c.__id__, True)
|
2021-12-13 02:14:53 +01:00
|
|
|
provider.append(
|
|
|
|
{"name": c.__name__, "active": ac, "initial": ac, "id": c.__id__}
|
|
|
|
)
|
|
|
|
return Response(json.dumps(provider), mimetype="application/json")
|
2021-07-08 19:14:38 +02:00
|
|
|
|
|
|
|
|
2021-12-13 02:14:53 +01:00
|
|
|
@meta.route("/metadata/provider", methods=["POST"])
|
|
|
|
@meta.route("/metadata/provider/<prov_name>", methods=["POST"])
|
2021-07-08 19:14:38 +02:00
|
|
|
@login_required
|
2021-08-18 21:38:20 +02:00
|
|
|
def metadata_change_active_provider(prov_name):
|
2021-08-01 13:50:17 +02:00
|
|
|
new_state = request.get_json()
|
2021-12-13 02:14:53 +01:00
|
|
|
active = current_user.view_settings.get("metadata", {})
|
|
|
|
active[new_state["id"]] = new_state["value"]
|
|
|
|
current_user.view_settings["metadata"] = active
|
2021-08-01 13:50:17 +02:00
|
|
|
try:
|
|
|
|
try:
|
|
|
|
flag_modified(current_user, "view_settings")
|
|
|
|
except AttributeError:
|
|
|
|
pass
|
|
|
|
ub.session.commit()
|
|
|
|
except (InvalidRequestError, OperationalError):
|
|
|
|
log.error("Invalid request received: {}".format(request))
|
|
|
|
return "Invalid request", 400
|
2021-08-18 21:38:20 +02:00
|
|
|
if "initial" in new_state and prov_name:
|
2021-12-13 17:21:41 +01:00
|
|
|
data = []
|
|
|
|
provider = next((c for c in cl if c.__id__ == prov_name), None)
|
|
|
|
if provider is not None:
|
|
|
|
data = provider.search(new_state.get("query", ""))
|
|
|
|
return Response(
|
|
|
|
json.dumps([asdict(x) for x in data]), mimetype="application/json"
|
|
|
|
)
|
2021-08-18 21:38:20 +02:00
|
|
|
return ""
|
2021-07-05 18:55:54 +02:00
|
|
|
|
2021-12-13 02:14:53 +01:00
|
|
|
|
|
|
|
@meta.route("/metadata/search", methods=["POST"])
|
2021-07-05 18:55:54 +02:00
|
|
|
@login_required
|
|
|
|
def metadata_search():
|
2021-12-13 02:14:53 +01:00
|
|
|
query = request.form.to_dict().get("query")
|
2021-07-07 21:10:38 +02:00
|
|
|
data = list()
|
2021-12-13 02:14:53 +01:00
|
|
|
active = current_user.view_settings.get("metadata", {})
|
2021-12-13 15:14:19 +01:00
|
|
|
locale = get_locale()
|
2021-07-07 21:10:38 +02:00
|
|
|
if query:
|
2021-12-13 02:14:53 +01:00
|
|
|
static_cover = url_for("static", filename="generic_cover.jpg")
|
2022-01-27 18:37:02 +01:00
|
|
|
# start = current_milli_time()
|
2021-08-29 14:36:05 +02:00
|
|
|
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
|
2021-12-13 02:14:53 +01:00
|
|
|
meta = {
|
2021-12-13 15:14:19 +01:00
|
|
|
executor.submit(c.search, query, static_cover, locale): c
|
2021-12-13 02:14:53 +01:00
|
|
|
for c in cl
|
|
|
|
if active.get(c.__id__, True)
|
|
|
|
}
|
2021-08-29 14:36:05 +02:00
|
|
|
for future in concurrent.futures.as_completed(meta):
|
2021-12-13 17:21:41 +01:00
|
|
|
data.extend([asdict(x) for x in future.result()])
|
2022-01-27 18:37:02 +01:00
|
|
|
# log.info({'Time elapsed {}'.format(current_milli_time()-start)})
|
2021-12-13 02:14:53 +01:00
|
|
|
return Response(json.dumps(data), mimetype="application/json")
|