2017-02-27 22:52:55 -08:00
|
|
|
|
/*
|
|
|
|
|
* 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-04-03 10:57:45 -07:00
|
|
|
|
/* global i18nMsg */
|
2017-04-21 11:44:17 -07:00
|
|
|
|
var dbResults = [];
|
|
|
|
|
var ggResults = [];
|
2017-02-27 22:52:55 -08:00
|
|
|
|
|
|
|
|
|
$(document).ready(function () {
|
2017-03-30 12:17:18 -07:00
|
|
|
|
var msg = i18nMsg;
|
2017-03-29 12:43:55 -07:00
|
|
|
|
var douban = "https://api.douban.com";
|
2017-03-30 12:17:18 -07:00
|
|
|
|
var dbSearch = "/v2/book/search";
|
2017-04-02 01:05:07 -07:00
|
|
|
|
// var dbGetInfo = "/v2/book/";
|
|
|
|
|
// var db_get_info_by_isbn = "/v2/book/isbn/ ";
|
2017-03-30 12:17:18 -07:00
|
|
|
|
var dbDone = false;
|
2017-02-27 22:52:55 -08:00
|
|
|
|
|
2017-03-30 12:17:18 -07:00
|
|
|
|
var google = "https://www.googleapis.com/";
|
2017-04-02 01:05:07 -07:00
|
|
|
|
var ggSearch = "/books/v1/volumes";
|
|
|
|
|
// var gg_get_info = "/books/v1/volumes/";
|
|
|
|
|
var ggDone = false;
|
2017-02-27 22:52:55 -08:00
|
|
|
|
|
2017-04-02 01:05:07 -07:00
|
|
|
|
var showFlag = 0;
|
|
|
|
|
String.prototype.replaceAll = function (s1, s2) {
|
2017-03-30 12:17:18 -07:00
|
|
|
|
return this.replace(new RegExp(s1, "gm"), s2);
|
2017-03-29 12:43:55 -07:00
|
|
|
|
};
|
2017-02-27 22:52:55 -08:00
|
|
|
|
|
2017-04-02 01:05:07 -07:00
|
|
|
|
function showResult () {
|
2017-04-03 10:57:45 -07:00
|
|
|
|
var book;
|
|
|
|
|
var i;
|
|
|
|
|
var bookHtml;
|
2017-04-02 01:05:07 -07:00
|
|
|
|
showFlag++;
|
|
|
|
|
if (showFlag === 1) {
|
2017-04-21 11:44:17 -07:00
|
|
|
|
$("#metaModal #meta-info").html("<ul id=\"book-list\" class=\"media-list\"></ul>");
|
2017-04-02 01:05:07 -07:00
|
|
|
|
}
|
|
|
|
|
if (ggDone && dbDone) {
|
|
|
|
|
if (!ggResults && !dbResults) {
|
2017-04-21 11:44:17 -07:00
|
|
|
|
$("#metaModal #meta-info").html("<p class=\"text-danger\">"+ msg.no_result +"</p>");
|
2017-04-02 01:05:07 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (ggDone && ggResults.length > 0) {
|
2017-04-03 10:57:45 -07:00
|
|
|
|
for (i = 0; i < ggResults.length; i++) {
|
|
|
|
|
book = ggResults[i];
|
2017-04-02 01:05:07 -07:00
|
|
|
|
var bookCover;
|
|
|
|
|
if (book.volumeInfo.imageLinks) {
|
|
|
|
|
bookCover = book.volumeInfo.imageLinks.thumbnail;
|
|
|
|
|
} else {
|
|
|
|
|
bookCover = "/static/generic_cover.jpg";
|
|
|
|
|
}
|
2017-04-03 10:57:45 -07:00
|
|
|
|
bookHtml = "<li class=\"media\">" +
|
|
|
|
|
"<img class=\"pull-left img-responsive\" data-toggle=\"modal\" data-target=\"#metaModal\" src=\"" +
|
2017-04-21 11:44:17 -07:00
|
|
|
|
bookCover + "\" alt=\"Cover\" style=\"width:100px;height:150px\" onclick='getMeta(\"google\"," +
|
|
|
|
|
i + ")'>" +
|
2017-04-03 10:57:45 -07:00
|
|
|
|
"<div class=\"media-body\">" +
|
|
|
|
|
"<h4 class=\"media-heading\"><a href=\"https://books.google.com/books?id=" +
|
|
|
|
|
book.id + "\" target=\"_blank\">" + book.volumeInfo.title + "</a></h4>" +
|
2017-04-02 01:05:07 -07:00
|
|
|
|
"<p>"+ msg.author +":" + book.volumeInfo.authors + "</p>" +
|
|
|
|
|
"<p>"+ msg.publisher + ":" + book.volumeInfo.publisher + "</p>" +
|
|
|
|
|
"<p>"+ msg.description + ":" + book.volumeInfo.description + "</p>" +
|
2017-04-03 10:57:45 -07:00
|
|
|
|
"<p>"+ msg.source + ":<a href=\"https://books.google.com\" target=\"_blank\">Google Books</a></p>" +
|
2017-04-02 01:05:07 -07:00
|
|
|
|
"</div>" +
|
|
|
|
|
"</li>";
|
2017-04-21 11:44:17 -07:00
|
|
|
|
$("#metaModal #book-list").append(bookHtml);
|
2017-04-02 01:05:07 -07:00
|
|
|
|
}
|
|
|
|
|
ggDone = false;
|
|
|
|
|
}
|
|
|
|
|
if (dbDone && dbResults.length > 0) {
|
2017-04-03 10:57:45 -07:00
|
|
|
|
for (i = 0; i < dbResults.length; i++) {
|
|
|
|
|
book = dbResults[i];
|
|
|
|
|
bookHtml = "<li class=\"media\">" +
|
|
|
|
|
"<img class=\"pull-left img-responsive\" data-toggle=\"modal\" data-target=\"#metaModal\" src=\"" +
|
2017-04-21 11:44:17 -07:00
|
|
|
|
book.image + "\" alt=\"Cover\" style=\"width:100px;height: 150px\" onclick='getMeta(\"douban\"," +
|
|
|
|
|
i + ")'>" +
|
2017-04-03 10:57:45 -07:00
|
|
|
|
"<div class=\"media-body\">" +
|
|
|
|
|
"<h4 class=\"media-heading\"><a href=\"https://book.douban.com/subject/" +
|
|
|
|
|
book.id + "\" target=\"_blank\">" + book.title + "</a></h4>" +
|
2017-04-02 01:05:07 -07:00
|
|
|
|
"<p>" + msg.author + ":" + book.author + "</p>" +
|
|
|
|
|
"<p>" + msg.publisher + ":" + book.publisher + "</p>" +
|
|
|
|
|
"<p>" + msg.description + ":" + book.summary + "</p>" +
|
2017-04-03 10:57:45 -07:00
|
|
|
|
"<p>" + msg.source + ":<a href=\"https://book.douban.com\" target=\"_blank\">Douban Books</a></p>" +
|
2017-04-02 01:05:07 -07:00
|
|
|
|
"</div>" +
|
|
|
|
|
"</li>";
|
2017-04-21 11:44:17 -07:00
|
|
|
|
$("#metaModal #book-list").append(bookHtml);
|
2017-04-02 01:05:07 -07:00
|
|
|
|
}
|
|
|
|
|
dbDone = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function ggSearchBook (title) {
|
2017-03-30 12:17:18 -07:00
|
|
|
|
title = title.replaceAll(/\s+/, "+");
|
2017-04-02 01:05:07 -07:00
|
|
|
|
var url = google + ggSearch + "?q=" + title;
|
2017-02-27 22:52:55 -08:00
|
|
|
|
$.ajax({
|
2017-03-30 12:17:18 -07:00
|
|
|
|
url,
|
2017-02-27 23:17:37 -08:00
|
|
|
|
type: "GET",
|
|
|
|
|
dataType: "jsonp",
|
2017-03-30 12:17:18 -07:00
|
|
|
|
jsonp: "callback",
|
2017-04-03 10:57:45 -07:00
|
|
|
|
success (data) {
|
2017-04-02 01:05:07 -07:00
|
|
|
|
ggResults = data.items;
|
2017-02-27 22:52:55 -08:00
|
|
|
|
},
|
2017-04-03 10:57:45 -07:00
|
|
|
|
complete () {
|
2017-04-02 01:05:07 -07:00
|
|
|
|
ggDone = true;
|
|
|
|
|
showResult();
|
2017-02-27 22:52:55 -08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-02 01:05:07 -07:00
|
|
|
|
function dbSearchBook (title) {
|
|
|
|
|
var url = douban + dbSearch + "?q=" + title + "&fields=all&count=10";
|
2017-03-31 09:41:05 -07:00
|
|
|
|
$.ajax({
|
2017-04-03 12:05:28 -07:00
|
|
|
|
url,
|
2017-03-31 09:41:05 -07:00
|
|
|
|
type: "GET",
|
|
|
|
|
dataType: "jsonp",
|
2017-04-02 01:05:07 -07:00
|
|
|
|
jsonp: "callback",
|
2017-04-03 10:57:45 -07:00
|
|
|
|
success (data) {
|
2017-04-02 01:05:07 -07:00
|
|
|
|
dbResults = data.books;
|
2017-03-31 09:41:05 -07:00
|
|
|
|
},
|
2017-04-03 10:57:45 -07:00
|
|
|
|
error () {
|
2017-04-21 11:44:17 -07:00
|
|
|
|
$("#metaModal #meta-info").html("<p class=\"text-danger\">"+ msg.search_error+"!</p>");
|
2017-03-31 09:41:05 -07:00
|
|
|
|
},
|
2017-04-03 10:57:45 -07:00
|
|
|
|
complete () {
|
2017-03-31 09:41:05 -07:00
|
|
|
|
dbDone = true;
|
2017-04-02 01:05:07 -07:00
|
|
|
|
showResult();
|
2017-03-31 09:41:05 -07:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-03 10:57:45 -07:00
|
|
|
|
function doSearch (keyword) {
|
|
|
|
|
showFlag = 0;
|
2017-04-21 11:44:17 -07:00
|
|
|
|
$("#metaModal #meta-info").text(msg.loading);
|
2017-04-03 10:57:45 -07:00
|
|
|
|
// var keyword = $("#keyword").val();
|
|
|
|
|
if (keyword) {
|
|
|
|
|
dbSearchBook(keyword);
|
|
|
|
|
ggSearchBook(keyword);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-30 12:17:18 -07:00
|
|
|
|
$("#do-search").click(function () {
|
|
|
|
|
var keyword = $("#keyword").val();
|
2017-02-27 22:52:55 -08:00
|
|
|
|
if (keyword) {
|
2017-04-02 01:05:07 -07:00
|
|
|
|
doSearch(keyword);
|
2017-02-27 22:52:55 -08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2017-03-30 12:17:18 -07:00
|
|
|
|
$("#get_meta").click(function () {
|
2017-04-02 01:05:07 -07:00
|
|
|
|
var bookTitle = $("#book_title").val();
|
|
|
|
|
if (bookTitle) {
|
|
|
|
|
$("#keyword").val(bookTitle);
|
|
|
|
|
doSearch(bookTitle);
|
2017-02-27 22:52:55 -08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2017-04-21 11:44:17 -07:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function getMeta (source, id) {
|
|
|
|
|
var meta;
|
|
|
|
|
var tags;
|
|
|
|
|
if (source === "google") {
|
|
|
|
|
meta = ggResults[id];
|
|
|
|
|
$("#description").val(meta.volumeInfo.description);
|
|
|
|
|
$("#bookAuthor").val(meta.volumeInfo.authors.join(" & "));
|
|
|
|
|
$("#book_title").val(meta.volumeInfo.title);
|
|
|
|
|
if (meta.volumeInfo.categories) {
|
|
|
|
|
tags = meta.volumeInfo.categories.join(",");
|
|
|
|
|
$("#tags").val(tags);
|
|
|
|
|
}
|
|
|
|
|
if (meta.volumeInfo.averageRating) {
|
|
|
|
|
$("#rating").val(Math.round(meta.volumeInfo.averageRating));
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (source === "douban") {
|
|
|
|
|
meta = dbResults[id];
|
|
|
|
|
$("#description").val(meta.summary);
|
|
|
|
|
$("#bookAuthor").val(meta.author.join(" & "));
|
|
|
|
|
$("#book_title").val(meta.title);
|
|
|
|
|
tags = "";
|
|
|
|
|
for (var i = 0; i < meta.tags.length; i++) {
|
|
|
|
|
tags = tags + meta.tags[i].title + ",";
|
|
|
|
|
}
|
|
|
|
|
$("#tags").val(tags);
|
|
|
|
|
$("#rating").val(Math.round(meta.rating.average / 2));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|