File: _cover.py

package info (click to toggle)
isbnlib 3.9.3-1
  • links: PTS
  • area: main
  • in suites: buster
  • size: 596 kB
  • sloc: python: 4,575; makefile: 4
file content (39 lines) | stat: -rw-r--r-- 1,227 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# -*- coding: utf-8 -*-
"""Get image links of the book's cover."""

import logging

from .dev.webquery import query as wquery

LOGGER = logging.getLogger(__name__)

UA = "isbnlib (gzip)"
SERVICE_URL = 'https://www.googleapis.com/books/v1/volumes?q=isbn+{isbn}'\
              '&fields=items/volumeInfo(imageLinks)&maxResults=1'


def cover(isbn):
    """Main entry point for cover."""
    from .registry import metadata_cache  # <-- dynamic
    # check the cache first
    cache = metadata_cache
    if cache:  # pragma: no cover
        key = 'img-url-go-' + isbn
        try:  # pragma: no cover
            if cache[key]:
                return cache[key]
            else:
                raise KeyError  # <-- IMPORTANT: caches don't return error!
        except KeyError:  # pragma: no cover
            pass
    # request to the web service
    data = wquery(SERVICE_URL.format(isbn=isbn), user_agent=UA)
    try:
        lnks = data['items'][0]['volumeInfo']['imageLinks']
        # put in cache
        if cache and lnks:  # pragma: no cover
            cache[key] = lnks
        return lnks
    except (KeyError, IndexError):  # pragma: no cover
        LOGGER.debug('No cover img data for %s', isbn)
    return None