Support subtitle in Douban metadata plugin

This commit is contained in:
Li Fanxi 2019-06-05 23:23:01 +08:00
parent dedbc1ddb3
commit d1a6113a8a

View File

@ -1,5 +1,6 @@
#!/usr/bin/env python2 #!/usr/bin/env python2
# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai # vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
from __future__ import absolute_import, division, print_function, unicode_literals from __future__ import absolute_import, division, print_function, unicode_literals
__license__ = 'GPL v3' __license__ = 'GPL v3'
@ -15,7 +16,7 @@ except ImportError:
from calibre.ebooks.metadata import check_isbn from calibre.ebooks.metadata import check_isbn
from calibre.ebooks.metadata.sources.base import Source from calibre.ebooks.metadata.sources.base import Option, Source
from calibre.ebooks.metadata.book.base import Metadata from calibre.ebooks.metadata.book.base import Metadata
from calibre import as_unicode from calibre import as_unicode
@ -44,7 +45,32 @@ def get_details(browser, url, timeout): # {{{
# }}} # }}}
def to_metadata(browser, log, entry_, timeout): # {{{ class Douban(Source):
name = 'Douban Books'
author = 'Li Fanxi'
version = (2, 1, 0)
minimum_calibre_version = (2, 80, 0)
description = _('Downloads metadata and covers from Douban.com. '
'Useful only for Chinese language books.')
capabilities = frozenset(['identify', 'cover'])
touched_fields = frozenset(['title', 'authors', 'tags',
'pubdate', 'comments', 'publisher', 'identifier:isbn', 'rating',
'identifier:douban']) # language currently disabled
supports_gzip_transfer_encoding = True
cached_cover_url_is_reliable = True
DOUBAN_API_KEY = '0bd1672394eb1ebf2374356abec15c3d'
DOUBAN_BOOK_URL = 'https://book.douban.com/subject/%s/'
options = (
Option('include_subtitle_in_title', 'bool', True, _('Include subtitle in book title:'),
_('Whether to append subtitle in the book title.')),
)
def to_metadata(self, browser, log, entry_, timeout): # {{{
from lxml import etree from lxml import etree
from calibre.ebooks.chardet import xml_to_unicode from calibre.ebooks.chardet import xml_to_unicode
from calibre.utils.date import parse_date, utcnow from calibre.utils.date import parse_date, utcnow
@ -55,6 +81,7 @@ def to_metadata(browser, log, entry_, timeout): # {{{
entry_id = XPath('descendant::atom:id') entry_id = XPath('descendant::atom:id')
title = XPath('descendant::atom:title') title = XPath('descendant::atom:title')
description = XPath('descendant::atom:summary') description = XPath('descendant::atom:summary')
subtitle = XPath("descendant::db:attribute[@name='subtitle']")
publisher = XPath("descendant::db:attribute[@name='publisher']") publisher = XPath("descendant::db:attribute[@name='publisher']")
isbn = XPath("descendant::db:attribute[@name='isbn13']") isbn = XPath("descendant::db:attribute[@name='isbn13']")
date = XPath("descendant::db:attribute[@name='pubdate']") date = XPath("descendant::db:attribute[@name='pubdate']")
@ -77,6 +104,9 @@ def to_metadata(browser, log, entry_, timeout): # {{{
id_url = entry_id(entry_)[0].text.replace('http://', 'https://') id_url = entry_id(entry_)[0].text.replace('http://', 'https://')
douban_id = id_url.split('/')[-1] douban_id = id_url.split('/')[-1]
title_ = ': '.join([x.text for x in title(entry_)]).strip() title_ = ': '.join([x.text for x in title(entry_)]).strip()
subtitle = ': '.join([x.text for x in subtitle(entry_)]).strip()
if self.prefs['include_subtitle_in_title'] and len(subtitle) > 0:
title_ = title_ + ' - ' + subtitle
authors = [x.text.strip() for x in creator(entry_) if x.text] authors = [x.text.strip() for x in creator(entry_) if x.text]
if not authors: if not authors:
authors = [_('Unknown')] authors = [_('Unknown')]
@ -87,6 +117,7 @@ def to_metadata(browser, log, entry_, timeout): # {{{
mi = Metadata(title_, authors) mi = Metadata(title_, authors)
mi.identifiers = {'douban':douban_id} mi.identifiers = {'douban':douban_id}
try: try:
log.info(id_url)
raw = get_details(browser, id_url, timeout) raw = get_details(browser, id_url, timeout)
feed = etree.fromstring(xml_to_unicode(clean_ascii_chars(raw), feed = etree.fromstring(xml_to_unicode(clean_ascii_chars(raw),
strip_encoding_pats=True)[0]) strip_encoding_pats=True)[0])
@ -147,28 +178,7 @@ def to_metadata(browser, log, entry_, timeout): # {{{
if u.find('book-default') == -1: if u.find('book-default') == -1:
mi.has_douban_cover = u mi.has_douban_cover = u
return mi return mi
# }}} # }}}
class Douban(Source):
name = 'Douban Books'
author = 'Li Fanxi'
version = (2, 1, 0)
minimum_calibre_version = (2, 80, 0)
description = _('Downloads metadata and covers from Douban.com. '
'Useful only for Chinese language books.')
capabilities = frozenset(['identify', 'cover'])
touched_fields = frozenset(['title', 'authors', 'tags',
'pubdate', 'comments', 'publisher', 'identifier:isbn', 'rating',
'identifier:douban']) # language currently disabled
supports_gzip_transfer_encoding = True
cached_cover_url_is_reliable = True
DOUBAN_API_KEY = '0bd1672394eb1ebf2374356abec15c3d'
DOUBAN_BOOK_URL = 'https://book.douban.com/subject/%s/'
def get_book_url(self, identifiers): # {{{ def get_book_url(self, identifiers): # {{{
db = identifiers.get('douban', None) db = identifiers.get('douban', None)
@ -286,7 +296,7 @@ class Douban(Source):
from lxml import etree from lxml import etree
for relevance, i in enumerate(entries): for relevance, i in enumerate(entries):
try: try:
ans = to_metadata(br, log, i, timeout) ans = self.to_metadata(br, log, i, timeout)
if isinstance(ans, Metadata): if isinstance(ans, Metadata):
ans.source_relevance = relevance ans.source_relevance = relevance
db = ans.identifiers['douban'] db = ans.identifiers['douban']
@ -352,8 +362,6 @@ if __name__ == '__main__': # tests {{{
title_test, authors_test) title_test, authors_test)
test_identify_plugin(Douban.name, test_identify_plugin(Douban.name,
[ [
( (
{'identifiers':{'isbn': '9787536692930'}, 'title':'三体', {'identifiers':{'isbn': '9787536692930'}, 'title':'三体',
'authors':['刘慈欣']}, 'authors':['刘慈欣']},