Last active
August 8, 2025 17:54
-
-
Save timathom/a9946451bcc57de3daec1cbb895ba722 to your computer and use it in GitHub Desktop.
bibframe-in-alma-draft.ipynb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "nbformat": 4, | |
| "nbformat_minor": 0, | |
| "metadata": { | |
| "colab": { | |
| "provenance": [], | |
| "authorship_tag": "ABX9TyPpebyjzaR5T1awGQuPUAdv", | |
| "include_colab_link": true | |
| }, | |
| "kernelspec": { | |
| "name": "python3", | |
| "display_name": "Python 3" | |
| }, | |
| "language_info": { | |
| "name": "python" | |
| } | |
| }, | |
| "cells": [ | |
| { | |
| "cell_type": "markdown", | |
| "metadata": { | |
| "id": "view-in-github", | |
| "colab_type": "text" | |
| }, | |
| "source": [ | |
| "<a href=\"https://colab.research.google.com/gist/timathom/a9946451bcc57de3daec1cbb895ba722/bibframe-in-alma-draft.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "source": [ | |
| "# BIBFRAME in Alma Draft Notebook\n", | |
| "Adapted from https://github.com/jimfhahn/marva2alma/blob/main/marva2alma.ipynb" | |
| ], | |
| "metadata": { | |
| "id": "tzc7EEnXhqjM" | |
| } | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "id": "g0qpjw0nIQuw" | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "# Install necessary Python packages\n", | |
| "!pip install requests lxml\n", | |
| "import os\n", | |
| "import warnings\n", | |
| "from google.colab import userdata\n", | |
| "import json\n", | |
| "import logging\n", | |
| "from urllib.parse import urlencode, urljoin\n", | |
| "\n", | |
| "import requests\n", | |
| "from lxml import etree\n", | |
| "from lxml.builder import ElementMaker\n", | |
| "\n", | |
| "# Configure logging\n", | |
| "logging.basicConfig(level=logging.INFO, format='%(levelname)s:%(message)s')\n", | |
| "\n", | |
| "warnings.filterwarnings('ignore')\n", | |
| "\n", | |
| "ALMA_URI_REGION = \"https://api-na.hosted.exlibrisgroup.com\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def build_alma_uri(uri_region, alma_api_key, mms_id, method):\n", | |
| " \"\"\"\n", | |
| " Constructs the Alma API URI by concatenating the provided segments.\n", | |
| "\n", | |
| " Parameters:\n", | |
| " - uri_region (str): Base URL for the Alma API.\n", | |
| " - alma_api_key (str): Alma API key.\n", | |
| " - mms_id (str): The MMS ID to include in the URI.\n", | |
| "\n", | |
| " Returns:\n", | |
| " - str: The fully constructed Alma API URI.\n", | |
| " \"\"\"\n", | |
| " if method == \"get\":\n", | |
| " base_path = f\"/almaws/v1/bibs/{mms_id}\"\n", | |
| " else:\n", | |
| " base_path = f\"/almaws/v1/bibs\"\n", | |
| "\n", | |
| " # query_params = {\n", | |
| " # \"view\": \"full\",\n", | |
| " # \"expand\": \"p_avail\",\n", | |
| " # \"validate\": \"false\",\n", | |
| " # \"override_warning\": \"true\",\n", | |
| " # \"check_match\": \"false\",\n", | |
| " # \"apikey\": alma_api_key\n", | |
| " # }\n", | |
| " query_params = {\n", | |
| " \"from_nz_mms_id\": \"\",\n", | |
| " \"from_cz_mms_id\": \"\",\n", | |
| " \"normalization\": \"\",\n", | |
| " \"validate\": \"false\",\n", | |
| " \"override_warning\": \"true\",\n", | |
| " \"check_match\": \"false\",\n", | |
| " \"import_profile\": \"\",\n", | |
| " \"apikey\": alma_api_key\n", | |
| " }\n", | |
| " query_string = urlencode(query_params)\n", | |
| " alma_uri = urljoin(uri_region, base_path) + \"?\" + query_string\n", | |
| " logging.debug(f\"π Constructed Alma URI: {alma_uri}\")\n", | |
| "\n", | |
| " return alma_uri" | |
| ], | |
| "metadata": { | |
| "id": "hIeA8IvqLaQL" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "bibframe = \"\"\"\n", | |
| "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"\n", | |
| " xmlns:bf=\"http://id.loc.gov/ontologies/bibframe/\"\n", | |
| " xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\"\n", | |
| " xmlns:bflc=\"http://id.loc.gov/ontologies/bflc/\"\n", | |
| " xmlns:madsrdf=\"http://www.loc.gov/mads/rdf/v1#\">\n", | |
| " <bf:Work rdf:about=\"\">\n", | |
| " <bf:adminMetadata>\n", | |
| " <bf:AdminMetadata>\n", | |
| " <bf:status>\n", | |
| " <bf:Status rdf:about=\"http://id.loc.gov/vocabulary/mstatus/n\">\n", | |
| " <rdfs:label>new</rdfs:label>\n", | |
| " </bf:Status>\n", | |
| " </bf:status>\n", | |
| " <bf:date rdf:datatype=\"http://www.w3.org/2001/XMLSchema#date\">1985-02-27</bf:date>\n", | |
| " <bf:agent>\n", | |
| " <bf:Agent>\n", | |
| " <bf:code>CtY</bf:code>\n", | |
| " </bf:Agent>\n", | |
| " </bf:agent>\n", | |
| " </bf:AdminMetadata>\n", | |
| " </bf:adminMetadata>\n", | |
| " <bf:adminMetadata>\n", | |
| " <bf:AdminMetadata>\n", | |
| " <bf:status>\n", | |
| " <bf:Status rdf:about=\"http://id.loc.gov/vocabulary/mstatus/c\">\n", | |
| " <rdfs:label>changed</rdfs:label>\n", | |
| " </bf:Status>\n", | |
| " </bf:status>\n", | |
| " <bf:date rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\"\n", | |
| " >2025-07-29T18:26:41</bf:date>\n", | |
| " <bf:agent>\n", | |
| " <bf:Agent>\n", | |
| " <bf:code>CtY</bf:code>\n", | |
| " </bf:Agent>\n", | |
| " </bf:agent>\n", | |
| " </bf:AdminMetadata>\n", | |
| " </bf:adminMetadata>\n", | |
| " <bf:adminMetadata>\n", | |
| " <bf:AdminMetadata>\n", | |
| " <bf:status>\n", | |
| " <bf:Status rdf:about=\"http://id.loc.gov/vocabulary/mstatus/c\">\n", | |
| " <rdfs:label>changed</rdfs:label>\n", | |
| " </bf:Status>\n", | |
| " </bf:status>\n", | |
| " <bf:generationProcess\n", | |
| " rdf:resource=\"https://github.com/lcnetdev/marc2bibframe2/releases/tag/v2.8.1\"/>\n", | |
| " <bf:date rdf:datatype=\"http://www.w3.org/2001/XMLSchema#dateTime\"\n", | |
| " >2025-07-29T22:41:12.749622654Z</bf:date>\n", | |
| " </bf:AdminMetadata>\n", | |
| " </bf:adminMetadata>\n", | |
| " <bf:adminMetadata>\n", | |
| " <bf:AdminMetadata>\n", | |
| " <bf:descriptionLevel rdf:resource=\"http://id.loc.gov/ontologies/bibframe-2-4-0/\"/>\n", | |
| " <bf:descriptionConventions>\n", | |
| " <bf:DescriptionConventions\n", | |
| " rdf:about=\"http://id.loc.gov/vocabulary/descriptionConventions/aacr\">\n", | |
| " <bf:code>aacr</bf:code>\n", | |
| " </bf:DescriptionConventions>\n", | |
| " </bf:descriptionConventions>\n", | |
| " <bf:descriptionConventions>\n", | |
| " <bf:DescriptionConventions\n", | |
| " rdf:about=\"http://id.loc.gov/vocabulary/descriptionConventions/appm\">\n", | |
| " <bf:code>appm</bf:code>\n", | |
| " </bf:DescriptionConventions>\n", | |
| " </bf:descriptionConventions>\n", | |
| " <bf:note>\n", | |
| " <bf:Note>\n", | |
| " <rdf:type rdf:resource=\"http://id.loc.gov/vocabulary/mnotetype/internal\"/>\n", | |
| " <rdfs:label>040 $aCtY$cCtY$eappm</rdfs:label>\n", | |
| " </bf:Note>\n", | |
| " </bf:note>\n", | |
| " </bf:AdminMetadata>\n", | |
| " </bf:adminMetadata>\n", | |
| " <rdf:type rdf:resource=\"http://id.loc.gov/ontologies/bibframe/MixedMaterial\"/>\n", | |
| " <rdf:type rdf:resource=\"http://id.loc.gov/ontologies/bibframe/Collection\"/>\n", | |
| " <bf:language rdf:resource=\"http://id.loc.gov/vocabulary/languages/eng\"/>\n", | |
| " <bf:contribution>\n", | |
| " <bf:Contribution>\n", | |
| " <rdf:type rdf:resource=\"http://id.loc.gov/ontologies/bibframe/PrimaryContribution\"/>\n", | |
| " <bf:agent>\n", | |
| " <bf:Agent\n", | |
| " rdf:about=\"\">\n", | |
| " <rdf:type rdf:resource=\"http://id.loc.gov/ontologies/bibframe/Person\"/>\n", | |
| " <rdfs:label>Ford, William</rdfs:label>\n", | |
| " <bflc:marcKey>1001 $aFord, William.$9no_linkage</bflc:marcKey>\n", | |
| " </bf:Agent>\n", | |
| " </bf:agent>\n", | |
| " <bf:role>\n", | |
| " <bf:Role rdf:about=\"http://id.loc.gov/vocabulary/relators/ctb\"/>\n", | |
| " </bf:role>\n", | |
| " </bf:Contribution>\n", | |
| " </bf:contribution>\n", | |
| " <bf:originDate>1789-1791 (inclusive)</bf:originDate>\n", | |
| " <bf:title>\n", | |
| " <bf:Title>\n", | |
| " <bf:mainTitle>William Ford papers</bf:mainTitle>\n", | |
| " </bf:Title>\n", | |
| " </bf:title>\n", | |
| " <bf:content>\n", | |
| " <bf:Content rdf:about=\"http://id.loc.gov/vocabulary/contentTypes/txt\">\n", | |
| " <rdfs:label>text</rdfs:label>\n", | |
| " <bf:source>\n", | |
| " <bf:Source rdf:about=\"http://id.loc.gov/vocabulary/genreFormSchemes/rdacontent\"/>\n", | |
| " </bf:source>\n", | |
| " </bf:Content>\n", | |
| " </bf:content>\n", | |
| " <bf:summary>\n", | |
| " <bf:Summary>\n", | |
| " <rdfs:label>Diary of William Ford, 1789-1791, of Games Farm, Branormlie, Devonshire,\n", | |
| " England concerning rural life. Also included is a one page diary and notes on hunting\n", | |
| " of Nicholas Ford.</rdfs:label>\n", | |
| " </bf:Summary>\n", | |
| " </bf:summary>\n", | |
| " <bf:summary>\n", | |
| " <bf:Summary>\n", | |
| " <rdfs:label>Diary of William Ford, 1789-1791, documenting rural life at Games Farm,\n", | |
| " Branormlie, Devonshire, England. The diary provides insights into agricultural\n", | |
| " practices, daily routines, and social interactions during the late 18th century.\n", | |
| " Additionally, the resource includes a one-page diary and notes on hunting by Nicholas\n", | |
| " Ford, offering a glimpse into recreational activities and wildlife observations of\n", | |
| " the period.</rdfs:label>\n", | |
| " </bf:Summary>\n", | |
| " </bf:summary>\n", | |
| " <bf:summary>\n", | |
| " <bf:Summary>\n", | |
| " <rdfs:label>Diario de William Ford, 1789-1791, que documenta la vida rural en Games\n", | |
| " Farm, Branormlie, Devonshire, Inglaterra. El diario ofrece informaciΓ³n sobre\n", | |
| " prΓ‘cticas agrΓcolas, rutinas diarias e interacciones sociales durante finales del\n", | |
| " siglo XVIII. AdemΓ‘s, el recurso incluye un diario de una pΓ‘gina y notas sobre caza de\n", | |
| " Nicholas Ford, que brindan una visiΓ³n de las actividades recreativas y observaciones\n", | |
| " de la vida silvestre de la Γ©poca.</rdfs:label>\n", | |
| " </bf:Summary>\n", | |
| " </bf:summary>\n", | |
| " <bf:subject>\n", | |
| " <bf:Agent\n", | |
| " rdf:about=\"\">\n", | |
| " <rdf:type rdf:resource=\"http://id.loc.gov/ontologies/bibframe/Person\"/>\n", | |
| " <rdfs:label>Ford, Nicholas</rdfs:label>\n", | |
| " <bflc:marcKey>60010$aFord, Nicholas.$9no_linkage</bflc:marcKey>\n", | |
| " <bf:source rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " </bf:Agent>\n", | |
| " </bf:subject>\n", | |
| " <bf:subject>\n", | |
| " <bf:Agent\n", | |
| " rdf:about=\"\">\n", | |
| " <rdf:type rdf:resource=\"http://id.loc.gov/ontologies/bibframe/Person\"/>\n", | |
| " <rdfs:label>Ford, William</rdfs:label>\n", | |
| " <bflc:marcKey>60010$aFord, William.$9no_linkage</bflc:marcKey>\n", | |
| " <bf:source rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " </bf:Agent>\n", | |
| " </bf:subject>\n", | |
| " <bf:subject>\n", | |
| " <bf:Topic rdf:about=\"http://id.loc.gov/authorities/subjects/sh85056938\">\n", | |
| " <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#ComplexSubject\"/>\n", | |
| " <rdfs:label>Great Britain--Rural conditions</rdfs:label>\n", | |
| " <madsrdf:authoritativeLabel>Great Britain--Rural\n", | |
| " conditions</madsrdf:authoritativeLabel>\n", | |
| " <madsrdf:isMemberOfMADSScheme rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " <madsrdf:componentList rdf:parseType=\"Collection\">\n", | |
| " <madsrdf:Geographic>\n", | |
| " <madsrdf:authoritativeLabel>Great Britain</madsrdf:authoritativeLabel>\n", | |
| " </madsrdf:Geographic>\n", | |
| " <madsrdf:Topic>\n", | |
| " <madsrdf:authoritativeLabel>Rural conditions</madsrdf:authoritativeLabel>\n", | |
| " </madsrdf:Topic>\n", | |
| " </madsrdf:componentList>\n", | |
| " <bf:source rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " </bf:Topic>\n", | |
| " </bf:subject>\n", | |
| " <bf:genreForm>\n", | |
| " <bf:GenreForm\n", | |
| " rdf:about=\"\">\n", | |
| " <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#GenreForm\"/>\n", | |
| " <rdfs:label>Diaries</rdfs:label>\n", | |
| " <madsrdf:authoritativeLabel>Diaries</madsrdf:authoritativeLabel>\n", | |
| " <bf:source>\n", | |
| " <bf:Source rdf:about=\"http://id.loc.gov/vocabulary/genreFormSchemes/ftamc\">\n", | |
| " <bf:code>ftamc</bf:code>\n", | |
| " </bf:Source>\n", | |
| " </bf:source>\n", | |
| " </bf:GenreForm>\n", | |
| " </bf:genreForm>\n", | |
| " <bf:subject>\n", | |
| " <bf:Topic rdf:about=\"http://id.loc.gov/authorities/subjects/sh85063117\">\n", | |
| " <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#ComplexSubject\"/>\n", | |
| " <rdfs:label>Hunting.--England.--18th century.</rdfs:label>\n", | |
| " <madsrdf:authoritativeLabel>Hunting.--England.--18th\n", | |
| " century.</madsrdf:authoritativeLabel>\n", | |
| " <madsrdf:isMemberOfMADSScheme rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " <madsrdf:componentList rdf:parseType=\"Collection\">\n", | |
| " <madsrdf:Topic>\n", | |
| " <madsrdf:authoritativeLabel>Hunting</madsrdf:authoritativeLabel>\n", | |
| " </madsrdf:Topic>\n", | |
| " <madsrdf:Geographic>\n", | |
| " <madsrdf:authoritativeLabel>England</madsrdf:authoritativeLabel>\n", | |
| " </madsrdf:Geographic>\n", | |
| " <madsrdf:Temporal>\n", | |
| " <madsrdf:authoritativeLabel>18th century</madsrdf:authoritativeLabel>\n", | |
| " </madsrdf:Temporal>\n", | |
| " </madsrdf:componentList>\n", | |
| " <bf:source rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " </bf:Topic>\n", | |
| " </bf:subject>\n", | |
| " <bf:subject>\n", | |
| " <bf:Topic\n", | |
| " rdf:about=\"\">\n", | |
| " <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#ComplexSubject\"/>\n", | |
| " <rdfs:label>Devonshire (England)--Rural conditions</rdfs:label>\n", | |
| " <madsrdf:authoritativeLabel>Devonshire (England)--Rural\n", | |
| " conditions</madsrdf:authoritativeLabel>\n", | |
| " <madsrdf:isMemberOfMADSScheme rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " <madsrdf:componentList rdf:parseType=\"Collection\">\n", | |
| " <madsrdf:Geographic>\n", | |
| " <madsrdf:authoritativeLabel>Devonshire (England)</madsrdf:authoritativeLabel>\n", | |
| " </madsrdf:Geographic>\n", | |
| " <madsrdf:Topic>\n", | |
| " <madsrdf:authoritativeLabel>Rural conditions</madsrdf:authoritativeLabel>\n", | |
| " </madsrdf:Topic>\n", | |
| " </madsrdf:componentList>\n", | |
| " <bf:source rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " </bf:Topic>\n", | |
| " </bf:subject>\n", | |
| " <bf:subject>\n", | |
| " <bf:Topic rdf:about=\"http://id.loc.gov/authorities/names/n2006006349\">\n", | |
| " <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#ComplexSubject\"/>\n", | |
| " <madsrdf:authoritativeLabel>Ford, William--Diaries.</madsrdf:authoritativeLabel>\n", | |
| " <madsrdf:isMemberOfMADSScheme rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " <bf:source rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " <madsrdf:componentList rdf:parseType=\"Collection\">\n", | |
| " <bf:Agent rdf:about=\"http://id.loc.gov/rwo/agents/n2006006349\">\n", | |
| " <rdf:type rdf:resource=\"http://id.loc.gov/ontologies/bibframe/Person\"/>\n", | |
| " <rdfs:label>Ford, William</rdfs:label>\n", | |
| " <bflc:marcKey>60010$aFord, William</bflc:marcKey>\n", | |
| " <madsrdf:isIdentifiedByAuthority>\n", | |
| " <madsrdf:PersonalName rdf:about=\"http://id.loc.gov/authorities/names/n2006006349\">\n", | |
| " <madsrdf:authoritativeLabel>Ford, William</madsrdf:authoritativeLabel>\n", | |
| " <madsrdf:isMemberOfMADSScheme\n", | |
| " rdf:resource=\"http://id.loc.gov/authorities/names\"/>\n", | |
| " </madsrdf:PersonalName>\n", | |
| " </madsrdf:isIdentifiedByAuthority>\n", | |
| " <bf:source rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " </bf:Agent>\n", | |
| " <madsrdf:Topic>\n", | |
| " <madsrdf:authoritativeLabel>Diaries</madsrdf:authoritativeLabel>\n", | |
| " </madsrdf:Topic>\n", | |
| " </madsrdf:componentList>\n", | |
| " </bf:Topic>\n", | |
| " </bf:subject>\n", | |
| " <bf:subject>\n", | |
| " <bf:Topic rdf:about=\"http://id.loc.gov/authorities/names/no95021271\">\n", | |
| " <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#ComplexSubject\"/>\n", | |
| " <madsrdf:authoritativeLabel>Ford, Nicholas--Hunting.</madsrdf:authoritativeLabel>\n", | |
| " <madsrdf:isMemberOfMADSScheme rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " <bf:source rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " <madsrdf:componentList rdf:parseType=\"Collection\">\n", | |
| " <bf:Agent rdf:about=\"http://id.loc.gov/rwo/agents/no95021271\">\n", | |
| " <rdf:type rdf:resource=\"http://id.loc.gov/ontologies/bibframe/Person\"/>\n", | |
| " <rdfs:label>Ford, Nicholas</rdfs:label>\n", | |
| " <bflc:marcKey>60010$aFord, Nicholas</bflc:marcKey>\n", | |
| " <madsrdf:isIdentifiedByAuthority>\n", | |
| " <madsrdf:PersonalName rdf:about=\"http://id.loc.gov/authorities/names/no95021271\">\n", | |
| " <madsrdf:authoritativeLabel>Ford, Nicholas</madsrdf:authoritativeLabel>\n", | |
| " <madsrdf:isMemberOfMADSScheme\n", | |
| " rdf:resource=\"http://id.loc.gov/authorities/names\"/>\n", | |
| " </madsrdf:PersonalName>\n", | |
| " </madsrdf:isIdentifiedByAuthority>\n", | |
| " <bf:source rdf:resource=\"http://id.loc.gov/authorities/subjects\"/>\n", | |
| " </bf:Agent>\n", | |
| " <madsrdf:Topic>\n", | |
| " <madsrdf:authoritativeLabel>Hunting</madsrdf:authoritativeLabel>\n", | |
| " </madsrdf:Topic>\n", | |
| " </madsrdf:componentList>\n", | |
| " </bf:Topic>\n", | |
| " </bf:subject>\n", | |
| " <bf:genreForm>\n", | |
| " <bf:GenreForm rdf:about=\"http://id.loc.gov/authorities/genreForms/gf2014026085\">\n", | |
| " <rdf:type rdf:resource=\"http://www.loc.gov/mads/rdf/v1#GenreForm\"/>\n", | |
| " <rdfs:label>Diaries</rdfs:label>\n", | |
| " <madsrdf:authoritativeLabel>Diaries</madsrdf:authoritativeLabel>\n", | |
| " <bf:source>\n", | |
| " <bf:Source rdf:about=\"http://id.loc.gov/vocabulary/genreFormSchemes/lcgft\">\n", | |
| " <bf:code>lcgft</bf:code>\n", | |
| " </bf:Source>\n", | |
| " </bf:source>\n", | |
| " </bf:GenreForm>\n", | |
| " </bf:genreForm>\n", | |
| " <bf:contribution>\n", | |
| " <bf:Contribution>\n", | |
| " <bf:agent>\n", | |
| " <bf:Agent rdf:about=\"http://id.loc.gov/rwo/agents/no95021271\">\n", | |
| " <rdf:type rdf:resource=\"http://id.loc.gov/ontologies/bibframe/Person\"/>\n", | |
| " <rdfs:label>Ford, Nicholas</rdfs:label>\n", | |
| " <bflc:marcKey>7001 $aFord,\n", | |
| " Nicholas.$ediarist.$0http://id.loc.gov/authorities/names/no95021271</bflc:marcKey>\n", | |
| " <madsrdf:isIdentifiedByAuthority\n", | |
| " rdf:resource=\"http://id.loc.gov/authorities/names/no95021271\"/>\n", | |
| " </bf:Agent>\n", | |
| " </bf:agent>\n", | |
| " <bf:role>\n", | |
| " <bf:Role>\n", | |
| " <rdfs:label>diarist</rdfs:label>\n", | |
| " </bf:Role>\n", | |
| " </bf:role>\n", | |
| " </bf:Contribution>\n", | |
| " </bf:contribution>\n", | |
| " </bf:Work>\n", | |
| "</rdf:RDF>\n", | |
| "\"\"\"" | |
| ], | |
| "metadata": { | |
| "id": "VscHoAb_VlPQ" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "tree = etree.fromstring(bibframe)\n", | |
| "namespaces = {k: v for k, v in tree.nsmap.items() if k}\n", | |
| "print(f\"π§© Extracted namespaces: {namespaces}\")" | |
| ], | |
| "metadata": { | |
| "id": "qzQMEUW2W0CT" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "# Extract bf:Work and bf:Instance\n", | |
| "bf_work = tree.find('.//bf:Work', namespaces)\n", | |
| "bf_instance = tree.find('.//bf:Instance', namespaces)\n", | |
| "\n", | |
| "work_about_attr = bf_work.get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about') if bf_work is not None else None\n", | |
| "instance_about_attr = bf_instance.get('{http://www.w3.org/1999/02/22-rdf-syntax-ns#}about') if bf_instance is not None else None\n", | |
| "print(f\"π Work URL: {work_about_attr}\")\n", | |
| "print(f\"π Instance URL: {instance_about_attr}\")\n", | |
| "\n", | |
| "work_mms_id = None\n", | |
| "instance_mms_id = None\n", | |
| "work_uri = None\n" | |
| ], | |
| "metadata": { | |
| "id": "2IcUzuBmXQLK" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "def format_work(bf_work):\n", | |
| " # Process bf:Work\n", | |
| " if bf_work is not None:\n", | |
| " print(\"π Processing bf:Work element.\")\n", | |
| " try:\n", | |
| " bf_work_str = etree.tostring(bf_work, pretty_print=True, encoding='UTF-8').decode('utf-8')\n", | |
| " bf_work_str = f\"\"\"<bib>\n", | |
| " <record_format>lcbf_work</record_format>\n", | |
| " <suppress_from_publishing>false</suppress_from_publishing>\n", | |
| " <record>\n", | |
| " <rdf:RDF {' '.join([f'xmlns:{k}=\"{v}\"' for k, v in namespaces.items()])}>\n", | |
| " {bf_work_str}\n", | |
| " </rdf:RDF>\n", | |
| " </record>\n", | |
| " </bib>\"\"\"\n", | |
| " return bf_work_str\n", | |
| " #print(etree.tostring(bf_work, encoding='unicode', pretty_print=True))\n", | |
| " except Exception as e:\n", | |
| " print(f\"β Error processing bf:Work: {e}\")" | |
| ], | |
| "metadata": { | |
| "id": "VWcrzOjjXgvi" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "headers = {\n", | |
| " \"Content-Type\": \"application/xml; charset=utf-8\",\n", | |
| " \"Accept\": \"application/xml\",\n", | |
| " \"x-api-key\": userdata.get(\"ALMA_API_KEY\"),\n", | |
| "}\n", | |
| "alma_uri = build_alma_uri(ALMA_URI_REGION, userdata.get(\"ALMA_API_KEY\"), \"\", \"post\")" | |
| ], | |
| "metadata": { | |
| "id": "PdB2dpLRJKAM" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "response = requests.get(\n", | |
| " alma_uri,\n", | |
| " headers=headers,\n", | |
| ")\n", | |
| "logging.debug(f\"DEBUG: Alma result status code: {response.status_code}\")\n", | |
| "logging.debug(f\"DEBUG: Alma result text: {response.text}\")\n", | |
| "response.raise_for_status()\n", | |
| "\n", | |
| "# Parse successful response\n", | |
| "tree = etree.fromstring(response.content)\n", | |
| "print(etree.tostring(tree, pretty_print=True).decode())" | |
| ], | |
| "metadata": { | |
| "id": "YvdNm_xFirxX" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "format_work(bf_work)" | |
| ], | |
| "metadata": { | |
| "id": "IYucCczblQZr" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "source": [ | |
| "content = format_work(bf_work)\n", | |
| "\n", | |
| "try:\n", | |
| "\n", | |
| " response = requests.post(\n", | |
| " alma_uri,\n", | |
| " headers=headers,\n", | |
| " data=content.encode('utf-8'),\n", | |
| " )\n", | |
| " logging.debug(f\"DEBUG: Alma result status code: {response.status_code}\")\n", | |
| " logging.debug(f\"DEBUG: Alma result text: {response.text}\")\n", | |
| "\n", | |
| "\n", | |
| " # Parse successful response\n", | |
| " tree = etree.fromstring(response.content)\n", | |
| " print(etree.tostring(tree, pretty_print=True).decode())\n", | |
| "\n", | |
| "except requests.exceptions.HTTPError as e:\n", | |
| " tree = etree.fromstring(response.content)\n", | |
| "\n", | |
| " print(etree.tostring(tree, pretty_print=True).decode())\n" | |
| ], | |
| "metadata": { | |
| "id": "t9svAWY8gbPW" | |
| }, | |
| "execution_count": null, | |
| "outputs": [] | |
| } | |
| ] | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment