support douban book search using apikey

pull/1093/head
zhiyue 5 years ago
parent d1afdb4aac
commit 222797e631
No known key found for this signature in database
GPG Key ID: 59D1B2DBE6E56B77

@ -19,15 +19,15 @@
* Google Books api document: https://developers.google.com/books/docs/v1/using * 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) * Douban Books api document: https://developers.douban.com/wiki/?title=book_v2 (Chinese Only)
*/ */
/* global _, i18nMsg, tinymce */ /* global _, i18nMsg, tinymce */
// var dbResults = []; var dbResults = [];
var ggResults = []; var ggResults = [];
$(function () { $(function () {
var msg = i18nMsg; var msg = i18nMsg;
/*var douban = "https://api.douban.com"; var douban = "https://api.douban.com";
var dbSearch = "/v2/book/search";*/ var dbSearch = "/v2/book/search";
// var dbDone = true; var dbDone = true;
var google = "https://www.googleapis.com"; var google = "https://www.googleapis.com";
var ggSearch = "/books/v1/volumes"; var ggSearch = "/books/v1/volumes";
@ -43,12 +43,22 @@ $(function () {
function populateForm (book) { function populateForm (book) {
tinymce.get("description").setContent(book.description); tinymce.get("description").setContent(book.description);
var uniqueTags = [];
$.each(book.tags, function(i, el) {
if ($.inArray(el, uniqueTags) === -1) uniqueTags.push(el);
});
$("#bookAuthor").val(book.authors); $("#bookAuthor").val(book.authors);
$("#book_title").val(book.title); $("#book_title").val(book.title);
$("#tags").val(book.tags.join(",")); $("#tags").val(uniqueTags.join(","));
$("#rating").data("rating").setValue(Math.round(book.rating)); $("#rating").data("rating").setValue(Math.round(book.rating));
$(".cover img").attr("src", book.cover); $(".cover img").attr("src", book.cover);
$("#cover_url").val(book.cover); $("#cover_url").val(book.cover);
$("#pubdate").val(book.publishedDate);
$("#publisher").val(book.publisher)
if (book.series != undefined) {
$("#series").val(book.series)
}
} }
function showResult () { function showResult () {
@ -56,10 +66,24 @@ $(function () {
if (showFlag === 1) { if (showFlag === 1) {
$("#meta-info").html("<ul id=\"book-list\" class=\"media-list\"></ul>"); $("#meta-info").html("<ul id=\"book-list\" class=\"media-list\"></ul>");
} }
if (!ggDone) { if (!ggDone && !dbDone) {
$("#meta-info").html("<p class=\"text-danger\">" + msg.no_result + "</p>"); $("#meta-info").html("<p class=\"text-danger\">" + msg.no_result + "</p>");
return; return;
} }
function formatDate (date) {
var d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();
if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;
return [year, month, day].join('-');
}
if (ggDone && ggResults.length > 0) { if (ggDone && ggResults.length > 0) {
ggResults.forEach(function(result) { ggResults.forEach(function(result) {
var book = { var book = {
@ -72,8 +96,7 @@ $(function () {
tags: result.volumeInfo.categories || [], tags: result.volumeInfo.categories || [],
rating: result.volumeInfo.averageRating || 0, rating: result.volumeInfo.averageRating || 0,
cover: result.volumeInfo.imageLinks ? cover: result.volumeInfo.imageLinks ?
result.volumeInfo.imageLinks.thumbnail : result.volumeInfo.imageLinks.thumbnail : "/static/generic_cover.jpg",
"/static/generic_cover.jpg",
url: "https://books.google.com/books?id=" + result.id, url: "https://books.google.com/books?id=" + result.id,
source: { source: {
id: "google", id: "google",
@ -91,19 +114,30 @@ $(function () {
}); });
ggDone = false; ggDone = false;
} }
/*if (dbDone && dbResults.length > 0) { if (dbDone && dbResults.length > 0) {
dbResults.forEach(function(result) { dbResults.forEach(function(result) {
if (result.series){
var series_title = result.series.title
}
var date_fomers = result.pubdate.split("-")
var publishedYear = parseInt(date_fomers[0])
var publishedMonth = parseInt(date_fomers[1])
var publishedDate = new Date(publishedYear, publishedMonth-1, 1)
publishedDate = formatDate(publishedDate)
var book = { var book = {
id: result.id, id: result.id,
title: result.title, title: result.title,
authors: result.author || [], authors: result.author || [],
description: result.summary, description: result.summary,
publisher: result.publisher || "", publisher: result.publisher || "",
publishedDate: result.pubdate || "", publishedDate: publishedDate || "",
tags: result.tags.map(function(tag) { tags: result.tags.map(function(tag) {
return tag.title; return tag.title.toLowerCase().replace(/,/g, "_");
}), }),
rating: result.rating.average || 0, rating: result.rating.average || 0,
series: series_title || "",
cover: result.image, cover: result.image,
url: "https://book.douban.com/subject/" + result.id, url: "https://book.douban.com/subject/" + result.id,
source: { source: {
@ -125,7 +159,7 @@ $(function () {
$("#book-list").append($book); $("#book-list").append($book);
}); });
dbDone = false; dbDone = false;
}*/ }
} }
function ggSearchBook (title) { function ggSearchBook (title) {
@ -148,9 +182,10 @@ $(function () {
}); });
} }
/*function dbSearchBook (title) { function dbSearchBook (title) {
apikey="0df993c66c0c636e29ecbb5344252a4a"
$.ajax({ $.ajax({
url: douban + dbSearch + "?q=" + title + "&fields=all&count=10", url: douban + dbSearch + "?apikey=" + apikey + "&q=" + title + "&fields=all&count=10",
type: "GET", type: "GET",
dataType: "jsonp", dataType: "jsonp",
jsonp: "callback", jsonp: "callback",
@ -166,13 +201,13 @@ $(function () {
$("#show-douban").trigger("change"); $("#show-douban").trigger("change");
} }
}); });
}*/ }
function doSearch (keyword) { function doSearch (keyword) {
showFlag = 0; showFlag = 0;
$("#meta-info").text(msg.loading); $("#meta-info").text(msg.loading);
if (keyword) { if (keyword) {
// dbSearchBook(keyword); dbSearchBook(keyword);
ggSearchBook(keyword); ggSearchBook(keyword);
} }
} }

@ -219,8 +219,8 @@
</div> </div>
<div class="modal-body"> <div class="modal-body">
<div class="text-center padded-bottom"> <div class="text-center padded-bottom">
<!--input type="checkbox" id="show-douban" class="pill" data-control="douban" checked> <input type="checkbox" id="show-douban" class="pill" data-control="douban" checked>
<label for="show-douban">Douban <span class="glyphicon glyphicon-ok"></span></label--> <label for="show-douban">Douban <span class="glyphicon glyphicon-ok"></span></label>
<input type="checkbox" id="show-google" class="pill" data-control="google" checked> <input type="checkbox" id="show-google" class="pill" data-control="google" checked>
<label for="show-google">Google <span class="glyphicon glyphicon-ok"></span></label> <label for="show-google">Google <span class="glyphicon glyphicon-ok"></span></label>
@ -288,6 +288,7 @@
<script src="{{ url_for('static', filename='js/edit_books.js') }}"></script> <script src="{{ url_for('static', filename='js/edit_books.js') }}"></script>
{% endblock %} {% endblock %}
{% block header %} {% block header %}
<meta name="referrer" content="never">
<link href="{{ url_for('static', filename='css/libs/typeahead.css') }}" rel="stylesheet" media="screen"> <link href="{{ url_for('static', filename='css/libs/typeahead.css') }}" rel="stylesheet" media="screen">
<link href="{{ url_for('static', filename='css/libs/bootstrap-datepicker3.min.css') }}" rel="stylesheet" media="screen"> <link href="{{ url_for('static', filename='css/libs/bootstrap-datepicker3.min.css') }}" rel="stylesheet" media="screen">
{% endblock %} {% endblock %}

@ -1,4 +1,7 @@
{% extends "layout.html" %} {% extends "layout.html" %}
{% block header %}
<meta name="referrer" content="never">
{% endblock %}
{% block body %} {% block body %}
<div class="discover"> <div class="discover">
{% if entries|length < 1 %} {% if entries|length < 1 %}

Loading…
Cancel
Save