1#!/usr/local/bin/python3.8
2# vim:fileencoding=UTF-8:ts=4:sw=4:sta:et:sts=4:ai
3from __future__ import absolute_import, division, print_function, unicode_literals
4
5__license__   = 'GPL v3'
6__copyright__ = '2011, Kovid Goyal <kovid@kovidgoyal.net>'
7__docformat__ = 'restructuredtext en'
8
9from calibre.ebooks.metadata.sources.base import Source
10
11
12class OpenLibrary(Source):
13
14    name = 'Open Library'
15    version = (1, 0, 0)
16    minimum_calibre_version = (2, 80, 0)
17    description = _('Downloads covers from The Open Library')
18
19    capabilities = frozenset(['cover'])
20
21    OPENLIBRARY = 'https://covers.openlibrary.org/b/isbn/%s-L.jpg?default=false'
22
23    def download_cover(self, log, result_queue, abort,
24            title=None, authors=None, identifiers={}, timeout=30, get_best_cover=False):
25        if 'isbn' not in identifiers:
26            return
27        isbn = identifiers['isbn']
28        br = self.browser
29        try:
30            ans = br.open_novisit(self.OPENLIBRARY%isbn, timeout=timeout).read()
31            result_queue.put((self, ans))
32        except Exception as e:
33            if callable(getattr(e, 'getcode', None)) and e.getcode() == 404:
34                log.error('No cover for ISBN: %r found'%isbn)
35            else:
36                log.exception('Failed to download cover for ISBN:', isbn)
37