File: _ext.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 (93 lines) | stat: -rw-r--r-- 2,701 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""Extra methods."""

import re

from ._core import EAN13
from ._cover import cover as gcover
from ._desc import goo_desc
from ._editions import editions as eds
from ._gwords import goos
from ._infogroup import infogroup
from ._metadata import query
from ._msk import msk
from .dev._bouth23 import b2u3, u
from .dev.helpers import File, cutoff_tokens, last_first


def mask(isbn, separator='-'):
    """`Mask` a canonical ISBN."""
    return msk(isbn, separator)


def meta(isbn, service='default', cache='default'):
    """Get metadata from worldcat.org ('wcat'), Google Books ('goob') , ..."""
    service = service if service else 'default'
    return query(isbn, service, cache) if isbn else None


def info(isbn):
    """Get language or country assigned to this ISBN."""
    return infogroup(isbn)


def editions(isbn, service='merge'):
    """Return the list of ISBNs of editions related with this ISBN.

    'service' can have the values:
    'any', 'merge' (default), 'wcat', 'openl' and 'thingl'
    """
    return eds(isbn, service)


def isbn_from_words(words):
    """Return the most probable ISBN from a list of words."""
    return goos(words)


def doi(isbn):
    """Return a DOI's ISBN-A from a ISBN-13."""
    return "10.%s.%s%s/%s%s" % \
           tuple(msk(EAN13(isbn), '-').split('-'))


def ren(fp):
    """Rename a file using metadata from an ISBN in his filename."""
    cfp = File(fp)
    isbn = EAN13(cfp.name)
    if not isbn:  # pragma: no cover
        return None
    data = meta(isbn)
    author = data.get('Authors', u('UNKNOWN'))
    if author != u('UNKNOWN'):  # pragma: no cover
        author = last_first(author[0])['last']
    year = data.get('Year', u('UNKNOWN'))
    maxlen = 98 - (20 + len(author) + len(year))
    title = data.get('Title', u('UNKNOWN'))
    if title != u('UNKNOWN'):
        regex1 = re.compile(r'[.,_!?/\\]')
        regex2 = re.compile(r'\s\s+')
        title = regex1.sub(' ', title)
        title = regex2.sub(' ', title)
        title = title.strip()
    if title == u('UNKNOWN') or not title:  # pragma: no cover
        return None
    if ' ' in title:  # pragma: no cover
        tokens = title.split(' ')
        stitle = cutoff_tokens(tokens, maxlen)
        title = ' '.join(stitle)
    isbn13 = data.get('ISBN-13', u('UNKNOWN'))
    new_name = "%s%s_%s_%s" % (author, year, title, isbn13)
    return cfp.baserename(b2u3(new_name + cfp.ext))


def cover(isbn):
    """Get the img urls of the cover of the ISBN."""
    isbn = EAN13(isbn)
    return gcover(isbn) if isbn else None


def desc(isbn):
    """Return a descripion of the ISBN."""
    isbn = EAN13(isbn)
    return goo_desc(isbn) if isbn else None