1
0
mirror of https://github.com/janeczku/calibre-web synced 2024-12-20 23:20:32 +00:00
calibre-web/cps/search_metadata.py

118 lines
4.1 KiB
Python
Raw Normal View History

2021-07-05 16:55:54 +00: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/>.
import os
2021-07-07 19:10:38 +00:00
import json
2021-07-07 19:24:29 +00:00
import importlib
import sys
import inspect
2021-08-29 12:36:05 +00:00
import datetime
import concurrent.futures
2021-07-05 16:55:54 +00:00
2021-08-29 12:36:05 +00:00
from flask import Blueprint, request, Response, url_for
2021-07-08 17:14:38 +00:00
from flask_login import current_user
2021-07-05 16:55:54 +00:00
from flask_login import login_required
from sqlalchemy.orm.attributes import flag_modified
from sqlalchemy.exc import OperationalError, InvalidRequestError
2021-07-05 16:55:54 +00:00
from . import constants, logger, ub
2021-07-07 19:24:29 +00:00
from cps.services.Metadata import Metadata
2021-07-05 16:55:54 +00:00
2021-08-29 12:36:05 +00:00
2021-07-07 19:10:38 +00:00
meta = Blueprint('metadata', __name__)
2021-07-05 16:55:54 +00:00
log = logger.create()
2021-07-06 18:24:27 +00:00
new_list = list()
2021-07-05 16:55:54 +00:00
meta_dir = os.path.join(constants.BASE_DIR, "cps", "metadata_provider")
2021-08-18 19:38:20 +00:00
modules = os.listdir(os.path.join(constants.BASE_DIR, "cps", "metadata_provider"))
2021-07-06 18:24:27 +00:00
for f in modules:
2021-07-07 19:24:29 +00:00
if os.path.isfile(os.path.join(meta_dir, f)) and not f.endswith('__init__.py'):
a = os.path.basename(f)[:-3]
2021-07-06 18:24:27 +00:00
try:
importlib.import_module("cps.metadata_provider." + a)
new_list.append(a)
except ImportError:
log.error("Import error for metadata source: {}".format(a))
pass
2021-07-05 16:55:54 +00:00
2021-07-06 18:24:27 +00:00
def list_classes(provider_list):
classes = list()
for element in provider_list:
for name, obj in inspect.getmembers(sys.modules["cps.metadata_provider." + element]):
if inspect.isclass(obj) and name != "Metadata" and issubclass(obj, Metadata):
classes.append(obj())
return classes
2021-07-05 16:55:54 +00:00
2021-07-06 18:24:27 +00:00
cl = list_classes(new_list)
2021-07-05 16:55:54 +00:00
2021-07-07 19:10:38 +00:00
@meta.route("/metadata/provider")
2021-07-05 16:55:54 +00:00
@login_required
def metadata_provider():
2021-07-08 17:14:38 +00:00
active = current_user.view_settings.get('metadata', {})
provider = list()
for c in cl:
2021-08-18 19:38:20 +00:00
ac = active.get(c.__id__, True)
provider.append({"name": c.__name__, "active": ac, "initial": ac, "id": c.__id__})
2021-07-08 17:14:38 +00:00
return Response(json.dumps(provider), mimetype='application/json')
@meta.route("/metadata/provider", methods=['POST'])
2021-08-18 19:38:20 +00:00
@meta.route("/metadata/provider/<prov_name>", methods=['POST'])
2021-07-08 17:14:38 +00:00
@login_required
2021-08-18 19:38:20 +00:00
def metadata_change_active_provider(prov_name):
new_state = request.get_json()
2021-07-08 17:14:38 +00:00
active = current_user.view_settings.get('metadata', {})
active[new_state['id']] = new_state['value']
current_user.view_settings['metadata'] = active
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 19:38:20 +00:00
if "initial" in new_state and prov_name:
for c in cl:
if c.__id__ == prov_name:
data = c.search(new_state.get('query', ""))
break
return Response(json.dumps(data), mimetype='application/json')
return ""
2021-07-05 16:55:54 +00:00
2021-07-07 19:10:38 +00:00
@meta.route("/metadata/search", methods=['POST'])
2021-07-05 16:55:54 +00:00
@login_required
def metadata_search():
2021-07-07 19:10:38 +00:00
query = request.form.to_dict().get('query')
data = list()
2021-07-08 17:14:38 +00:00
active = current_user.view_settings.get('metadata', {})
2021-07-07 19:10:38 +00:00
if query:
2021-08-29 12:36:05 +00:00
static_cover = url_for('static', filename='generic_cover.jpg')
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
meta = {executor.submit(c.search, query, static_cover): c for c in cl if active.get(c.__id__, True)}
for future in concurrent.futures.as_completed(meta):
data.extend(future.result())
2021-07-07 19:10:38 +00:00
return Response(json.dumps(data), mimetype='application/json')
2021-08-29 12:36:05 +00:00