File size: 1,944 Bytes
dd7931c
 
 
 
 
 
 
d7684ca
dd7931c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import requests
from xml.etree import ElementTree as ET


def get_alma_information(endpoint: str, params={}):
    alma_url = "https://api-ap.hosted.exlibrisgroup.com"
    api_key = os.environ.get("ALMA_API_KEY")

    print("Key", api_key)

    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Authorization": f"apikey {api_key}",
    }

    r = requests.get(f"{alma_url}{endpoint}", headers=headers, params=params)

    if r.status_code != 200:
        print(
            f"Could not retrieve info from ALMA, status {r.status_code} return with the following text {r.text}"
        )
        return False

    return r.json()


def retrieve_bib(mms_id: str):

    endpoint = f"/almaws/v1/bibs/{mms_id}"
    mms_data = get_alma_information(endpoint)

    return mms_data


def extract_mms_id_metadata(mms_id):

    try:

        bib_data = retrieve_bib(mms_id)

        mms_id = bib_data.get("mms_id")
        title = bib_data.get("title")

        anies = bib_data["anies"]

        tree = ET.fromstring(anies[0])

        p_950 = "n/a"
        summary = "Could not find"
        title_statement = "n/a"

        for node in tree:
            if node.attrib.get("tag") == "520":
                for n in node:
                    summary = n.text

            if node.attrib.get("tag") == "245":
                for n in node:
                    if n.attrib.get("code") == "a":
                        title_statement = n.text

            if node.attrib.get("tag") == "950":
                for n in node:
                    if n.attrib.get("code") == "p":
                        p_950 = n.text

        mms_id_metadata = [title, title_statement, summary, p_950]

        print(mms_id, mms_id_metadata)

        return mms_id_metadata

    except Exception as e:

        print(f"Couldn't retrieve metadata for {mms_id}. here's the error {e}")

        return ["", "", "", ""]