5
5
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
6
6
__docformat__ = 'restructuredtext en'
8
import traceback, socket, re, sys
8
import traceback, socket, sys
9
9
from functools import partial
10
10
from threading import Thread, Event
11
11
from Queue import Queue, Empty
115
class LibraryThingCovers(CoverDownload): # {{{
118
class AmazonCovers(CoverDownload): # {{{
117
name = 'librarything.com covers'
118
description = _('Download covers from librarything.com')
120
name = 'amazon.com covers'
121
description = _('Download covers from amazon.com')
119
122
author = 'Kovid Goyal'
121
LIBRARYTHING = 'http://www.librarything.com/isbn/'
123
def get_cover_url(self, isbn, br, timeout=5.):
126
src = br.open_novisit('http://www.librarything.com/isbn/'+isbn,
127
timeout=timeout).read().decode('utf-8', 'replace')
128
except Exception, err:
129
if isinstance(getattr(err, 'args', [None])[0], socket.timeout):
130
err = Exception(_('LibraryThing.com timed out. Try again later.'))
133
if '/wiki/index.php/HelpThing:Verify' in src:
134
raise Exception('LibraryThing is blocking calibre.')
135
s = BeautifulSoup(src)
136
url = s.find('td', attrs={'class':'left'})
138
if s.find('div', attrs={'class':'highloadwarning'}) is not None:
139
raise Exception(_('Could not fetch cover as server is experiencing high load. Please try again later.'))
140
raise Exception(_('ISBN: %s not found')%isbn)
141
url = url.find('img')
143
raise Exception(_('LibraryThing.com server error. Try again later.'))
144
url = re.sub(r'_S[XY]\d+', '', url['src'])
147
125
def has_cover(self, mi, ans, timeout=5.):
148
if not mi.isbn or not self.site_customization:
150
from calibre.ebooks.metadata.library_thing import get_browser, login
152
un, _, pw = self.site_customization.partition(':')
128
from calibre.ebooks.metadata.amazon import get_cover_url
155
self.get_cover_url(mi.isbn, br, timeout=timeout)
131
get_cover_url(mi.isbn, br)
156
132
self.debug('cover for', mi.isbn, 'found')
158
134
except Exception, e:
161
137
def get_covers(self, mi, result_queue, abort, timeout=5.):
162
if not mi.isbn or not self.site_customization:
164
from calibre.ebooks.metadata.library_thing import get_browser, login
166
un, _, pw = self.site_customization.partition(':')
140
from calibre.ebooks.metadata.amazon import get_cover_url
169
url = self.get_cover_url(mi.isbn, br, timeout=timeout)
143
url = get_cover_url(mi.isbn, br)
145
raise ValueError('No cover found for ISBN: %s'%mi.isbn)
170
146
cover_data = br.open_novisit(url).read()
171
147
result_queue.put((True, cover_data, 'jpg', self.name))
172
148
except Exception, e:
173
149
result_queue.put((False, self.exception_to_string(e),
174
150
traceback.format_exc(), self.name))
176
def customization_help(self, gui=False):
177
ans = _('To use librarything.com you must sign up for a %sfree account%s '
178
'and enter your username and password separated by a : below.')
179
return '<p>'+ans%('<a href="http://www.librarything.com">', '</a>')
183
154
def check_for_cover(mi, timeout=5.): # {{{