Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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
def desc(isbn):
"""Return a descripion of the ISBN."""
isbn = EAN13(isbn)
return goo_desc(isbn) if isbn else ''
def __init__(self, isbnlike):
try:
self.ean13 = EAN13(isbnlike)
if not self.ean13:
raise NotValidISBNError(isbnlike)
except Exception:
LOGGER.debug('error: %s is not a valid ISBN', isbnlike)
raise NotValidISBNError(isbnlike)
self.canonical = canonical(isbnlike)
self.isbn13 = mask(self.ean13) or self.ean13
self.isbn10 = mask(to_isbn10(self.ean13)) or to_isbn10(self.ean13)
self.doi = doi(self.ean13)
self.info = info(self.ean13)
self.issued = '-' in self.isbn13
def editions(isbn, service='merge'):
"""Return the list of ISBNs of editions related with this ISBN."""
isbn = EAN13(isbn)
if not isbn:
LOGGER.critical('%s is not a valid ISBN', isbn)
raise NotValidISBNError(isbn)
if service not in PROVIDERS:
LOGGER.critical('%s is not a recognized editions provider', service)
raise NotRecognizedServiceError(service)
return get_editions(isbn, service)
def msk(isbn, separator='-'):
"""Transform a canonical ISBN to a `masked` one.
`Mask` the ISBN, separating by identifier
ISBN-10 identifiers: country-publisher-title-check
Used the iterative version of the `sliding-window` algorithm.
Not pretty, but fast! Lines 42-52 implement the search loop:
O(n) for n (number of keys),
with data structure 'ranges' (see data4mask.py)
"""
if not isbn:
return ''
ib = canonical(isbn)
ean = EAN13(ib)
if len(ib) not in (10, 13) or not ean:
LOGGER.critical('%s is not a valid ISBN', isbn)
raise NotValidISBNError(isbn)
isbn10 = False
if len(ib) == 10:
check10 = ib[-1]
ib = to_isbn13(ib)
isbn10 = True
idx = None
check = ib[-1:] # <-- HACK!
cur = 3
igi = cur
buf = ib[igi:cur + 1]
group = ib[0:cur] + '-' + buf
def infogroup(isbn):
"""Get the Language/Country of this ISBN."""
# if isbn is not a valid ISBN this def can give a wrong result!
# => clean and validate
isbn = EAN13(isbn)
if not isbn:
LOGGER.critical('%s is not a valid ISBN', isbn)
raise NotValidISBNError(isbn)
# put isbn in the form 978-...
prefix = isbn[0:3] + '-'
isbn = prefix + isbn[3:]
dtxt = countries
idents = identifiers
ixi, ixf = 4, 5
for ident in idents:
iid = prefix + isbn[ixi:ixf]
ixf += 1
# stop if identifier is found
if iid in ident:
return dtxt[iid]
LOGGER.debug('Identifier not found for %s (probably not issued yet!)',
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)
data = []
data.append(EAN13(isbn))
return set(data)
def query(isbn, service='default'):
"""Query services like Google Books (JSON API), ... for metadata."""
# validate inputs
ean = EAN13(isbn)
if not ean:
LOGGER.critical('%s is not a valid ISBN', isbn)
raise NotValidISBNError(isbn)
isbn = ean
# only import when needed
from .registry import services
if service != 'default' and service not in services: # pragma: no cover
LOGGER.critical('%s is not a valid service', service)
raise NotRecognizedServiceError(service)
meta = services[service](isbn)
return meta or {}
def cover(isbn):
"""Get the img urls of the cover of the ISBN."""
isbn = EAN13(isbn)
return gcover(isbn) if isbn else {}
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]