fetch metadata from douban while editing metadata

pull/124/head
idalin 8 years ago
parent 4ec3050082
commit 80e6311b93

@ -0,0 +1,87 @@
/*
* Get Metadata from Douban api
* Created by idalin<dalin.lin@gmail.com>
*/
$(document).ready(function () {
var get_meta_btn = '<li>' +
'<a href="#" id="get_meta" data-toggle="modal" data-target="#metaModal">' +
'获取Meta</a></li>';
$('#main-nav').prepend(get_meta_btn);
var douban = 'https://api.douban.com';
var search = '/v2/book/search';
var get_info = '/v2/book/';
var get_info_by_isbn = '/v2/book/isbn/ ';
$.ajaxSetup({
type: "GET",
dataType: "jsonp",
jsonp: 'callback',
async: false
});
get_meta = function (id) {
var url = douban + get_info + id;
console.log('getting book meta:' + id);
$.ajax({
url: url,
success: function (meta) {
console.log(meta);
//$('#metaModal').modal('hide');
$('#description').val(meta.summary);
$('#bookAuthor').val(meta.author.join(' & '));
$('#book_title').val(meta.title);
var 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 / 4));
}
});
}
get_meta_by_isbn = function (isbn) {
var url = douban + get_info_by_isbn + isbn;
}
search_book = function (title) {
var url = douban + search + '?q=' + title + '&fields=id,title,author,publisher,isbn13,image,summary';
$.ajax({
url: url,
success: function (data) {
if (data.books.length < 1) {
$('#meta-info').html('<p class="text-danger">搜索不到对应的书籍</p>');
} else {
$('#meta-info').html('<ul id="book-list" class="media-list"></ul>');
for (var i = 0; i < data.books.length; i++) {
var book = '<li class="media">' +
'<img class="pull-left img-responsive" data-toggle="modal" data-target="#metaModal" src="' +
data.books[i].image + '" alt="Cover" onclick="javascript:get_meta(' +
data.books[i].id + ')">' +
'<div class="media-body">' +
'<h4 class="media-heading"><a href="https://book.douban.com/subject/' +
data.books[i].id + '" target="_blank">' + data.books[i].title + '</a></h4>' +
'<p>作者:' + data.books[i].author + '</p>' +
'<p>出版社:' + data.books[i].publisher + '</p>' +
'<p>简介:' + data.books[i].summary + '</p>' +
'</div>' +
'</li>';
$("#book-list").append(book);
}
}
},
error: function () {
$('#meta-info').html('<p class="text-danger">搜索出错</p>');
}
});
}
$('#get_meta').click(function () {
var book_title = $('#book_title').val();
if (book_title) {
// console.log(book_title);
search_book(book_title);
}
});
});

@ -109,11 +109,28 @@
</form>
</div>
{% endif %}
<div class="modal fade" id="metaModal" tabindex="-1" role="dialog" aria-labelledby="metaModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="metaModalLabel">{{_('Get meta data')}}</h4>
</div>
<div class="modal-body" id="meta-info">
{{_("Loading...")}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">{{_('Close')}}</button>
</div>
</div>
</div>
</div>
{% endblock %}
{% block js %}
<script src="{{ url_for('static', filename='js/libs/typeahead.bundle.js') }}"></script>
<script src="{{ url_for('static', filename='js/edit_books.js') }}"></script>
<script src="{{ url_for('static', filename='js/douban_meta.js') }}"></script>
{% endblock %}
{% block header %}
<link href="{{ url_for('static', filename='css/libs/typeahead.css') }}" rel="stylesheet" media="screen">

Loading…
Cancel
Save