#!/usr/bin/env python # coding: utf-8 # ## NARA API Catalog Queries for all the Morgenthau Diaries # # For example, you can open a NARA catalog URL that runs an **API Catalog query** showing **all 879 File Units** under the records of the **Morgenthau Diaries Series**. The query returns JSON data which can be displayed in a JSON browser such as the ** browser: # - https://catalog.archives.gov/api/v1/?description.fileUnit.parentSeries.naId=589213&description.fileUnit.title=Volume&rows=2050&resultTypes=fileUnit # where NARA ID 589213 is the unique ID for the Morgenthau Diaries Series. # # |
NARA Catalog Hierarchy
|
JSON Structure for the Query of the Morgenthau Diaries Series
| # | ----- | ----- | # | | | # # --- # ### Let's use JSON to walk **all 879 Morgenthau Diary Volumes**: # # In[10]: import requests import json # Going down the JSON tree to the RESULT section: response = requests.get("https://catalog.archives.gov/api/v2/records/parentNaId/589213?naId=589213&limit=1000", headers={"Content-Type": "application/json", "x-api-key": "API-KEY"}) data = response.json() with open('data.FUS.json', 'w') as f: json.dump(data, f) hits = data["body"]["hits"] #result # In[18]: count = hits["total"]["value"] print( count, "FileUnits" ) print( "------------------------------------------------------------------------" ) # In[43]: filecount = 0 print( "\n------------------------------------------------------------------------" ) for i in range(0, count): record = hits["hits"][i]["_source"]["record"] print( i, "FILE-UNIT_TITLE:", record["title"] ) # Going down to the fiole section of the objects/object portion of the JSON tree if record.get( "digitalObjects" ) is not None: digitalObjs = record["digitalObjects"] length = len( digitalObjs ) # print( "LENGTH: ", length ) if length >=5: filecount = filecount + 1 print( "\tOBJECT_FILENAME:", digitalObjs[length-1]["objectFilename"] ) else: for j in range(0, length): filecount = filecount + 1 print( "\tOBJECT_FILENAME:", digitalObjs[j]["objectFilename"] ) print( "------------------------------------------------------------------------" ) print( "\nTotal Number of Files:", filecount) # In[ ]: