2021-10-29 10:14:32 +00:00
|
|
|
"""ISNI author checking utilities"""
|
2021-10-29 05:12:31 +00:00
|
|
|
import xml.etree.ElementTree as ET
|
2021-10-29 10:14:32 +00:00
|
|
|
import requests
|
2021-10-29 05:12:31 +00:00
|
|
|
|
2021-11-21 08:55:55 +00:00
|
|
|
from bookwyrm import activitypub, models
|
2021-11-16 09:40:22 +00:00
|
|
|
|
2021-11-22 01:52:59 +00:00
|
|
|
|
2021-10-31 23:20:19 +00:00
|
|
|
def request_isni_data(search_index, search_term, max_records=5):
|
2021-10-31 06:58:15 +00:00
|
|
|
"""Request data from the ISNI API"""
|
2021-10-29 05:12:31 +00:00
|
|
|
|
2021-11-16 09:35:47 +00:00
|
|
|
search_string = f'{search_index}="{search_term}"'
|
|
|
|
query_params = {
|
2021-11-16 09:40:22 +00:00
|
|
|
"query": search_string,
|
|
|
|
"version": "1.1",
|
|
|
|
"operation": "searchRetrieve",
|
|
|
|
"recordSchema": "isni-b",
|
|
|
|
"maximumRecords": max_records,
|
|
|
|
"startRecord": "1",
|
|
|
|
"recordPacking": "xml",
|
|
|
|
"sortKeys": "RLV,pica,0,,",
|
2021-11-16 09:35:47 +00:00
|
|
|
}
|
2021-12-01 10:02:25 +00:00
|
|
|
result = requests.get("http://isni.oclc.org/sru/", params=query_params, timeout=15)
|
2021-10-29 10:00:35 +00:00
|
|
|
# the OCLC ISNI server asserts the payload is encoded
|
|
|
|
# in latin1, but we know better
|
2021-10-29 10:14:32 +00:00
|
|
|
result.encoding = "utf-8"
|
2021-10-31 06:58:15 +00:00
|
|
|
return result.text
|
|
|
|
|
2021-10-31 09:48:47 +00:00
|
|
|
|
2021-10-31 06:58:15 +00:00
|
|
|
def make_name_string(element):
|
|
|
|
"""create a string of form 'personal_name surname'"""
|
|
|
|
|
2021-10-31 09:48:47 +00:00
|
|
|
# NOTE: this will often be incorrect, many naming systems
|
2021-10-31 06:58:15 +00:00
|
|
|
# list "surname" before personal name
|
|
|
|
forename = element.find(".//forename")
|
|
|
|
surname = element.find(".//surname")
|
|
|
|
if forename is not None:
|
2021-10-31 09:48:47 +00:00
|
|
|
return "".join([forename.text, " ", surname.text])
|
2021-10-31 06:58:15 +00:00
|
|
|
return surname.text
|
|
|
|
|
2021-10-31 09:48:47 +00:00
|
|
|
|
2021-10-31 06:58:15 +00:00
|
|
|
def get_other_identifier(element, code):
|
|
|
|
"""Get other identifiers associated with an author from their ISNI record"""
|
|
|
|
|
|
|
|
identifiers = element.findall(".//otherIdentifierOfIdentity")
|
|
|
|
for section_head in identifiers:
|
|
|
|
if (
|
2021-10-31 09:48:47 +00:00
|
|
|
section_head.find(".//type") is not None
|
2021-10-31 06:58:15 +00:00
|
|
|
and section_head.find(".//type").text == code
|
2021-10-31 09:48:47 +00:00
|
|
|
and section_head.find(".//identifier") is not None
|
2021-10-31 06:58:15 +00:00
|
|
|
):
|
|
|
|
return section_head.find(".//identifier").text
|
2021-11-21 08:55:55 +00:00
|
|
|
|
|
|
|
# if we can't find it in otherIdentifierOfIdentity,
|
|
|
|
# try sources
|
|
|
|
for source in element.findall(".//sources"):
|
|
|
|
code_of_source = source.find(".//codeOfSource")
|
2021-11-22 02:01:58 +00:00
|
|
|
if code_of_source is not None and code_of_source.text.lower() == code.lower():
|
2021-11-21 08:55:55 +00:00
|
|
|
return source.find(".//sourceIdentifier").text
|
|
|
|
|
2021-10-31 23:20:19 +00:00
|
|
|
return ""
|
2021-10-31 09:48:47 +00:00
|
|
|
|
2021-10-31 06:58:15 +00:00
|
|
|
|
|
|
|
def get_external_information_uri(element, match_string):
|
|
|
|
"""Get URLs associated with an author from their ISNI record"""
|
|
|
|
|
|
|
|
sources = element.findall(".//externalInformation")
|
|
|
|
for source in sources:
|
2021-11-21 08:55:55 +00:00
|
|
|
information = source.find(".//information")
|
2021-10-31 06:58:15 +00:00
|
|
|
uri = source.find(".//URI")
|
2021-11-21 08:55:55 +00:00
|
|
|
if (
|
2021-11-22 01:52:59 +00:00
|
|
|
uri is not None
|
|
|
|
and information is not None
|
|
|
|
and information.text.lower() == match_string.lower()
|
|
|
|
):
|
2021-10-31 09:48:47 +00:00
|
|
|
return uri.text
|
2021-10-31 23:20:19 +00:00
|
|
|
return ""
|
2021-10-31 06:58:15 +00:00
|
|
|
|
2021-10-31 09:48:47 +00:00
|
|
|
|
2021-11-21 21:49:22 +00:00
|
|
|
def find_authors_by_name(name_string, description=False):
|
2021-10-31 06:58:15 +00:00
|
|
|
"""Query the ISNI database for possible author matches by name"""
|
|
|
|
|
|
|
|
payload = request_isni_data("pica.na", name_string)
|
2021-10-29 10:00:35 +00:00
|
|
|
# parse xml
|
2021-10-29 05:12:31 +00:00
|
|
|
root = ET.fromstring(payload)
|
|
|
|
# build list of possible authors
|
|
|
|
possible_authors = []
|
2021-10-29 10:14:32 +00:00
|
|
|
for element in root.iter("responseRecord"):
|
|
|
|
personal_name = element.find(".//forename/..")
|
2021-10-31 06:58:15 +00:00
|
|
|
if not personal_name:
|
|
|
|
continue
|
2021-10-29 05:12:31 +00:00
|
|
|
|
2021-11-21 21:49:22 +00:00
|
|
|
author = get_author_from_isni(element.find(".//isniUnformatted").text)
|
|
|
|
|
|
|
|
if bool(description):
|
2021-11-22 01:15:06 +00:00
|
|
|
|
|
|
|
titles = []
|
|
|
|
# prefer title records from LoC+ coop, Australia, Ireland, or Singapore
|
|
|
|
# in that order
|
|
|
|
for source in ["LCNACO", "NLA", "N6I", "NLB"]:
|
|
|
|
for parent in element.findall(f'.//titleOfWork/[@source="{source}"]'):
|
|
|
|
titles.append(parent.find(".//title"))
|
|
|
|
for parent in element.findall(f'.//titleOfWork[@subsource="{source}"]'):
|
|
|
|
titles.append(parent.find(".//title"))
|
|
|
|
# otherwise just grab the first title listing
|
|
|
|
titles.append(element.find(".//title"))
|
|
|
|
|
|
|
|
if titles is not None:
|
2021-11-22 01:52:59 +00:00
|
|
|
# some of the "titles" in ISNI are a little ...iffy
|
|
|
|
# '@' is used by ISNI/OCLC to index the starting point ignoring stop words
|
2021-11-22 01:15:06 +00:00
|
|
|
# (e.g. "The @Government of no one")
|
2021-11-22 01:52:59 +00:00
|
|
|
title_elements = [
|
|
|
|
e for e in titles if not e.text.replace("@", "").isnumeric()
|
|
|
|
]
|
2021-11-22 01:15:06 +00:00
|
|
|
if len(title_elements):
|
2021-11-22 01:52:59 +00:00
|
|
|
author.bio = title_elements[0].text.replace("@", "")
|
2021-11-22 01:15:06 +00:00
|
|
|
else:
|
|
|
|
author.bio = None
|
2021-11-21 08:55:55 +00:00
|
|
|
|
2021-10-31 06:58:15 +00:00
|
|
|
possible_authors.append(author)
|
2021-10-29 05:12:31 +00:00
|
|
|
|
|
|
|
return possible_authors
|
2021-10-31 06:58:15 +00:00
|
|
|
|
2021-10-31 09:48:47 +00:00
|
|
|
|
2021-11-21 08:55:55 +00:00
|
|
|
def get_author_from_isni(isni):
|
2021-10-31 23:20:19 +00:00
|
|
|
"""Find data to populate a new author record from their ISNI"""
|
2021-10-31 06:58:15 +00:00
|
|
|
|
|
|
|
payload = request_isni_data("pica.isn", isni)
|
|
|
|
# parse xml
|
|
|
|
root = ET.fromstring(payload)
|
|
|
|
# there should only be a single responseRecord
|
|
|
|
# but let's use the first one just in case
|
|
|
|
element = root.find(".//responseRecord")
|
2021-11-21 08:55:55 +00:00
|
|
|
name = make_name_string(element.find(".//forename/.."))
|
|
|
|
viaf = get_other_identifier(element, "viaf")
|
|
|
|
# use a set to dedupe aliases in ISNI
|
|
|
|
aliases = set()
|
|
|
|
aliases_element = element.findall(".//personalNameVariant")
|
|
|
|
for entry in aliases_element:
|
|
|
|
aliases.add(make_name_string(entry))
|
|
|
|
# aliases needs to be list not set
|
|
|
|
aliases = list(aliases)
|
2021-10-31 06:58:15 +00:00
|
|
|
bio = element.find(".//nameTitle")
|
2021-11-21 08:55:55 +00:00
|
|
|
bio = bio.text if bio is not None else ""
|
|
|
|
wikipedia = get_external_information_uri(element, "Wikipedia")
|
|
|
|
|
|
|
|
author = activitypub.Author(
|
2021-11-22 01:52:59 +00:00
|
|
|
id=element.find(".//isniURI").text,
|
|
|
|
name=name,
|
|
|
|
isni=isni,
|
|
|
|
viafId=viaf,
|
|
|
|
aliases=aliases,
|
|
|
|
bio=bio,
|
|
|
|
wikipediaLink=wikipedia,
|
2021-11-21 08:55:55 +00:00
|
|
|
)
|
2021-11-01 00:34:32 +00:00
|
|
|
|
2021-11-21 08:55:55 +00:00
|
|
|
return author
|
2021-11-01 00:39:37 +00:00
|
|
|
|
2021-11-22 01:52:59 +00:00
|
|
|
|
2021-11-21 08:55:55 +00:00
|
|
|
def build_author_from_isni(match_value):
|
2021-11-22 01:52:59 +00:00
|
|
|
"""Build basic author class object from ISNI URL"""
|
2021-11-01 00:34:32 +00:00
|
|
|
|
|
|
|
# if it is an isni value get the data
|
2021-11-21 21:49:22 +00:00
|
|
|
if match_value.startswith("https://isni.org/isni/"):
|
|
|
|
isni = match_value.replace("https://isni.org/isni/", "")
|
2021-11-22 01:52:59 +00:00
|
|
|
return {"author": get_author_from_isni(isni)}
|
2021-11-01 00:34:32 +00:00
|
|
|
# otherwise it's a name string
|
2021-11-21 08:55:55 +00:00
|
|
|
return {}
|
2021-11-01 08:50:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
def augment_author_metadata(author, isni):
|
|
|
|
"""Update any missing author fields from ISNI data"""
|
2021-11-21 08:55:55 +00:00
|
|
|
|
|
|
|
isni_author = get_author_from_isni(isni)
|
|
|
|
isni_author.to_model(model=models.Author, instance=author, overwrite=False)
|
|
|
|
|
2021-11-22 01:52:59 +00:00
|
|
|
# we DO want to overwrite aliases because we're adding them to the
|
2021-11-21 08:55:55 +00:00
|
|
|
# existing aliases and ISNI will usually have more.
|
2021-11-21 21:49:22 +00:00
|
|
|
# We need to dedupe because ISNI records often have lots of dupe aliases
|
2021-11-21 22:25:31 +00:00
|
|
|
aliases = set(isni_author.aliases)
|
2021-11-01 09:04:25 +00:00
|
|
|
for alias in author.aliases:
|
|
|
|
aliases.add(alias)
|
2021-11-01 08:50:49 +00:00
|
|
|
author.aliases = list(aliases)
|
|
|
|
author.save()
|