~stub/ubuntu/precise/calibre/devel

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/metadata/covers.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2011-04-12 11:29:25 UTC
  • mfrom: (42.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110412112925-c7171kt2bb5rmft4
Tags: 0.7.50+dfsg-2
* debian/control: Build with libpodofo-dev to enable PDF metadata.
  (Closes: #619632)
* debian/control: Add libboost1.42-dev build dependency. Apparently it is
  needed in some setups. (Closes: #619807)
* debian/rules: Call dh_sip to generate a proper sip API dependency, to
  prevent crashes like #616372 for partial upgrades.
* debian/control: Bump python-qt4 dependency to >= 4.8.3-2, which reportedly
  fixes crashes on startup. (Closes: #619701, #620125)

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
__copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
6
6
__docformat__ = 'restructuredtext en'
7
7
 
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
15
15
 
16
16
from calibre.customize import Plugin
17
17
from calibre import browser, prints
18
 
from calibre.ebooks.BeautifulSoup import BeautifulSoup
19
18
from calibre.constants import preferred_encoding, DEBUG
20
19
 
21
20
class CoverDownload(Plugin):
75
74
class OpenLibraryCovers(CoverDownload): # {{{
76
75
    'Download covers from openlibrary.org'
77
76
 
 
77
    # See http://openlibrary.org/dev/docs/api/covers
 
78
 
78
79
    OPENLIBRARY = 'http://covers.openlibrary.org/b/isbn/%s-L.jpg?default=false'
79
80
    name = 'openlibrary.org covers'
80
81
    description = _('Download covers from openlibrary.org')
83
84
    def has_cover(self, mi, ans, timeout=5.):
84
85
        if not mi.isbn:
85
86
            return False
86
 
        br = browser()
 
87
        from calibre.ebooks.metadata.library_thing import get_browser
 
88
        br = get_browser()
87
89
        br.set_handle_redirect(False)
88
90
        try:
89
91
            br.open_novisit(HeadRequest(self.OPENLIBRARY%mi.isbn), timeout=timeout)
99
101
    def get_covers(self, mi, result_queue, abort, timeout=5.):
100
102
        if not mi.isbn:
101
103
            return
102
 
        br = browser()
 
104
        from calibre.ebooks.metadata.library_thing import get_browser
 
105
        br = get_browser()
103
106
        try:
104
107
            ans = br.open(self.OPENLIBRARY%mi.isbn, timeout=timeout).read()
105
108
            result_queue.put((True, ans, 'jpg', self.name))
112
115
 
113
116
# }}}
114
117
 
115
 
class LibraryThingCovers(CoverDownload): # {{{
 
118
class AmazonCovers(CoverDownload): # {{{
116
119
 
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'
120
123
 
121
 
    LIBRARYTHING = 'http://www.librarything.com/isbn/'
122
 
 
123
 
    def get_cover_url(self, isbn, br, timeout=5.):
124
 
 
125
 
        try:
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.'))
131
 
            raise err
132
 
        else:
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'})
137
 
            if url is None:
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')
142
 
            if url is None:
143
 
                raise Exception(_('LibraryThing.com server error. Try again later.'))
144
 
            url = re.sub(r'_S[XY]\d+', '', url['src'])
145
 
            return url
146
124
 
147
125
    def has_cover(self, mi, ans, timeout=5.):
148
 
        if not mi.isbn or not self.site_customization:
 
126
        if not mi.isbn:
149
127
            return False
150
 
        from calibre.ebooks.metadata.library_thing import get_browser, login
151
 
        br = get_browser()
152
 
        un, _, pw = self.site_customization.partition(':')
153
 
        login(br, un, pw)
 
128
        from calibre.ebooks.metadata.amazon import get_cover_url
 
129
        br = browser()
154
130
        try:
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')
157
133
            ans.set()
158
134
        except Exception, e:
159
135
            self.debug(e)
160
136
 
161
137
    def get_covers(self, mi, result_queue, abort, timeout=5.):
162
 
        if not mi.isbn or not self.site_customization:
 
138
        if not mi.isbn:
163
139
            return
164
 
        from calibre.ebooks.metadata.library_thing import get_browser, login
165
 
        br = get_browser()
166
 
        un, _, pw = self.site_customization.partition(':')
167
 
        login(br, un, pw)
 
140
        from calibre.ebooks.metadata.amazon import get_cover_url
 
141
        br = browser()
168
142
        try:
169
 
            url = self.get_cover_url(mi.isbn, br, timeout=timeout)
 
143
            url = get_cover_url(mi.isbn, br)
 
144
            if url is None:
 
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))
175
151
 
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>')
180
 
 
181
152
# }}}
182
153
 
183
154
def check_for_cover(mi, timeout=5.): # {{{