Created
August 5, 2025 19:54
-
-
Save markpbaggett/3c6862ce0c5e1f1a68cff3553877d9ec to your computer and use it in GitHub Desktop.
Code to generate arks
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
import csv | |
import requests | |
import os | |
class EZIDARKGenerator: | |
def __init__(self, shoulder_url='https://ezid.cdlib.org/shoulder/ark:/81423/d2'): | |
self.url = shoulder_url | |
self.headers = {'Content-Type': 'text/plain'} | |
self.auth = (os.getenv("EZID_USER"), os.getenv("EZID_PASSWORD")) | |
self.completed = [] | |
def create_metadata(self, who, what, when, where): | |
"""Create metadata content string for EZID request. | |
Args: | |
who (str): the agent responsible for the resource — typically the creator, author, or contributor. | |
what (str): the title of the work | |
when (str): the date of publication for the original work | |
where (str): URL or current location of the resource | |
Returns: | |
dict: The data formatted for the Post and Creation of the Ark | |
""" | |
return ( | |
f'erc.who: {who}\n' | |
f'erc.what: {what}\n' | |
f'erc.when: {when}\n' | |
f'_target: {where}\n' | |
f'_status: \n' | |
) | |
def create_ark(self, who, what, when, where): | |
"""Create a single ARK identifier. | |
Args: | |
who (str): the agent responsible for the resource — typically the creator, author, or contributor. | |
what (str): the title of the work | |
when (str): the date of publication for the original work | |
where (str): URL or current location of the resource | |
Returns: | |
dict: Data sent to the ARK with the ARK returned | |
""" | |
metadata_content = self.create_metadata(who, what, when, where) | |
data = metadata_content.encode('utf-8') | |
response = requests.post(self.url, data=data, headers=self.headers, auth=self.auth) | |
full_message = response.content.decode('utf-8') | |
ark = "" | |
if "success" in full_message: | |
ark = f"https://n2t.net/{full_message.split(' ')[-1]}" | |
return { | |
'who': who, | |
'what': what, | |
'when': when, | |
'where': where, | |
'message': full_message, | |
'ark': ark, | |
} | |
def process_csv(self, input_file): | |
"""Process CSV file and create ARKs for each row. | |
Args: | |
input_file (str): The CSV that contains your ARK information with appropriate headings. | |
""" | |
with open(input_file, 'r', newline='') as csvfile: | |
reader = csv.DictReader(csvfile) | |
for row in reader: | |
result = self.create_ark( | |
row['who'], | |
row['what'], | |
row['when'], | |
row['where'] | |
) | |
self.completed.append(result) | |
def save_results(self, output_file): | |
"""Save completed results to CSV file.""" | |
fieldnames = ['who', 'what', 'when', 'where', 'message', 'ark'] | |
with open(output_file, 'w', newline='') as csvfile: | |
writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
writer.writeheader() | |
for row in self.completed: | |
writer.writerow(row) | |
def run(self, input_file, output_file): | |
"""Main method to process input and save results.""" | |
self.process_csv(input_file) | |
self.save_results(output_file) | |
return self.completed | |
if __name__ == "__main__": | |
input_csv = "quick.csv" | |
output_csv = "output2.csv" | |
generator = EZIDARKGenerator() | |
results = generator.run(input_csv, output_csv) | |
print(f"Processed {len(results)} records") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment