code cleanup

This commit is contained in:
Hugh Rundle 2021-11-22 12:52:59 +11:00
parent 09c5a3861b
commit a9d921cc06
No known key found for this signature in database
GPG key ID: CD23D6039184286B
3 changed files with 33 additions and 35 deletions

View file

@ -4,6 +4,7 @@ import requests
from bookwyrm import activitypub, models from bookwyrm import activitypub, models
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"""
@ -18,11 +19,7 @@ def request_isni_data(search_index, search_term, max_records=5):
"recordPacking": "xml", "recordPacking": "xml",
"sortKeys": "RLV,pica,0,,", "sortKeys": "RLV,pica,0,,",
} }
result = requests.get( result = requests.get("http://isni.oclc.org/sru/", params=query_params, timeout=10)
"http://isni.oclc.org/sru/",
params=query_params,
timeout=10
)
# 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"
@ -59,8 +56,7 @@ def get_other_identifier(element, code):
code_of_source = source.find(".//codeOfSource") code_of_source = source.find(".//codeOfSource")
if ( if (
code_of_source is not None code_of_source is not None
and code_of_source.text == code.upper() and code_of_source.text.lower() == code.lower()
or code_of_source.text == code.lower()
): ):
return source.find(".//sourceIdentifier").text return source.find(".//sourceIdentifier").text
@ -115,9 +111,11 @@ def find_authors_by_name(name_string, description=False):
# some of the "titles" in ISNI are a little ...iffy # some of the "titles" in ISNI are a little ...iffy
# '@' is used by ISNI/OCLC to index the starting point ignoring stop words # '@' is used by ISNI/OCLC to index the starting point ignoring stop words
# (e.g. "The @Government of no one") # (e.g. "The @Government of no one")
title_elements = [e for e in titles if not e.text.replace('@', '').isnumeric()] title_elements = [
e for e in titles if not e.text.replace("@", "").isnumeric()
]
if len(title_elements): if len(title_elements):
author.bio = title_elements[0].text.replace('@', '') author.bio = title_elements[0].text.replace("@", "")
else: else:
author.bio = None author.bio = None
@ -155,13 +153,14 @@ def get_author_from_isni(isni):
viafId=viaf, viafId=viaf,
aliases=aliases, aliases=aliases,
bio=bio, bio=bio,
wikipediaLink=wikipedia wikipediaLink=wikipedia,
) )
return author return author
def build_author_from_isni(match_value): def build_author_from_isni(match_value):
"""Build dict with basic author details from ISNI or author name""" """Build basic author class object from ISNI URL"""
# if it is an isni value get the data # if it is an isni value get the data
if match_value.startswith("https://isni.org/isni/"): if match_value.startswith("https://isni.org/isni/"):

View file

@ -12,6 +12,7 @@ from django.utils.decorators import method_decorator
from django.views import View from django.views import View
from bookwyrm import book_search, forms, models from bookwyrm import book_search, forms, models
# from bookwyrm.activitypub.base_activity import ActivityObject # from bookwyrm.activitypub.base_activity import ActivityObject
from bookwyrm.utils.isni import ( from bookwyrm.utils.isni import (
find_authors_by_name, find_authors_by_name,
@ -72,8 +73,7 @@ class EditBook(View):
) )
isni_authors = find_authors_by_name( isni_authors = find_authors_by_name(
author, author, description=True
description=True
) # find matches from ISNI API ) # find matches from ISNI API
# dedupe isni authors we already have in the DB # dedupe isni authors we already have in the DB
@ -187,12 +187,11 @@ class ConfirmEditBook(View):
author_object = build_author_from_isni(isni_match) author_object = build_author_from_isni(isni_match)
# with author data class from isni id # with author data class from isni id
if "author" in author_object: if "author" in author_object:
# TESTING skeleton = models.Author.objects.create(
skeleton = models.Author.objects.create(name=author_object["author"].name) name=author_object["author"].name
)
author = author_object["author"].to_model( author = author_object["author"].to_model(
model=models.Author, model=models.Author, overwrite=True, instance=skeleton
overwrite=True,
instance=skeleton
) )
else: else:
# or it's just a name # or it's just a name