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