From 80e6311b9306d29dc3ff31014705f92ab0cc6894 Mon Sep 17 00:00:00 2001 From: idalin Date: Tue, 21 Feb 2017 17:49:43 +0800 Subject: [PATCH] fetch metadata from douban while editing metadata --- cps/static/js/douban_meta.js | 87 ++++++++++++++++++++++++++++++++++++ cps/templates/book_edit.html | 17 +++++++ 2 files changed, 104 insertions(+) create mode 100644 cps/static/js/douban_meta.js diff --git a/cps/static/js/douban_meta.js b/cps/static/js/douban_meta.js new file mode 100644 index 00000000..773939f9 --- /dev/null +++ b/cps/static/js/douban_meta.js @@ -0,0 +1,87 @@ +/* + * Get Metadata from Douban api + * Created by idalin + */ + +$(document).ready(function () { + var get_meta_btn = '
  • ' + + '' + + '获取Meta
  • '; + $('#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('

    搜索不到对应的书籍

    '); + } else { + $('#meta-info').html(''); + for (var i = 0; i < data.books.length; i++) { + var book = '
  • ' + + 'Cover' + + '
    ' + + '

    ' + data.books[i].title + '

    ' + + '

    作者:' + data.books[i].author + '

    ' + + '

    出版社:' + data.books[i].publisher + '

    ' + + '

    简介:' + data.books[i].summary + '

    ' + + '
    ' + + '
  • '; + $("#book-list").append(book); + } + } + }, + error: function () { + $('#meta-info').html('

    搜索出错

    '); + } + }); + } + + $('#get_meta').click(function () { + var book_title = $('#book_title').val(); + if (book_title) { + // console.log(book_title); + search_book(book_title); + } + }); +}); \ No newline at end of file diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index 32452dd8..16e1c0b9 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -109,11 +109,28 @@ {% endif %} + {% endblock %} {% block js %} + {% endblock %} {% block header %}