diff --git a/cps/static/js/douban_meta.js b/cps/static/js/douban_meta.js deleted file mode 100644 index b7935145..00000000 --- a/cps/static/js/douban_meta.js +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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); - if(i>20){break;} - } - } - }, - 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/static/js/get_meta.js b/cps/static/js/get_meta.js new file mode 100644 index 00000000..59b5523c --- /dev/null +++ b/cps/static/js/get_meta.js @@ -0,0 +1,179 @@ +/* + * Get Metadata from Douban Books api and Google Books api + * Created by idalin + * 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 () { + var douban = 'https://api.douban.com'; + var db_search = '/v2/book/search'; + var db_get_info = '/v2/book/'; + var db_get_info_by_isbn = '/v2/book/isbn/ '; + var db_done = false; + + var google = 'https://www.googleapis.com/'; + var gg_search = '/books/v1/volumes'; + var gg_get_info = '/books/v1/volumes/'; + var gg_done = false; + + var db_results = []; + var gg_results = []; + var show_flag = 0; + String.prototype.replaceAll = function (s1, s2) {   + return this.replace(new RegExp(s1, "gm"), s2);   + } + + $.ajaxSetup({ + type: "GET", + dataType: "jsonp", + jsonp: 'callback', + }); + + gg_search_book = function (title) { + title = title.replaceAll(/\s+/, '+'); + var url = google + gg_search + '?q=' + title; + $.ajax({ + url: url, + success: function (data) { + gg_results = data.items; + }, + complete: function () { + gg_done = true; + show_result(); + } + }); + } + + get_meta = function (source, id) { + var meta; + if (source == 'google') {; + meta = gg_results[id]; + $('#description').val(meta.volumeInfo.description); + $('#bookAuthor').val(meta.volumeInfo.authors.join(' & ')); + $('#book_title').val(meta.volumeInfo.title); + if (meta.volumeInfo.categories) { + var tags = meta.volumeInfo.categories.join(','); + $('#tags').val(tags); + } + if (meta.volumeInfo.averageRating) { + $('#rating').val(Math.round(meta.volumeInfo.averageRating)); + } + return; + } + if (source == 'douban') { + meta = db_results[id]; + $('#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 / 2)); + return; + } + } + do_search = function (keyword) { + show_flag = 0; + $('#meta-info').text('Loading...'); + var keyword = $('#keyword').val(); + if (keyword) { + db_search_book(keyword); + gg_search_book(keyword); + } + } + + db_search_book = function (title) { + var url = douban + db_search + '?q=' + title + '&fields=all&count=10'; + $.ajax({ + url: url, + success: function (data) { + db_results = data.books; + }, + error: function () { + $('#meta-info').html('

    Search error!

    '); + }, + complete: function () { + db_done = true; + show_result(); + } + }); + } + + show_result = function () { + show_flag++; + if (show_flag == 1) { + $('#meta-info').html(''); + } + if (gg_done && db_done) { + if (gg_results.length < 1 && db_results.length < 1) { + $('#meta-info').html('

    No Result! Please try anonther keyword.

    '); + 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 = '
  • ' + + 'Cover' + + '
    ' + + '

    ' + book.volumeInfo.title + '

    ' + + '

    Author:' + book.volumeInfo.authors + '

    ' + + '

    Publisher:' + book.volumeInfo.publisher + '

    ' + + '

    Description:' + book.volumeInfo.description + '

    ' + + '

    Source:Google Books

    ' + + '
    ' + + '
  • '; + $("#book-list").append(book_html); + } + gg_done = false; + } + if (db_done && db_results.length > 0) { + for (var i = 0; i < db_results.length; i++) { + var book = db_results[i]; + var book_html = '
  • ' + + 'Cover' + + '
    ' + + '

    ' + book.title + '

    ' + + '

    Author:' + book.author + '

    ' + + '

    Publisher:' + book.publisher + '

    ' + + '

    Description:' + book.summary + '

    ' + + '

    Source:Douban Books

    ' + + '
    ' + + '
  • '; + $("#book-list").append(book_html); + } + db_done = false; + } + } + + $('#do-search').click(function () { + var keyword = $('#keyword').val(); + if (keyword) { + do_search(keyword); + } + }); + + $('#get_meta').click(function () { + var book_title = $('#book_title').val(); + if (book_title) { + $('#keyword').val(book_title); + do_search(book_title); + } + }); + +}); \ No newline at end of file diff --git a/cps/templates/book_edit.html b/cps/templates/book_edit.html index 16e1c0b9..fe1407c9 100644 --- a/cps/templates/book_edit.html +++ b/cps/templates/book_edit.html @@ -1,13 +1,9 @@ -{% extends "layout.html" %} -{% block body %} -{% if book %} +{% extends "layout.html" %} {% block body %} {% if book %}
    {% if book.has_cover %} - - {% else %} - - {% endif %} + {% else %} + {% endif %}
    @@ -29,54 +25,43 @@
    -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    - +
    + + +
    +
    + + +
    +
    + + +
    +
    + + +
    +
    - - {% if cc|length > 0 %} - {% for c in cc %} -
    - - {% if c.datatype == 'bool' %} - - - {% endif %} - {% if c.datatype in ['text', 'series'] and not c.is_multiple %} - 0 %} - value="{{ book['custom_column_' ~ c.id][0].value }}" - {% endif %}> - {% endif %} - - {% if c.datatype in ['text', 'series'] and c.is_multiple %} - 0 %} - value="{% for column in book['custom_column_' ~ c.id] %}{{ column.value.strip() }}{% if not loop.last %}, {% endif %}{% endfor %}"{% endif %}> - {% endif %} - - {% if c.datatype == 'enumeration' %} - {% endif %} {% if c.datatype in ['text', 'series'] and not c.is_multiple %} + 0 %} value="{{ book['custom_column_' ~ c.id][0].value }}" {% endif %}> {% endif %} {% if c.datatype + in ['text', 'series'] and c.is_multiple %} + 0 %} value="{% for column in book['custom_column_' ~ c.id] %}{{ column.value.strip() }}{% if + not loop.last %}, {% endif %}{% endfor %}"{% endif %}> {% endif %} {% if c.datatype == 'enumeration' %} + - {% endif %} + {% endif %} {% if c.datatype == 'rating' %} + 0 %} value="{{ '%d' % (book['custom_column_' ~ c.id][0].value / + 2) }}" {% endif %}> {% endif %} +
    + {% endfor %} {% endif %} + - {% if c.datatype == 'rating' %} - 0 %} - value="{{ '%d' % (book['custom_column_' ~ c.id][0].value / 2) }}" - {% endif %}> - {% endif %} - - {% endfor %} - {% endif %} - -
    + {{_('Get Meta Data')}} {{_('Back')}} @@ -115,6 +95,14 @@ -{% endblock %} - -{% block js %} +{% endblock %} {% block js %} - -{% endblock %} -{% block header %} - -{% endblock %} + +{% endblock %} {% block header %} + {% endblock %} \ No newline at end of file