File: _thinged.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 (37 lines) | stat: -rw-r--r-- 1,094 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
# -*- coding: utf-8 -*-
"""Query the ThingISBN api (Library Thing) for related ISBNs."""

import logging
from xml.dom.minidom import parseString

from ._core import EAN13
from .dev.webquery import query as wquery

LOGGER = logging.getLogger(__name__)
UA = 'isbnlib (gzip)'
SERVICE_URL = 'http://www.librarything.com/api/thingISBN/{isbn}'


def _get_text(topnode):
    """Get the text values in the child nodes."""
    text = ""
    for node in topnode.childNodes:
        if node.nodeType == node.TEXT_NODE:
            text = text + node.data
    return text


def parser_thinged(xml):
    """Parse the response from the ThingISBN service."""
    dom = parseString(xml)
    nodes = dom.getElementsByTagName("idlist")[0].getElementsByTagName("isbn")
    return [EAN13(_get_text(isbn)) for isbn in nodes]


def query(isbn):
    """Query the ThingISBN service for related ISBNs."""
    data = wquery(
        SERVICE_URL.format(isbn=isbn), user_agent=UA, parser=parser_thinged)
    if not data:  # pragma: no cover
        LOGGER.debug('No data from ThingISBN for isbn %s', isbn)
    return data