Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
matches = regex.findall(line)
if len(matches) > 0:
logger.debug('Unchecked [' + ' '.join(matches) + ']')
for match in matches:
match = match.strip()
match = match.replace('i', 'I')
match = match.replace('s', 'S')
match = match.replace('b', 'B')
match = match.replace('n', 'N')
match = re.sub(r'\x20', '', match)
match = re.sub(r'ISBN', 'ISBN\x20', match)
# logger.debug('match= ' + match)
if match not in self.SPECIAL_ISBN:
try:
# logger.debug('isbn= ' + isbn)
isbn = isbnlib.get_canonical_isbn(match)
except:
logger.error('Error in isbnlib while calling get_canonical_isbn')
else:
if isbn:
isbns.append(isbn)
return isbns
def extract_from_text(text):
"""
Extract ISBNs from a text.
:param text: Some text.
:returns: A list of canonical ISBNs found in the text.
>>> extract_from_text("978-3-16-148410-0 9783161484100 9783161484100aa abcd 0136091814 0136091812 9780136091817 123456789X")
['9783161484100', '9783161484100', '9783161484100', '0136091814', '123456789X']
"""
isbns = [isbnlib.get_canonical_isbn(isbn)
for isbn in isbnlib.get_isbnlike(text)]
return [i for i in isbns if i is not None]
def number(self):
return get_canonical_isbn(self._number) \
if self._number else None
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 = {u(get_canonical_isbn(isbnlike)) for isbnlike in isbnlikes}
except Exception as ex: # pragma: no cover
LOGGER.debug('No data from Open Library for isbn %s -- %s', isbn,
str(ex))
return {get_canonical_isbn(isbn)}
return isbns
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 = {u(get_canonical_isbn(isbnlike)) for isbnlike in isbnlikes}
except Exception as ex: # pragma: no cover
LOGGER.debug('No data from Open Library for isbn %s -- %s', isbn,
str(ex))
return {get_canonical_isbn(isbn)}
return isbns
def get_canonical_isbn2(self, line):
# logger.debug('[ ' + line + ' ]')
isbns = []
matches = isbnlib.get_isbnlike(line)
if len(matches) > 0:
logger.debug('Unchecked [' + ' '.join(matches) + ']')
for match in matches:
if match not in self.SPECIAL_ISBN and not any(match in s for s in isbns):
try:
# logger.debug('isbn= ' + isbn)
isbn = isbnlib.get_canonical_isbn(match)
except:
logger.error('Error in isbnlib while calling get_canonical_isbn')
else:
if isbn:
isbns.append(isbn)
return isbns
>>> is_valid("0136091814")
True
>>> is_valid("0136091812")
False
>>> is_valid("9780136091817")
False
>>> is_valid("123456789X")
True
"""
return (
(not isbnlib.notisbn(isbn_id)) and (
isbnlib.get_canonical_isbn(isbn_id) == isbn_id or
isbnlib.mask(isbnlib.get_canonical_isbn(isbn_id)) == isbn_id)
)
>>> is_valid("0136091814")
True
>>> is_valid("0136091812")
False
>>> is_valid("9780136091817")
False
>>> is_valid("123456789X")
True
"""
return (
(not isbnlib.notisbn(isbn_id)) and (
isbnlib.get_canonical_isbn(isbn_id) == isbn_id or
isbnlib.mask(isbnlib.get_canonical_isbn(isbn_id)) == isbn_id)
)