Fix #6040 (Update the Douban.com metadata plugin)

This commit is contained in:
Kovid Goyal 2010-07-01 07:14:21 -06:00
commit 736400e2d2

View File

@ -15,7 +15,6 @@ from calibre.utils.config import OptionParser
from calibre.ebooks.metadata.fetch import MetadataSource from calibre.ebooks.metadata.fetch import MetadataSource
from calibre.utils.date import parse_date, utcnow from calibre.utils.date import parse_date, utcnow
DOUBAN_API_KEY = None
NAMESPACES = { NAMESPACES = {
'openSearch':'http://a9.com/-/spec/opensearchrss/1.0/', 'openSearch':'http://a9.com/-/spec/opensearchrss/1.0/',
'atom' : 'http://www.w3.org/2005/Atom', 'atom' : 'http://www.w3.org/2005/Atom',
@ -35,13 +34,15 @@ date = XPath("descendant::db:attribute[@name='pubdate']")
creator = XPath("descendant::db:attribute[@name='author']") creator = XPath("descendant::db:attribute[@name='author']")
tag = XPath("descendant::db:tag") tag = XPath("descendant::db:tag")
CALIBRE_DOUBAN_API_KEY = '0bd1672394eb1ebf2374356abec15c3d'
class DoubanBooks(MetadataSource): class DoubanBooks(MetadataSource):
name = 'Douban Books' name = 'Douban Books'
description = _('Downloads metadata from Douban.com') description = _('Downloads metadata from Douban.com')
supported_platforms = ['windows', 'osx', 'linux'] # Platforms this plugin will run on supported_platforms = ['windows', 'osx', 'linux'] # Platforms this plugin will run on
author = 'Li Fanxi <lifanxi@freemindworld.com>' # The author of this plugin author = 'Li Fanxi <lifanxi@freemindworld.com>' # The author of this plugin
version = (1, 0, 0) # The version number of this plugin version = (1, 0, 1) # The version number of this plugin
def fetch(self): def fetch(self):
try: try:
@ -65,7 +66,7 @@ class Query(object):
type = "search" type = "search"
def __init__(self, title=None, author=None, publisher=None, isbn=None, def __init__(self, title=None, author=None, publisher=None, isbn=None,
max_results=20, start_index=1): max_results=20, start_index=1, api_key=''):
assert not(title is None and author is None and publisher is None and \ assert not(title is None and author is None and publisher is None and \
isbn is None) isbn is None)
assert (int(max_results) < 21) assert (int(max_results) < 21)
@ -89,16 +90,16 @@ class Query(object):
if self.type == "isbn": if self.type == "isbn":
self.url = self.ISBN_URL + q self.url = self.ISBN_URL + q
if DOUBAN_API_KEY is not None: if api_key != '':
self.url = self.url + "?apikey=" + DOUBAN_API_KEY self.url = self.url + "?apikey=" + api_key
else: else:
self.url = self.SEARCH_URL+urlencode({ self.url = self.SEARCH_URL+urlencode({
'q':q, 'q':q,
'max-results':max_results, 'max-results':max_results,
'start-index':start_index, 'start-index':start_index,
}) })
if DOUBAN_API_KEY is not None: if api_key != '':
self.url = self.url + "&apikey=" + DOUBAN_API_KEY self.url = self.url + "&apikey=" + api_key
def __call__(self, browser, verbose): def __call__(self, browser, verbose):
if verbose: if verbose:
@ -177,7 +178,7 @@ class ResultList(list):
d = None d = None
return d return d
def populate(self, entries, browser, verbose=False): def populate(self, entries, browser, verbose=False, api_key=''):
for x in entries: for x in entries:
try: try:
id_url = entry_id(x)[0].text id_url = entry_id(x)[0].text
@ -186,8 +187,8 @@ class ResultList(list):
report(verbose) report(verbose)
mi = MetaInformation(title, self.get_authors(x)) mi = MetaInformation(title, self.get_authors(x))
try: try:
if DOUBAN_API_KEY is not None: if api_key != '':
id_url = id_url + "?apikey=" + DOUBAN_API_KEY id_url = id_url + "?apikey=" + api_key
raw = browser.open(id_url).read() raw = browser.open(id_url).read()
feed = etree.fromstring(raw) feed = etree.fromstring(raw)
x = entry(feed)[0] x = entry(feed)[0]
@ -203,12 +204,16 @@ class ResultList(list):
self.append(mi) self.append(mi)
def search(title=None, author=None, publisher=None, isbn=None, def search(title=None, author=None, publisher=None, isbn=None,
verbose=False, max_results=40): verbose=False, max_results=40, api_key=None):
br = browser() br = browser()
start, entries = 1, [] start, entries = 1, []
if api_key is None:
api_key = CALIBRE_DOUBAN_API_KEY
while start > 0 and len(entries) <= max_results: while start > 0 and len(entries) <= max_results:
new, start = Query(title=title, author=author, publisher=publisher, new, start = Query(title=title, author=author, publisher=publisher,
isbn=isbn, max_results=max_results, start_index=start)(br, verbose) isbn=isbn, max_results=max_results, start_index=start, api_key=api_key)(br, verbose)
if not new: if not new:
break break
entries.extend(new) entries.extend(new)
@ -216,7 +221,7 @@ def search(title=None, author=None, publisher=None, isbn=None,
entries = entries[:max_results] entries = entries[:max_results]
ans = ResultList() ans = ResultList()
ans.populate(entries, br, verbose) ans.populate(entries, br, verbose, api_key)
return ans return ans
def option_parser(): def option_parser():