Switch to using OpenLibrary as the primary source of book covers. Should speed up cover fetching. Also Fix #2316 (Calibre now fetches smaller images as covers)

This commit is contained in:
Kovid Goyal 2009-04-19 15:35:13 -07:00
parent 65a3b30a83
commit 40dd093352
4 changed files with 24 additions and 22 deletions

View File

@ -4,13 +4,15 @@ __copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
Fetch cover from LibraryThing.com based on ISBN number.
'''
import sys, socket, os, re, mechanize
import sys, socket, os, re
from calibre import browser as _browser
from calibre.utils.config import OptionParser
from calibre.ebooks.BeautifulSoup import BeautifulSoup
browser = None
OPENLIBRARY = 'http://covers.openlibrary.org/b/isbn/%s-L.jpg?default=false'
class LibraryThingError(Exception):
pass
@ -32,13 +34,19 @@ def login(username, password, force=True):
browser.submit()
def cover_from_isbn(isbn, timeout=5.):
def cover_from_isbn(isbn, timeout=5., username=None, password=None):
global browser
if browser is None:
browser = _browser()
_timeout = socket.getdefaulttimeout()
socket.setdefaulttimeout(timeout)
src = None
try:
return browser.open(OPENLIBRARY%isbn).read(), 'jpg'
except:
pass # Cover not found
if username and password:
login(username, password, force=False)
try:
src = browser.open('http://www.librarything.com/isbn/'+isbn).read().decode('utf-8', 'replace')
except Exception, err:
@ -55,7 +63,7 @@ def cover_from_isbn(isbn, timeout=5.):
url = url.find('img')
if url is None:
raise LibraryThingError(_('LibraryThing.com server error. Try again later.'))
url = re.sub(r'_SX\d+', '', url['src'])
url = re.sub(r'_S[XY]\d+', '', url['src'])
cover_data = browser.open(url).read()
return cover_data, url.rpartition('.')[-1]
finally:
@ -81,13 +89,8 @@ def main(args=sys.argv):
parser.print_help()
return 1
isbn = args[1]
if opts.username and opts.password:
try:
login(opts.username, opts.password)
except mechanize.FormNotFoundError:
raise LibraryThingError(_('LibraryThing.com server error. Try again later.'))
cover_data, ext = cover_from_isbn(isbn)
cover_data, ext = cover_from_isbn(isbn, username=opts.username,
password=opts.password)
if not ext:
ext = 'jpg'
oname = os.path.abspath(isbn+'.'+ext)

View File

@ -20,7 +20,7 @@ from calibre.gui2.dialogs.password import PasswordDialog
from calibre.gui2.widgets import ProgressIndicator
from calibre.ebooks import BOOK_EXTENSIONS
from calibre.ebooks.metadata import authors_to_sort_string, string_to_authors, authors_to_string
from calibre.ebooks.metadata.library_thing import login, cover_from_isbn
from calibre.ebooks.metadata.library_thing import cover_from_isbn
from calibre import islinux
from calibre.ebooks.metadata.meta import get_metadata
from calibre.utils.config import prefs
@ -60,9 +60,8 @@ class CoverFetcher(QThread):
return
self.isbn = results[0]
if self.username and self.password:
login(self.username, self.password, force=False)
self.cover_data = cover_from_isbn(self.isbn, timeout=self.timeout)[0]
self.cover_data = cover_from_isbn(self.isbn, timeout=self.timeout,
username=self.username, password=self.password)[0]
except Exception, e:
self.exception = e
self.traceback = traceback.format_exc()

View File

@ -589,7 +589,7 @@
<item>
<widget class="QPushButton" name="fetch_cover_button">
<property name="text">
<string>Fetch &amp;cover image from server</string>
<string>Download &amp;cover image</string>
</property>
</widget>
</item>

View File

@ -265,7 +265,7 @@ class BasicNewsRecipe(object, LoggingInterface):
def get_feeds(self):
'''
Return a list of :term:RSS feeds to fetch for this profile. Each element of the list
Return a list of :term:`RSS` feeds to fetch for this profile. Each element of the list
must be a 2-element tuple of the form (title, url). If title is None or an
empty string, the title from the feed is used. This method is useful if your recipe
needs to do some processing to figure out the list of feeds to download. If