Skip to content

Commit

Permalink
get_marc_record_from_ia: use existing metadata if present (#8332)
Browse files Browse the repository at this point in the history
* get_marc_record_from_ia: use existing metadata if present
  • Loading branch information
scottbarnes authored Oct 19, 2023
1 parent a6ebcf2 commit 01f6c6d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
14 changes: 10 additions & 4 deletions openlibrary/catalog/get_ia.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ def urlopen_keep_trying(url: str, headers=None, **kwargs):
sleep(2)


def get_marc_record_from_ia(identifier: str) -> MarcBinary | MarcXml | None:
def get_marc_record_from_ia(
identifier: str, ia_metadata: dict | None = None
) -> MarcBinary | MarcXml | None:
"""
Takes IA identifiers and returns MARC record instance.
Takes IA identifiers and optional IA metadata and returns MARC record instance.
08/2018: currently called by openlibrary/plugins/importapi/code.py
when the /api/import/ia endpoint is POSTed to.
:param ia_metadata: The full ia metadata; e.g. https://archive.org/metadata/goody,
not https://archive.org/metadata/goody/metadata
"""
metadata = ia.get_metadata(identifier)
filenames = metadata['_filenames']
if ia_metadata is None:
ia_metadata = ia.get_metadata(identifier)
filenames = ia_metadata['_filenames'] # type: ignore[index]

marc_xml_filename = identifier + '_marc.xml'
marc_bin_filename = identifier + '_meta.mrc'
Expand Down
4 changes: 3 additions & 1 deletion openlibrary/plugins/importapi/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ def ia_import(
raise BookImportError(status, 'Prohibited Item %s' % identifier)

# Case 4 - Does this item have a marc record?
marc_record = get_marc_record_from_ia(identifier)
marc_record = get_marc_record_from_ia(
identifier=identifier, ia_metadata=metadata
)
if require_marc and not marc_record:
raise BookImportError('no-marc-record')
if marc_record:
Expand Down

0 comments on commit 01f6c6d

Please sign in to comment.