isni utils cleanup

Utilises the requests module's built in functionality to pass params as a dict
which is url encoded by requests.
This commit is contained in:
Hugh Rundle 2021-11-16 20:35:47 +11:00
parent c3ba7ba547
commit 9ca18d9cd4
No known key found for this signature in database
GPG key ID: CD23D6039184286B

View file

@ -2,30 +2,21 @@
import xml.etree.ElementTree as ET
import requests
def url_stringify(string):
"""replace spaces for url encoding"""
# TODO: this is very lazy and incomplete
return string.replace(" ", "+")
def request_isni_data(search_index, search_term, max_records=5):
"""Request data from the ISNI API"""
search_string = url_stringify(search_term)
query_parts = [
"http://isni.oclc.org/sru/?query=",
search_index,
"+%3D+%22",
search_string,
"%22&version=1.1&operation=searchRetrieve&recordSchema=isni-b",
"&maximumRecords=",
str(max_records),
"&startRecord=1&recordPacking=xml&sortKeys=RLV%2Cpica%2C0%2C%2C",
]
query_url = "".join(query_parts)
result = requests.get(query_url)
search_string = f'{search_index}="{search_term}"'
query_params = {
"query" : search_string,
"version" : "1.1",
"operation" : "searchRetrieve",
"recordSchema" : "isni-b",
"maximumRecords" : max_records,
"startRecord" : "1",
"recordPacking" : "xml",
"sortKeys" : "RLV,pica,0,,"
}
result = requests.get("http://isni.oclc.org/sru/", params=query_params)
# the OCLC ISNI server asserts the payload is encoded
# in latin1, but we know better
result.encoding = "utf-8"