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

186 lines
6.7 KiB
JavaScript

/*
* 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)
*/
/* global i18nMsg */
$(document).ready(function () {
8 years ago
var msg = i18nMsg;
8 years ago
var douban = "https://api.douban.com";
8 years ago
var dbSearch = "/v2/book/search";
8 years ago
// var dbGetInfo = "/v2/book/";
// var db_get_info_by_isbn = "/v2/book/isbn/ ";
8 years ago
var dbDone = false;
8 years ago
var google = "https://www.googleapis.com/";
8 years ago
var ggSearch = "/books/v1/volumes";
// var gg_get_info = "/books/v1/volumes/";
var ggDone = false;
8 years ago
var dbResults = [];
var ggResults = [];
var showFlag = 0;
String.prototype.replaceAll = function (s1, s2) {
8 years ago
return this.replace(new RegExp(s1, "gm"), s2);
8 years ago
};
8 years ago
function showResult () {
var book;
var i;
var bookHtml;
8 years ago
showFlag++;
if (showFlag === 1) {
$("#meta-info").html("<ul id=\"book-list\" class=\"media-list\"></ul>");
8 years ago
}
if (ggDone && dbDone) {
if (!ggResults && !dbResults) {
$("#meta-info").html("<p class=\"text-danger\">"+ msg.no_result +"</p>");
8 years ago
return;
}
}
if (ggDone && ggResults.length > 0) {
for (i = 0; i < ggResults.length; i++) {
book = ggResults[i];
8 years ago
var bookCover;
if (book.volumeInfo.imageLinks) {
bookCover = book.volumeInfo.imageLinks.thumbnail;
} else {
bookCover = "/static/generic_cover.jpg";
}
bookHtml = "<li class=\"media\">" +
"<img class=\"pull-left img-responsive\" data-toggle=\"modal\" data-target=\"#metaModal\" src=\"" +
bookCover + "\" alt=\"Cover\" style=\"width:100px;height:150px\" onclick='javascript:getMeta(\"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>" +
8 years ago
"<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>" +
8 years ago
"</div>" +
"</li>";
$("#book-list").append(bookHtml);
}
ggDone = false;
}
if (dbDone && dbResults.length > 0) {
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=\"" +
book.image + "\" alt=\"Cover\" style=\"width:100px;height: 150px\" onclick='javascript:getMeta(\"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>" +
8 years ago
"<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>" +
8 years ago
"</div>" +
"</li>";
$("#book-list").append(bookHtml);
}
dbDone = false;
}
}
function ggSearchBook (title) {
8 years ago
title = title.replaceAll(/\s+/, "+");
8 years ago
var url = google + ggSearch + "?q=" + title;
$.ajax({
8 years ago
url,
type: "GET",
dataType: "jsonp",
8 years ago
jsonp: "callback",
success (data) {
8 years ago
ggResults = data.items;
},
complete () {
8 years ago
ggDone = true;
showResult();
}
});
}
function getMeta (source, id) {
var meta;
8 years ago
var tags;
if (source === "google") {
8 years ago
meta = ggResults[id];
8 years ago
$("#description").val(meta.volumeInfo.description);
8 years ago
$("#bookAuthor").val(meta.volumeInfo.authors.join(" & "));
8 years ago
$("#book_title").val(meta.volumeInfo.title);
if (meta.volumeInfo.categories) {
8 years ago
tags = meta.volumeInfo.categories.join(",");
8 years ago
$("#tags").val(tags);
}
if (meta.volumeInfo.averageRating) {
8 years ago
$("#rating").val(Math.round(meta.volumeInfo.averageRating));
}
return;
}
8 years ago
if (source === "douban") {
8 years ago
meta = dbResults[id];
8 years ago
$("#description").val(meta.summary);
8 years ago
$("#bookAuthor").val(meta.author.join(" & "));
8 years ago
$("#book_title").val(meta.title);
8 years ago
tags = "";
for (var i = 0; i < meta.tags.length; i++) {
8 years ago
tags = tags + meta.tags[i].title + ",";
}
8 years ago
$("#tags").val(tags);
$("#rating").val(Math.round(meta.rating.average / 2));
return;
}
}
8 years ago
8 years ago
function dbSearchBook (title) {
var url = douban + dbSearch + "?q=" + title + "&fields=all&count=10";
8 years ago
$.ajax({
8 years ago
url,
8 years ago
type: "GET",
dataType: "jsonp",
8 years ago
jsonp: "callback",
success (data) {
8 years ago
dbResults = data.books;
8 years ago
},
error () {
8 years ago
$("#meta-info").html("<p class=\"text-danger\">"+ msg.search_error+"!</p>");
8 years ago
},
complete () {
8 years ago
dbDone = true;
8 years ago
showResult();
8 years ago
}
});
}
function doSearch (keyword) {
showFlag = 0;
$("#meta-info").text(msg.loading);
// var keyword = $("#keyword").val();
if (keyword) {
dbSearchBook(keyword);
ggSearchBook(keyword);
}
}
8 years ago
$("#do-search").click(function () {
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);
}
});
});