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