2019-01-20 19:37:45 +01:00
|
|
|
/* This file is part of the Calibre-Web (https://github.com/janeczku/calibre-web)
|
|
|
|
* Copyright (C) 2018 idalin<dalin.lin@gmail.com>
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2017-02-28 07:52:55 +01:00
|
|
|
/*
|
2020-02-28 16:04:07 +01:00
|
|
|
* Get Metadata from Douban Books api and Google Books api and ComicVine
|
2017-02-28 07:52:55 +01:00
|
|
|
* Google Books api document: https://developers.google.com/books/docs/v1/using
|
|
|
|
* Douban Books api document: https://developers.douban.com/wiki/?title=book_v2 (Chinese Only)
|
2020-02-28 16:04:07 +01:00
|
|
|
* ComicVine api document: https://comicvine.gamespot.com/api/documentation
|
2017-09-17 07:15:08 +02:00
|
|
|
*/
|
2020-02-02 07:47:27 +01:00
|
|
|
/* global _, i18nMsg, tinymce */
|
2019-11-21 17:12:05 +01:00
|
|
|
var dbResults = [];
|
2017-04-21 20:44:17 +02:00
|
|
|
var ggResults = [];
|
2020-02-28 16:04:07 +01:00
|
|
|
var cvResults = [];
|
2017-02-28 07:52:55 +01:00
|
|
|
|
2017-08-13 07:24:56 +02:00
|
|
|
$(function () {
|
2017-03-30 21:17:18 +02:00
|
|
|
var msg = i18nMsg;
|
2019-11-21 17:12:05 +01:00
|
|
|
var douban = "https://api.douban.com";
|
|
|
|
var dbSearch = "/v2/book/search";
|
2020-04-04 08:53:22 +02:00
|
|
|
var dbDone = 0;
|
2017-02-28 07:52:55 +01:00
|
|
|
|
2019-06-10 16:38:29 +02:00
|
|
|
var google = "https://www.googleapis.com";
|
2017-04-02 10:05:07 +02:00
|
|
|
var ggSearch = "/books/v1/volumes";
|
2020-04-04 08:53:22 +02:00
|
|
|
var ggDone = 0;
|
2017-02-28 07:52:55 +01:00
|
|
|
|
2020-02-28 16:04:07 +01:00
|
|
|
var comicvine = "https://comicvine.gamespot.com";
|
|
|
|
var cvSearch = "/api/search/";
|
2020-04-04 08:53:22 +02:00
|
|
|
var cvDone = 0;
|
2020-02-28 16:04:07 +01:00
|
|
|
|
2017-04-02 10:05:07 +02:00
|
|
|
var showFlag = 0;
|
2017-02-28 07:52:55 +01:00
|
|
|
|
2017-08-16 06:31:15 +02:00
|
|
|
var templates = {
|
|
|
|
bookResult: _.template(
|
|
|
|
$("#template-book-result").html()
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
function populateForm (book) {
|
|
|
|
tinymce.get("description").setContent(book.description);
|
2019-11-21 17:12:05 +01:00
|
|
|
var uniqueTags = [];
|
|
|
|
$.each(book.tags, function(i, el) {
|
|
|
|
if ($.inArray(el, uniqueTags) === -1) uniqueTags.push(el);
|
|
|
|
});
|
|
|
|
|
2020-02-21 22:25:02 +01:00
|
|
|
var ampSeparatedAuthors = (book.authors || []).join(" & ");
|
|
|
|
$("#bookAuthor").val(ampSeparatedAuthors);
|
2017-08-16 06:31:15 +02:00
|
|
|
$("#book_title").val(book.title);
|
2019-11-21 17:12:05 +01:00
|
|
|
$("#tags").val(uniqueTags.join(","));
|
2017-08-16 06:31:15 +02:00
|
|
|
$("#rating").data("rating").setValue(Math.round(book.rating));
|
|
|
|
$(".cover img").attr("src", book.cover);
|
|
|
|
$("#cover_url").val(book.cover);
|
2019-11-21 17:12:05 +01:00
|
|
|
$("#pubdate").val(book.publishedDate);
|
2020-02-02 07:47:27 +01:00
|
|
|
$("#publisher").val(book.publisher);
|
|
|
|
if (typeof book.series !== "undefined") {
|
|
|
|
$("#series").val(book.series);
|
2019-11-21 17:12:05 +01:00
|
|
|
}
|
2017-08-16 06:31:15 +02:00
|
|
|
}
|
|
|
|
|
2017-04-02 10:05:07 +02:00
|
|
|
function showResult () {
|
|
|
|
showFlag++;
|
|
|
|
if (showFlag === 1) {
|
2017-08-13 07:24:56 +02:00
|
|
|
$("#meta-info").html("<ul id=\"book-list\" class=\"media-list\"></ul>");
|
2017-04-02 10:05:07 +02:00
|
|
|
}
|
2020-04-24 21:05:56 +02:00
|
|
|
if ((ggDone === 3 || (ggDone === 1 && ggResults.length === 0)) &&
|
|
|
|
(dbDone === 3 || (ggDone === 1 && dbResults.length === 0)) &&
|
|
|
|
(cvDone === 3 || (ggDone === 1 && cvResults.length === 0))) {
|
2019-06-10 16:38:29 +02:00
|
|
|
$("#meta-info").html("<p class=\"text-danger\">" + msg.no_result + "</p>");
|
|
|
|
return;
|
2017-04-02 10:05:07 +02:00
|
|
|
}
|
2019-11-21 17:12:05 +01:00
|
|
|
function formatDate (date) {
|
|
|
|
var d = new Date(date),
|
2020-02-02 07:47:27 +01:00
|
|
|
month = "" + (d.getMonth() + 1),
|
|
|
|
day = "" + d.getDate(),
|
2019-11-21 17:12:05 +01:00
|
|
|
year = d.getFullYear();
|
2020-02-02 07:47:27 +01:00
|
|
|
|
|
|
|
if (month.length < 2) {
|
|
|
|
month = "0" + month;
|
|
|
|
}
|
|
|
|
if (day.length < 2) {
|
|
|
|
day = "0" + day;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [year, month, day].join("-");
|
2019-11-21 17:12:05 +01:00
|
|
|
}
|
|
|
|
|
2020-04-04 08:53:22 +02:00
|
|
|
if (ggResults.length > 0) {
|
|
|
|
if (ggDone < 2) {
|
|
|
|
ggResults.forEach(function(result) {
|
|
|
|
var book = {
|
|
|
|
id: result.id,
|
|
|
|
title: result.volumeInfo.title,
|
|
|
|
authors: result.volumeInfo.authors || [],
|
|
|
|
description: result.volumeInfo.description || "",
|
|
|
|
publisher: result.volumeInfo.publisher || "",
|
|
|
|
publishedDate: result.volumeInfo.publishedDate || "",
|
|
|
|
tags: result.volumeInfo.categories || [],
|
|
|
|
rating: result.volumeInfo.averageRating || 0,
|
|
|
|
cover: result.volumeInfo.imageLinks ?
|
|
|
|
result.volumeInfo.imageLinks.thumbnail : "/static/generic_cover.jpg",
|
|
|
|
url: "https://books.google.com/books?id=" + result.id,
|
|
|
|
source: {
|
|
|
|
id: "google",
|
|
|
|
description: "Google Books",
|
|
|
|
url: "https://books.google.com/"
|
|
|
|
}
|
|
|
|
};
|
2017-08-16 06:31:15 +02:00
|
|
|
|
2020-04-04 08:53:22 +02:00
|
|
|
var $book = $(templates.bookResult(book));
|
|
|
|
$book.find("img").on("click", function () {
|
|
|
|
populateForm(book);
|
|
|
|
});
|
2017-08-16 06:31:15 +02:00
|
|
|
|
2020-04-04 08:53:22 +02:00
|
|
|
$("#book-list").append($book);
|
|
|
|
});
|
|
|
|
ggDone = 2;
|
|
|
|
} else {
|
|
|
|
ggDone = 3;
|
|
|
|
}
|
2017-04-02 10:05:07 +02:00
|
|
|
}
|
2020-02-02 07:47:27 +01:00
|
|
|
|
2020-04-04 08:53:22 +02:00
|
|
|
if (dbResults.length > 0) {
|
|
|
|
if (dbDone < 2) {
|
|
|
|
dbResults.forEach(function(result) {
|
|
|
|
var seriesTitle = "";
|
|
|
|
if (result.series) {
|
|
|
|
seriesTitle = result.series.title;
|
|
|
|
}
|
|
|
|
var dateFomers = result.pubdate.split("-");
|
|
|
|
var publishedYear = parseInt(dateFomers[0]);
|
|
|
|
var publishedMonth = parseInt(dateFomers[1]);
|
|
|
|
var publishedDate = new Date(publishedYear, publishedMonth - 1, 1);
|
|
|
|
|
|
|
|
publishedDate = formatDate(publishedDate);
|
|
|
|
|
|
|
|
var book = {
|
|
|
|
id: result.id,
|
|
|
|
title: result.title,
|
|
|
|
authors: result.author || [],
|
|
|
|
description: result.summary,
|
|
|
|
publisher: result.publisher || "",
|
|
|
|
publishedDate: publishedDate || "",
|
|
|
|
tags: result.tags.map(function(tag) {
|
|
|
|
return tag.title.toLowerCase().replace(/,/g, "_");
|
|
|
|
}),
|
|
|
|
rating: result.rating.average || 0,
|
|
|
|
series: seriesTitle || "",
|
|
|
|
cover: result.image,
|
|
|
|
url: "https://book.douban.com/subject/" + result.id,
|
|
|
|
source: {
|
|
|
|
id: "douban",
|
|
|
|
description: "Douban Books",
|
|
|
|
url: "https://book.douban.com/"
|
|
|
|
}
|
|
|
|
};
|
2019-11-21 17:12:05 +01:00
|
|
|
|
2020-04-04 08:53:22 +02:00
|
|
|
if (book.rating > 0) {
|
|
|
|
book.rating /= 2;
|
2017-08-16 06:31:15 +02:00
|
|
|
}
|
|
|
|
|
2020-04-04 08:53:22 +02:00
|
|
|
var $book = $(templates.bookResult(book));
|
|
|
|
$book.find("img").on("click", function () {
|
|
|
|
populateForm(book);
|
|
|
|
});
|
2017-09-11 18:58:39 +02:00
|
|
|
|
2020-04-04 08:53:22 +02:00
|
|
|
$("#book-list").append($book);
|
2017-08-16 06:31:15 +02:00
|
|
|
});
|
2020-04-04 08:53:22 +02:00
|
|
|
dbDone = 2;
|
|
|
|
} else {
|
|
|
|
dbDone = 3;
|
|
|
|
}
|
2019-11-21 17:12:05 +01:00
|
|
|
}
|
2020-04-04 08:53:22 +02:00
|
|
|
if (cvResults.length > 0) {
|
|
|
|
if (cvDone < 2) {
|
|
|
|
cvResults.forEach(function(result) {
|
|
|
|
var seriesTitle = "";
|
|
|
|
if (result.volume.name) {
|
|
|
|
seriesTitle = result.volume.name;
|
|
|
|
}
|
|
|
|
var dateFomers = "";
|
|
|
|
if (result.store_date) {
|
|
|
|
dateFomers = result.store_date.split("-");
|
2020-04-24 21:05:56 +02:00
|
|
|
} else {
|
2020-04-04 08:53:22 +02:00
|
|
|
dateFomers = result.date_added.split("-");
|
2020-02-28 16:04:07 +01:00
|
|
|
}
|
2020-04-04 08:53:22 +02:00
|
|
|
var publishedYear = parseInt(dateFomers[0]);
|
|
|
|
var publishedMonth = parseInt(dateFomers[1]);
|
|
|
|
var publishedDate = new Date(publishedYear, publishedMonth - 1, 1);
|
2020-02-28 16:04:07 +01:00
|
|
|
|
2020-04-04 08:53:22 +02:00
|
|
|
publishedDate = formatDate(publishedDate);
|
|
|
|
|
|
|
|
var book = {
|
|
|
|
id: result.id,
|
2020-04-24 21:05:56 +02:00
|
|
|
title: seriesTitle + " #" + ("00" + result.issue_number).slice(-3) + " - " + result.name,
|
2020-04-04 08:53:22 +02:00
|
|
|
authors: result.author || [],
|
|
|
|
description: result.description,
|
|
|
|
publisher: "",
|
|
|
|
publishedDate: publishedDate || "",
|
2020-04-24 21:05:56 +02:00
|
|
|
tags: ["Comics", seriesTitle],
|
2020-04-04 08:53:22 +02:00
|
|
|
rating: 0,
|
|
|
|
series: seriesTitle || "",
|
|
|
|
cover: result.image.original_url,
|
|
|
|
url: result.site_detail_url,
|
|
|
|
source: {
|
|
|
|
id: "comicvine",
|
|
|
|
description: "ComicVine Books",
|
|
|
|
url: "https://comicvine.gamespot.com/"
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
var $book = $(templates.bookResult(book));
|
|
|
|
$book.find("img").on("click", function () {
|
|
|
|
populateForm(book);
|
|
|
|
});
|
2020-02-28 16:04:07 +01:00
|
|
|
|
2020-04-04 08:53:22 +02:00
|
|
|
$("#book-list").append($book);
|
|
|
|
});
|
|
|
|
cvDone = 2;
|
|
|
|
} else {
|
|
|
|
cvDone = 3;
|
|
|
|
}
|
2020-02-28 16:04:07 +01:00
|
|
|
}
|
2017-04-02 10:05:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function ggSearchBook (title) {
|
2017-02-28 07:52:55 +01:00
|
|
|
$.ajax({
|
2017-08-13 07:24:56 +02:00
|
|
|
url: google + ggSearch + "?q=" + title.replace(/\s+/gm, "+"),
|
2017-02-28 08:17:37 +01:00
|
|
|
type: "GET",
|
|
|
|
dataType: "jsonp",
|
2017-03-30 21:17:18 +02:00
|
|
|
jsonp: "callback",
|
2017-08-13 07:24:56 +02:00
|
|
|
success: function success(data) {
|
2019-06-10 16:38:29 +02:00
|
|
|
if ("items" in data) {
|
|
|
|
ggResults = data.items;
|
|
|
|
}
|
2017-02-28 07:52:55 +01:00
|
|
|
},
|
2017-08-13 07:24:56 +02:00
|
|
|
complete: function complete() {
|
2020-04-04 08:53:22 +02:00
|
|
|
ggDone = 1;
|
2017-04-02 10:05:07 +02:00
|
|
|
showResult();
|
2017-08-16 06:31:15 +02:00
|
|
|
$("#show-google").trigger("change");
|
2017-02-28 07:52:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-21 17:12:05 +01:00
|
|
|
function dbSearchBook (title) {
|
2020-02-02 07:47:27 +01:00
|
|
|
var apikey = "0df993c66c0c636e29ecbb5344252a4a";
|
2017-03-31 18:41:05 +02:00
|
|
|
$.ajax({
|
2019-11-21 17:12:05 +01:00
|
|
|
url: douban + dbSearch + "?apikey=" + apikey + "&q=" + title + "&fields=all&count=10",
|
2017-03-31 18:41:05 +02:00
|
|
|
type: "GET",
|
|
|
|
dataType: "jsonp",
|
2017-04-02 10:05:07 +02:00
|
|
|
jsonp: "callback",
|
2017-08-13 07:24:56 +02:00
|
|
|
success: function success(data) {
|
2017-04-02 10:05:07 +02:00
|
|
|
dbResults = data.books;
|
2017-03-31 18:41:05 +02:00
|
|
|
},
|
2017-08-13 07:24:56 +02:00
|
|
|
error: function error() {
|
2020-02-02 07:47:27 +01:00
|
|
|
$("#meta-info").html("<p class=\"text-danger\">" + msg.search_error + "!</p>" + $("#meta-info")[0].innerHTML);
|
2017-03-31 18:41:05 +02:00
|
|
|
},
|
2017-08-13 07:24:56 +02:00
|
|
|
complete: function complete() {
|
2020-04-04 08:53:22 +02:00
|
|
|
dbDone = 1;
|
2017-04-02 10:05:07 +02:00
|
|
|
showResult();
|
2017-08-16 06:31:15 +02:00
|
|
|
$("#show-douban").trigger("change");
|
2017-03-31 18:41:05 +02:00
|
|
|
}
|
|
|
|
});
|
2019-11-21 17:12:05 +01:00
|
|
|
}
|
2017-03-31 18:41:05 +02:00
|
|
|
|
2020-02-28 16:04:07 +01:00
|
|
|
function cvSearchBook (title) {
|
|
|
|
var apikey = "57558043c53943d5d1e96a9ad425b0eb85532ee6";
|
|
|
|
title = encodeURIComponent(title);
|
|
|
|
$.ajax({
|
|
|
|
url: comicvine + cvSearch + "?api_key=" + apikey + "&resources=issue&query=" + title + "&sort=name:desc&format=jsonp",
|
|
|
|
type: "GET",
|
|
|
|
dataType: "jsonp",
|
|
|
|
jsonp: "json_callback",
|
|
|
|
success: function success(data) {
|
|
|
|
cvResults = data.results;
|
|
|
|
},
|
|
|
|
error: function error() {
|
|
|
|
$("#meta-info").html("<p class=\"text-danger\">" + msg.search_error + "!</p>" + $("#meta-info")[0].innerHTML);
|
|
|
|
},
|
|
|
|
complete: function complete() {
|
2020-04-04 08:53:22 +02:00
|
|
|
cvDone = 1;
|
2020-02-28 16:04:07 +01:00
|
|
|
showResult();
|
|
|
|
$("#show-comics").trigger("change");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-04-03 19:57:45 +02:00
|
|
|
function doSearch (keyword) {
|
|
|
|
showFlag = 0;
|
2020-04-04 08:53:22 +02:00
|
|
|
dbDone = ggDone = cvDone = 0;
|
|
|
|
dbResults = [];
|
|
|
|
ggResults = [];
|
|
|
|
cvResults = [];
|
2017-08-13 07:24:56 +02:00
|
|
|
$("#meta-info").text(msg.loading);
|
2017-04-03 19:57:45 +02:00
|
|
|
if (keyword) {
|
2019-11-21 17:12:05 +01:00
|
|
|
dbSearchBook(keyword);
|
2017-04-03 19:57:45 +02:00
|
|
|
ggSearchBook(keyword);
|
2020-02-28 16:04:07 +01:00
|
|
|
cvSearchBook(keyword);
|
2017-04-03 19:57:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-16 06:31:15 +02:00
|
|
|
$("#meta-search").on("submit", function (e) {
|
|
|
|
e.preventDefault();
|
2017-03-30 21:17:18 +02:00
|
|
|
var keyword = $("#keyword").val();
|
2017-02-28 07:52:55 +01:00
|
|
|
if (keyword) {
|
2017-04-02 10:05:07 +02:00
|
|
|
doSearch(keyword);
|
2017-02-28 07:52:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-03-30 21:17:18 +02:00
|
|
|
$("#get_meta").click(function () {
|
2017-04-02 10:05:07 +02:00
|
|
|
var bookTitle = $("#book_title").val();
|
|
|
|
if (bookTitle) {
|
|
|
|
$("#keyword").val(bookTitle);
|
|
|
|
doSearch(bookTitle);
|
2017-02-28 07:52:55 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-21 20:44:17 +02:00
|
|
|
});
|