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