File: _openled.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 (36 lines) | stat: -rw-r--r-- 1,209 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
# -*- coding: utf-8 -*-
"""Query the Open Library for related ISBNs."""

import logging

from . import get_canonical_isbn, get_isbnlike
from .dev._bouth23 import u
from .dev.webquery import query as wquery

LOGGER = logging.getLogger(__name__)
UA = 'isbnlib (gzip)'
SERVICE_URL = 'http://openlibrary.org/query.json?type=/type/edition&'\
              '{selectors}'
CODES = 'isbn_13={isbn}&books='
ISBNS = '{code}&isbn_13='


def query(isbn):
    """Query the Open Library for related ISBNs."""
    try:
        data = wquery(
            SERVICE_URL.format(selectors=CODES.format(isbn=isbn)),
            user_agent=UA)
        codes = [rec['key'] for rec in data]
        isbnlikes = [isbn]
        for code in codes:
            txt = wquery(
                SERVICE_URL.format(selectors=ISBNS.format(code=code)),
                user_agent=UA,
                parser=None)
            isbnlikes.extend(get_isbnlike(txt))
        isbns = [get_canonical_isbn(isbnlike) for isbnlike in isbnlikes]
        isbns = sorted(list(set([u(n) for n in isbns if n])))  # qa: py26
    except Exception:  # pragma: no cover, qa: FIXME
        LOGGER.debug('No data from Open Library for isbn %s', isbn)
    return isbns