From d5e9cdc5b7422f6499468d01f3f336e209454d10 Mon Sep 17 00:00:00 2001 From: Ozzie Isaacs Date: Wed, 7 Jul 2021 21:10:38 +0200 Subject: [PATCH] Search Metadata improvements --- cps.py | 2 + cps/metadata_provider/comicvine.py | 28 ++++++++++- cps/metadata_provider/google.py | 23 ++++++++- cps/metadata_provider/scholar.py | 41 +++++++++++----- cps/search_metadata.py | 24 ++++++---- cps/static/js/get_meta.js | 77 ++++++++++++++++++++---------- cps/templates/book_edit.html | 2 +- 7 files changed, 147 insertions(+), 50 deletions(-) diff --git a/cps.py b/cps.py index 737b0d97..f4012293 100755 --- a/cps.py +++ b/cps.py @@ -42,6 +42,7 @@ from cps.admin import admi from cps.gdrive import gdrive from cps.editbooks import editbook from cps.remotelogin import remotelogin +from cps.search_metadata import meta from cps.error_handler import init_errorhandler try: @@ -70,6 +71,7 @@ def main(): app.register_blueprint(shelf) app.register_blueprint(admi) app.register_blueprint(remotelogin) + app.register_blueprint(meta) # if config.config_use_google_drive: app.register_blueprint(gdrive) app.register_blueprint(editbook) diff --git a/cps/metadata_provider/comicvine.py b/cps/metadata_provider/comicvine.py index 0bc67488..42b949f3 100644 --- a/cps/metadata_provider/comicvine.py +++ b/cps/metadata_provider/comicvine.py @@ -26,12 +26,38 @@ class ComicVine(Metadata): __name__ = "ComicVine" def search(self, query): + val = list() if self.active: headers = { 'User-Agent': 'Not Evil Browser' # , } + result = requests.get("https://comicvine.gamespot.com/api/search?api_key=" + apikey + "&resources=issue&query=" + query + "&sort=name:desc&format=json", headers=headers) - return [result.json()['results']] + for r in result.json()['results']: + seriesTitle = r['volume'].get('name', "") + if r.get('store_date'): + dateFomers = r.get('store_date') + else: + dateFomers = r.get('date_added') + v = dict() + v['id'] = r['id'] + v['title'] = seriesTitle + " #" + r.get('issue_number', "0") + " - " + ( r.get('name', "") or "") + v['authors'] = r.get('authors', []) + v['description'] = r.get('description', "") + v['publisher'] = "" + v['publishedDate'] = dateFomers + v['tags'] = ["Comics", seriesTitle] + v['rating'] = 0 + v['series'] = seriesTitle + v['cover'] = r['image'].get('original_url') + v['source'] = { + "id": "comicvine", + "description": "ComicVine Books", + "link": "https://comicvine.gamespot.com/" + } + v['url'] = "" + val.append(v) + return val diff --git a/cps/metadata_provider/google.py b/cps/metadata_provider/google.py index d22b1f4d..d540f96f 100644 --- a/cps/metadata_provider/google.py +++ b/cps/metadata_provider/google.py @@ -25,7 +25,28 @@ class Google(Metadata): def search(self, query): if self.active: + val = list() result = requests.get("https://www.googleapis.com/books/v1/volumes?q="+query.replace(" ","+")) - return [result.json()['items']] + for r in result.json()['items']: + v = dict() + v['id'] = r['id'] + v['title'] = r['volumeInfo']['title'] + v['authors'] = r['volumeInfo'].get('authors', []) + v['description'] = r['volumeInfo'].get('description', "") + v['publisher'] = r['volumeInfo'].get('publisher', "") + v['publishedDate'] = r['volumeInfo'].get('publishedDate', "") + v['tags'] = r['volumeInfo'].get('categories', []) + v['rating'] = r['volumeInfo'].get('averageRating', 0) + if r['volumeInfo'].get('imageLinks'): + v['cover'] = r['volumeInfo']['imageLinks']['thumbnail'] + else: + v['cover'] = "/../../../static/generic_cover.jpg" + v['source'] = { + "id": "google", + "description": "Google Books", + "link": "https://books.google.com/"} + v['url'] = "" + val.append(v) + return val diff --git a/cps/metadata_provider/scholar.py b/cps/metadata_provider/scholar.py index 3314ba62..02c0c4f8 100644 --- a/cps/metadata_provider/scholar.py +++ b/cps/metadata_provider/scholar.py @@ -32,19 +32,36 @@ class scholar(Metadata): __name__ = "ComicVine" def search(self, query): + val = list() if self.active: - if True: - scholar_gen = scholarly.search_pubs(' '.join(query.split('+'))) - i = 0 - result = [] - for publication in scholar_gen: - del publication['source'] - result.append(publication) - i += 1 - if (i >= 10): - break - return json.dumps(result) - return "[]" + scholar_gen = scholarly.search_pubs(' '.join(query.split('+'))) + i = 0 + for publication in scholar_gen: + v = dict() + v['id'] = "1234" # publication['bib'].get('title') + v['title'] = publication['bib'].get('title') + v['authors'] = publication['bib'].get('author', []) + v['description'] = publication['bib'].get('abstract', "") + v['publisher'] = publication['bib'].get('venue', "") + if publication['bib'].get('pub_year'): + v['publishedDate'] = publication['bib'].get('pub_year')+"-01-01" + else: + v['publishedDate'] = "" + v['tags'] = "" + v['ratings'] = 0 + v['series'] = "" + v['cover'] = "/../../../static/generic_cover.jpg" + v['url'] = "" + v['source'] = { + "id": "googlescholar", + "description": "Google Scholar", + "link": "https://scholar.google.com/" + } + val.append(v) + i += 1 + if (i >= 10): + break + return val diff --git a/cps/search_metadata.py b/cps/search_metadata.py index 655e63b5..8ccf8203 100644 --- a/cps/search_metadata.py +++ b/cps/search_metadata.py @@ -19,8 +19,9 @@ from __future__ import division, print_function, unicode_literals from cps.services.Metadata import Metadata import os +import json -from flask import Blueprint +from flask import Blueprint, request, Response from flask_login import login_required from . import constants, logger @@ -28,7 +29,7 @@ from os.path import basename, isfile import importlib import sys, inspect -opds = Blueprint('metadata', __name__) +meta = Blueprint('metadata', __name__) log = logger.create() @@ -54,20 +55,25 @@ def list_classes(provider_list): return classes cl = list_classes(new_list) -for c in cl: - print(c.search("Walking")) +#for c in cl: +# print(c.search("Walking")) -@opds.route("/metadata/provider") +@meta.route("/metadata/provider") @login_required def metadata_provider(): return "" -@opds.route("/metadata/search") +@meta.route("/metadata/search", methods=['POST']) @login_required def metadata_search(): - return "" + query = request.form.to_dict().get('query') + data = list() + if query: + for c in cl: + data.extend(c.search(query)) + return Response(json.dumps(data), mimetype='application/json') -@opds.route("/metadata/replace/") +@meta.route("/metadata/replace/") @login_required -def metadata_provider(id): +def metadata_replace(id): return "" diff --git a/cps/static/js/get_meta.js b/cps/static/js/get_meta.js index 19f8e929..6d396ea9 100644 --- a/cps/static/js/get_meta.js +++ b/cps/static/js/get_meta.js @@ -20,7 +20,7 @@ * Douban Books api document: https://developers.douban.com/wiki/?title=book_v2 (Chinese Only) * ComicVine api document: https://comicvine.gamespot.com/api/documentation */ -/* global _, i18nMsg, tinymce */ +/* global _, i18nMsg, tinymce, getPatch */ var dbResults = []; var ggResults = []; var cvResults = []; @@ -80,7 +80,7 @@ $(function () { if (showFlag === 1) { $("#meta-info").html("
    "); } - if ((ggDone === 3 || (ggDone === 1 && ggResults.length === 0)) && + /*if ((ggDone === 3 || (ggDone === 1 && ggResults.length === 0)) && (dbDone === 3 || (dbDone === 1 && dbResults.length === 0)) && (cvDone === 3 || (cvDone === 1 && cvResults.length === 0)) && (gsDone === 3 || (gsDone === 1 && gsResults.length === 0))) { @@ -102,6 +102,7 @@ $(function () { return [year, month, day].join("-"); } + function generateID (title) { return title.split("").reduce(function(a,b){a=((a<<5)-a)+b.charCodeAt(0);return a&a},0).toString().substr(0,12); } @@ -118,8 +119,7 @@ $(function () { publishedDate: result.volumeInfo.publishedDate || "", tags: result.volumeInfo.categories || [], rating: result.volumeInfo.averageRating || 0, - cover: result.volumeInfo.imageLinks ? - result.volumeInfo.imageLinks.thumbnail : location + "/../../../static/generic_cover.jpg", + cover: result.volumeInfo.imageLinks ? result.volumeInfo.imageLinks.thumbnail : location + "/../../../static/generic_cover.jpg", url: "https://books.google.com/books?id=" + result.id, source: { id: "google", @@ -277,10 +277,10 @@ $(function () { } else { cvDone = 3; } - } + }*/ } - function ggSearchBook (title) { + /*function ggSearchBook (title) { $.ajax({ url: google + ggSearch + "?q=" + title.replace(/\s+/gm, "+"), type: "GET", @@ -355,11 +355,38 @@ $(function () { showResult(); $("#show-googlescholar").trigger("change"); } - }) - } + }); + }*/ - function doSearch (keyword) { - showFlag = 0; + function doSearch (keyword) { + if (keyword) { + $("#meta-info").text(msg.loading); + $.ajax({ + url: getPath() + "/metadata/search", + type: "POST", + data: {"query": keyword}, + dataType: "json", + success: function success(data) { + console.log(data); + data.forEach(function(book) { + var $book = $(templates.bookResult(book)); + $book.find("img").on("click", function () { + populateForm(book); + }); + $("#book-list").append($book); + }); + }, + error: function error() { + $("#meta-info").html("

    " + msg.search_error + "!

    " + $("#meta-info")[0].innerHTML); + }, + complete: function complete() { + showResult(); + // $("#show-douban").trigger("change"); + } + }); + } + } + /*showFlag = 0; dbDone = ggDone = cvDone = 0; dbResults = []; ggResults = []; @@ -371,23 +398,21 @@ $(function () { ggSearchBook(keyword); cvSearchBook(keyword); gsSearchBook(keyword); - } - } + }*/ - $("#meta-search").on("submit", function (e) { - e.preventDefault(); - var keyword = $("#keyword").val(); - if (keyword) { - doSearch(keyword); - } - }); - $("#get_meta").click(function () { - var bookTitle = $("#book_title").val(); - if (bookTitle) { - $("#keyword").val(bookTitle); - doSearch(bookTitle); - } - }); + $("#meta-search").on("submit", function (e) { + e.preventDefault(); + var keyword = $("#keyword").val(); + doSearch(keyword); + }); + + $("#get_meta").click(function () { + var bookTitle = $("#book_title").val(); + if (bookTitle) { + $("#keyword").val(bookTitle); + doSearch(bookTitle); + } + }); }); diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index b932f56d..63bcd6a3 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -259,7 +259,7 @@
    {{_("Loading...")}}
    -
      +