You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
parallel-library/cps/static/js/get_meta.js

196 lines
6.6 KiB
JavaScript

/* 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/>.
*/
/*
* Get Metadata from Douban Books api and Google Books api
* 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)
*/
/* global _, i18nMsg, tinymce */
6 years ago
// var dbResults = [];
8 years ago
var ggResults = [];
$(function () {
8 years ago
var msg = i18nMsg;
/*var douban = "https://api.douban.com";
var dbSearch = "/v2/book/search";*/
6 years ago
// var dbDone = true;
6 years ago
var google = "https://www.googleapis.com";
8 years ago
var ggSearch = "/books/v1/volumes";
var ggDone = false;
8 years ago
var showFlag = 0;
var templates = {
bookResult: _.template(
$("#template-book-result").html()
)
};
function populateForm (book) {
tinymce.get("description").setContent(book.description);
$("#bookAuthor").val(book.authors);
$("#book_title").val(book.title);
$("#tags").val(book.tags.join(","));
$("#rating").data("rating").setValue(Math.round(book.rating));
$(".cover img").attr("src", book.cover);
$("#cover_url").val(book.cover);
}
8 years ago
function showResult () {
showFlag++;
if (showFlag === 1) {
$("#meta-info").html("<ul id=\"book-list\" class=\"media-list\"></ul>");
8 years ago
}
6 years ago
if (!ggDone) {
$("#meta-info").html("<p class=\"text-danger\">" + msg.no_result + "</p>");
return;
8 years ago
}
if (ggDone && ggResults.length > 0) {
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/"
}
};
var $book = $(templates.bookResult(book));
$book.find("img").on("click", function () {
populateForm(book);
});
$("#book-list").append($book);
});
8 years ago
ggDone = false;
}
/*if (dbDone && dbResults.length > 0) {
dbResults.forEach(function(result) {
var book = {
id: result.id,
title: result.title,
authors: result.author || [],
description: result.summary,
publisher: result.publisher || "",
publishedDate: result.pubdate || "",
tags: result.tags.map(function(tag) {
return tag.title;
}),
rating: result.rating.average || 0,
cover: result.image,
url: "https://book.douban.com/subject/" + result.id,
source: {
id: "douban",
description: "Douban Books",
url: "https://book.douban.com/"
}
};
if (book.rating > 0) {
book.rating /= 2;
}
var $book = $(templates.bookResult(book));
$book.find("img").on("click", function () {
populateForm(book);
});
$("#book-list").append($book);
});
8 years ago
dbDone = false;
}*/
8 years ago
}
function ggSearchBook (title) {
$.ajax({
url: google + ggSearch + "?q=" + title.replace(/\s+/gm, "+"),
type: "GET",
dataType: "jsonp",
8 years ago
jsonp: "callback",
success: function success(data) {
6 years ago
if ("items" in data) {
ggResults = data.items;
ggDone = true;
}
},
complete: function complete() {
8 years ago
showResult();
$("#show-google").trigger("change");
}
});
}
/*function dbSearchBook (title) {
8 years ago
$.ajax({
url: douban + dbSearch + "?q=" + title + "&fields=all&count=10",
8 years ago
type: "GET",
dataType: "jsonp",
8 years ago
jsonp: "callback",
success: function success(data) {
8 years ago
dbResults = data.books;
8 years ago
},
error: function error() {
$("#meta-info").html("<p class=\"text-danger\">" + msg.search_error + "!</p>"+ $("#meta-info")[0].innerHTML)
8 years ago
},
complete: function complete() {
8 years ago
dbDone = true;
8 years ago
showResult();
$("#show-douban").trigger("change");
8 years ago
}
});
}*/
8 years ago
function doSearch (keyword) {
showFlag = 0;
$("#meta-info").text(msg.loading);
if (keyword) {
// dbSearchBook(keyword);
ggSearchBook(keyword);
}
}
$("#meta-search").on("submit", function (e) {
e.preventDefault();
8 years ago
var keyword = $("#keyword").val();
if (keyword) {
8 years ago
doSearch(keyword);
}
});
8 years ago
$("#get_meta").click(function () {
8 years ago
var bookTitle = $("#book_title").val();
if (bookTitle) {
$("#keyword").val(bookTitle);
doSearch(bookTitle);
}
});
8 years ago
});