File: _desc.py

package info (click to toggle)
isbnlib 3.9.3-1
  • links: PTS
  • area: main
  • in suites: bullseye, buster
  • size: 596 kB
  • sloc: python: 4,575; makefile: 4
file content (42 lines) | stat: -rw-r--r-- 1,342 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
40
41
42
# -*- coding: utf-8 -*-
"""Return a small description of the book."""

import logging
from json import loads
from textwrap import fill

from .dev.webservice import query as wsquery

LOGGER = logging.getLogger(__name__)

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


def goo_desc(isbn):
    """Get description from Google Books api."""
    from .registry import metadata_cache  # <-- dynamic
    cache = metadata_cache
    if cache is not None:  # pragma: no cover
        key = 'desc-go-' + isbn
        try:
            if cache[key]:
                return cache[key]
            else:
                raise KeyError  # <-- IMPORTANT: caches don't return error!
        except KeyError:
            pass
    url = SERVICE_URL.format(isbn=isbn)
    content = wsquery(url, user_agent=UA)
    try:
        content = loads(content)
        content = content['items'][0]['volumeInfo']['description']
        # TODO don't format content here!
        content = fill(content, width=75) if content else None
        if content and cache is not None:  # pragma: no cover
            cache[key] = content
        return content
    except:  # pragma: no cover
        LOGGER.debug('No description for %s', isbn)