calibre-web/cps/static/js/get_meta.js

181 lines
5.9 KiB
JavaScript
Raw Normal View History

/*
* Get Metadata from Douban Books api and Google Books api
* Created by idalin<dalin.lin@gmail.com>
* 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)
2017-09-17 07:15:08 +02:00
*/
/* global _, i18nMsg, tinymce */
2017-04-21 20:44:17 +02:00
var dbResults = [];
var ggResults = [];
$(function () {
2017-03-30 21:17:18 +02:00
var msg = i18nMsg;
2017-03-29 21:43:55 +02:00
var douban = "https://api.douban.com";
2017-03-30 21:17:18 +02:00
var dbSearch = "/v2/book/search";
2017-04-02 10:05:07 +02:00
// var dbGetInfo = "/v2/book/";
// var db_get_info_by_isbn = "/v2/book/isbn/ ";
2017-03-30 21:17:18 +02:00
var dbDone = false;
2017-03-30 21:17:18 +02:00
var google = "https://www.googleapis.com/";
2017-04-02 10:05:07 +02:00
var ggSearch = "/books/v1/volumes";
// var gg_get_info = "/books/v1/volumes/";
var ggDone = false;
2017-04-02 10:05:07 +02:00
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);
}
2017-04-02 10:05:07 +02:00
function showResult () {
showFlag++;
if (showFlag === 1) {
$("#meta-info").html("<ul id=\"book-list\" class=\"media-list\"></ul>");
2017-04-02 10:05:07 +02:00
}
if (ggDone && dbDone) {
if (!ggResults && !dbResults) {
$("#meta-info").html("<p class=\"text-danger\">" + msg.no_result + "</p>");
2017-04-02 10:05:07 +02:00
return;
}
}
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);
});
2017-04-02 10:05:07 +02:00
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/"
}
};
var $book = $(templates.bookResult(book));
$book.find("img").on("click", function () {
populateForm(book);
});
$("#book-list").append($book);
});
2017-04-02 10:05:07 +02:00
dbDone = false;
}
}
function ggSearchBook (title) {
$.ajax({
url: google + ggSearch + "?q=" + title.replace(/\s+/gm, "+"),
type: "GET",
dataType: "jsonp",
2017-03-30 21:17:18 +02:00
jsonp: "callback",
success: function success(data) {
2017-04-02 10:05:07 +02:00
ggResults = data.items;
},
complete: function complete() {
2017-04-02 10:05:07 +02:00
ggDone = true;
showResult();
$("#show-google").trigger("change");
}
});
}
2017-04-02 10:05:07 +02:00
function dbSearchBook (title) {
2017-03-31 18:41:05 +02:00
$.ajax({
url: douban + dbSearch + "?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",
success: function success(data) {
2017-04-02 10:05:07 +02:00
dbResults = data.books;
2017-03-31 18:41:05 +02:00
},
error: function error() {
$("#meta-info").html("<p class=\"text-danger\">" + msg.search_error + "!</p>");
2017-03-31 18:41:05 +02:00
},
complete: function complete() {
2017-03-31 18:41:05 +02:00
dbDone = true;
2017-04-02 10:05:07 +02:00
showResult();
$("#show-douban").trigger("change");
2017-03-31 18:41:05 +02:00
}
});
}
2017-04-03 19:57:45 +02:00
function doSearch (keyword) {
showFlag = 0;
$("#meta-info").text(msg.loading);
2017-04-03 19:57:45 +02:00
// var keyword = $("#keyword").val();
if (keyword) {
dbSearchBook(keyword);
ggSearchBook(keyword);
}
}
$("#meta-search").on("submit", function (e) {
e.preventDefault();
2017-03-30 21:17:18 +02:00
var keyword = $("#keyword").val();
if (keyword) {
2017-04-02 10:05:07 +02:00
doSearch(keyword);
}
});
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-04-21 20:44:17 +02:00
});