openmedialibrary/oml/meta/utils.py
2016-01-08 16:15:10 +05:30

36 lines
942 B
Python

# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
import re
import stdnum.isbn
import ox
def normalize_isbn(value):
return ''.join([s for s in value if s.isdigit() or s == 'X'])
def find_isbns(text):
if isinstance(text, bytes):
text = text.decode()
matches = re.compile('\d[\d\-X\ ]+').findall(text)
matches = [normalize_isbn(value) for value in matches]
return [isbn for isbn in matches if stdnum.isbn.is_valid(isbn)
and len(isbn) in (10, 13)
and isbn not in (
'0' * 10,
'0' * 13,
)]
def get_language(lang):
return ox.iso.codeToLang(lang.split('-')[0]) or lang
def decode_html_data(data):
if isinstance(data, dict):
for key in data:
data[key] = decode_html_data(data[key])
elif isinstance(data, list):
data = [decode_html_data(v) for v in data]
elif isinstance(data, str):
data = ox.decode_html(data)
return data