-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyCat.py
More file actions
126 lines (107 loc) · 4.19 KB
/
Copy pathPyCat.py
File metadata and controls
126 lines (107 loc) · 4.19 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from io import BytesIO
import json
import os
import pandas as pd
from bookops_worldcat import WorldcatAccessToken, MetadataSession
from pymarc import XmlHandler, parse_xml, Field, Subfield
# Sets up the XML handler to convert MarcXML to something less annoying
def parse_xml_record(data):
handler = XmlHandler()
parse_xml(data, handler)
return handler.records[0]
# Defines a method of getting tokens with BookOps
# Separate file for security
def get_token():
creds_fh = os.path.join(os.getenv("HOME"), "Documents/PycharmProjects/OCLCToolsAPI2/00my_wskey.json")
with open(creds_fh, "r") as file:
creds = json.load(file)
token = WorldcatAccessToken(
key=creds["key"],
secret=creds["secret"],
scopes=creds["scopes"],
#principal_id=creds["principal_id"],
#principal_idns=creds["principal_idns"],
agent="de12@rice.edu"
)
return token
# Define some user input to determine Import or Overlay
userMessage = "If you want to import type I; overlay O:"
userinput = input(userMessage)
inputValid = False
while not inputValid:
mode = userinput.upper()
if mode not in ["I", "O"]:
userinput = input(userMessage)
else:
inputValid = True
# Define added fields work into bibs
my_590 = Field(
tag="590",
indicators=["0", " "],
subfields=[Subfield(code="a", value="Imported with PyCat.")]
)
my_590pub = Field(
tag="590",
indicators=["1", " "],
subfields=[Subfield(code="a", value="Gift: Gift of Robert K. Ritner, Egyptologist and Rice "
"alumnus, from his personal collection.")]
)
# Defines the list of OCLC numbers
# Swap middle concat to change sheets
gsheet = pd.read_csv(
"https://docs.google.com/spreadsheets/d/"
+ "1INovWFSXIBgxKJIrixBD-LWIb_2DKZ6LhlB2P4Gx3Qc"
+ "/export?gid=0&format=csv"
)
# Create a dictionary of OCLC/MMS pairs
# Critical for overlaying the imported records into ILS
id_dict = pd.Series(gsheet.MMSID.values, index=gsheet.OCLCID).to_dict()
print(id_dict)
oclc_numbers = [o for o in gsheet["OCLCID"]]
token = get_token()
# Loop over oclc numbers and make calls to API for each
with MetadataSession(authorization=token, agent="de12@rice.edu") as session:
for o in oclc_numbers:
# !!Create an exception wrapper
# Preview through matchMARC should minimize errors
response = session.bib_get(oclcNumber=o)
print(response.content)
# Convert responses from XML then parse in pymarc
data = BytesIO(response.content)
bib = parse_xml_record(data)
# MARC additions
bib.add_ordered_field(my_590)
#bib.add_ordered_field(my_590pub)
# MARC changes
# Directly changing 001s as documented in pymarc threw TypeErrors
# Create a mmsID variable, remove old 001, add new 001 w/ variable
# Calls on dictionary key:value pairs created earlier; partial match dictionary?
# Lead 0s from older OCLC records will give you issues.
if mode == "O":
mms001 = Field(tag="001", data=str(id_dict.get(bib["001"].value())))
bib.remove_fields("001")
bib.add_ordered_field(mms001)
# MARC removals
bib.remove_fields("003", "015", "016", "017", "019",
"029", "055", "060", "070", "072",
"082", "084", "263", "856", "938",
)
# Test if ind2 == 7, remove field if so
subjects = bib.get_fields("600", "610", "611", "630", "647",
"648", "650", "651", "653", "654",
"655", "656", "657", "658", "662",
)
for s in subjects:
if "7" in s.indicator2:
bib.remove_field(s)
for s in subjects:
if "6" in s.indicator2:
bib.remove_field(s)
for s in subjects:
if "2" in s.indicator2:
bib.remove_field(s)
print(bib)
# Write to a MARC21 file
# !! Append date to file name? Change to "wb" mode?
with open("retrieved_bibs.mrc", "ab") as out:
out.write(bib.as_marc())