From daa01500443eba9894c0d636365271299f343f79 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 4 Apr 2011 12:06:54 -0600 Subject: [PATCH] When downloading metadata use the gzip transfer encoding when possible for a speedup. Fixes #749304 (metadata from google books not readable) --- src/calibre/ebooks/metadata/google_books.py | 1 + src/calibre/ebooks/metadata/sources/amazon.py | 1 + src/calibre/ebooks/metadata/sources/base.py | 8 ++++++++ src/calibre/ebooks/metadata/sources/google.py | 1 + src/calibre/utils/browser.py | 6 +++--- 5 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/metadata/google_books.py b/src/calibre/ebooks/metadata/google_books.py index 5a5e09234e..2e52bf020d 100644 --- a/src/calibre/ebooks/metadata/google_books.py +++ b/src/calibre/ebooks/metadata/google_books.py @@ -193,6 +193,7 @@ class ResultList(list): def search(title=None, author=None, publisher=None, isbn=None, min_viewability='none', verbose=False, max_results=40): br = browser() + br.set_handle_gzip(True) start, entries = 1, [] while start > 0 and len(entries) <= max_results: new, start = Query(title=title, author=author, publisher=publisher, diff --git a/src/calibre/ebooks/metadata/sources/amazon.py b/src/calibre/ebooks/metadata/sources/amazon.py index 9334d818ec..15282ad896 100644 --- a/src/calibre/ebooks/metadata/sources/amazon.py +++ b/src/calibre/ebooks/metadata/sources/amazon.py @@ -283,6 +283,7 @@ class Amazon(Source): touched_fields = frozenset(['title', 'authors', 'identifier:amazon', 'identifier:isbn', 'rating', 'comments', 'publisher', 'pubdate']) has_html_comments = True + supports_gzip_transfer_encoding = True AMAZON_DOMAINS = { 'com': _('US'), diff --git a/src/calibre/ebooks/metadata/sources/base.py b/src/calibre/ebooks/metadata/sources/base.py index d306a02bcb..d3b564204f 100644 --- a/src/calibre/ebooks/metadata/sources/base.py +++ b/src/calibre/ebooks/metadata/sources/base.py @@ -111,6 +111,12 @@ class Source(Plugin): #: Set this to True if your plugin return HTML formatted comments has_html_comments = False + #: Setting this to True means that the browser object will add + #: Accept-Encoding: gzip to all requests. This can speedup downloads + #: but make sure that the source actually supports gzip transfer encoding + #: correctly first + supports_gzip_transfer_encoding = False + def __init__(self, *args, **kwargs): Plugin.__init__(self, *args, **kwargs) self._isbn_to_identifier_cache = {} @@ -134,6 +140,8 @@ class Source(Plugin): def browser(self): if self._browser is None: self._browser = browser(user_agent=random_user_agent()) + if self.supports_gzip_transfer_encoding: + self._browser.set_handle_gzip(True) return self._browser.clone_browser() # }}} diff --git a/src/calibre/ebooks/metadata/sources/google.py b/src/calibre/ebooks/metadata/sources/google.py index 989320f710..21c99fdf46 100644 --- a/src/calibre/ebooks/metadata/sources/google.py +++ b/src/calibre/ebooks/metadata/sources/google.py @@ -160,6 +160,7 @@ class GoogleBooks(Source): touched_fields = frozenset(['title', 'authors', 'tags', 'pubdate', 'comments', 'publisher', 'identifier:isbn', 'rating', 'identifier:google']) # language currently disabled + supports_gzip_transfer_encoding = True GOOGLE_COVER = 'http://books.google.com/books?id=%s&printsec=frontcover&img=1' diff --git a/src/calibre/utils/browser.py b/src/calibre/utils/browser.py index 2f77ede6b3..f188d6b45a 100644 --- a/src/calibre/utils/browser.py +++ b/src/calibre/utils/browser.py @@ -38,10 +38,10 @@ class Browser(B): self._clone_actions['set_handle_equiv'] = ('set_handle_equiv', args, kwargs) - def set_handle_gzip(self, *args, **kwargs): - B.set_handle_gzip(self, *args, **kwargs) + def set_handle_gzip(self, handle): + self._set_handler('_gzip', handle) self._clone_actions['set_handle_gzip'] = ('set_handle_gzip', - args, kwargs) + (handle,), {}) def set_debug_redirect(self, *args, **kwargs): B.set_debug_redirect(self, *args, **kwargs)