File: _metadata.py

package info (click to toggle)
isbnlib 3.9.3-1
  • links: PTS
  • area: main
  • in suites:
  • size: 596 kB
  • sloc: python: 4,575; makefile: 4
file content (34 lines) | stat: -rw-r--r-- 1,103 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
# -*- coding: utf-8 -*-
"""Query providers for metadata."""

from ._core import EAN13
from ._exceptions import NotRecognizedServiceError, NotValidISBNError
from .registry import services


def query(isbn, service='default', cache='default'):
    """Query worldcat.org, Google Books (JSON API), ... for metadata."""
    # validate inputs
    ean = EAN13(isbn)
    if not ean:
        raise NotValidISBNError(isbn)
    isbn = ean
    if service != 'default' and service not in services:  # pragma: no cover
        raise NotRecognizedServiceError(service)
    # set cache and get metadata
    if cache is None:  # pragma: no cover
        return services[service](isbn)
    if cache == 'default':
        from .registry import metadata_cache
        cache = metadata_cache
    key = isbn + service
    try:
        if cache[key]:
            return cache[key]
        else:  # pragma: no cover
            raise  # <-- IMPORTANT: "caches don't return error"!
    except Exception:
        meta = services[service](isbn)
        if meta:
            cache[key] = meta
        return meta if meta else None