Repository navigation
Expand file tree
/
Copy pathtd.py
More file actions
113 lines (88 loc) · 4.75 KB
/
Copy pathtd.py
File metadata and controls
113 lines (88 loc) · 4.75 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
import requests, json, os, argparse, sys, time
reload(sys)
sys.setdefaultencoding("UTF8")
parser = argparse.ArgumentParser()
parser.add_argument('--offset', dest='offset', metavar='OFFSET',
action='store')
parser.add_argument('--day', dest='day', metavar='DAY',
action='store')
parser.add_argument('--month', dest='month', metavar='MONTH',
action='store')
args = parser.parse_args()
day = 0
month = 1
if args.day :
day = int(args.day) - 1
if args.month :
month = int(args.month)
if args.offset :
argoffset = int(args.offset)
while month < 13 :
offset = 0
if args.offset :
offset = offset + argoffset
argoffset = 0
day = day + 1
if day > 31 :
day = 1
month = month + 1
td = '/Users/Dominic/GitHub/Dominic-MP.github.io/td' + str(month) + '-' + str(day) + '.html'
geturl = 'https://catalog.archives.gov/api/v1/?resultTypes=item,fileUnit&description.item.productionDateArray.proposableQualifiableDate.month=' + str(month) + '&description.item.productionDateArray.proposableQualifiableDate.day=' + str(day) + '&resultFields=description.item.productionDateArray.proposableQualifiableDate.day,description.item.productionDateArray.proposableQualifiableDate.month,description.item.productionDateArray.proposableQualifiableDate.year,description.item.title,naId,objects&rows=1'
y = requests.get(geturl)
parsed = json.loads(y.text)
results = parsed['opaResponse']['results']['total']
if results > 500 :
results = 500
print 'There are ' + str(parsed['opaResponse']['results']['total']) + ' anniversary records for ' + str(month) + '-' + str(day) + '.'
while int(offset) < int(results) :
if int(offset) == 0 :
writefile = open(td, 'w')
writefile.write( ('<html>\n<head>\n<title>NARA records with anniversaries</title>\n</head>\n<body>\n<p><a href="td.html">return<a></p>\n\n<h1 align="center">NARA records with anniversaries</h1>\n\n<table style="width:100%" border="1">\n <tr style="text-align:center">\n <td>Date</td>\n <td>Year</td>\n <td>Thumbnail</td>\n <td>NAID</td>\n <td>Title</td>\n </tr>') )
writefile.close()
url = 'https://catalog.archives.gov/api/v1/?resultTypes=item,fileUnit&description.item.productionDateArray.proposableQualifiableDate.month=' + str(month) + '&description.item.productionDateArray.proposableQualifiableDate.day=' + str(day) + '&resultFields=description.item.productionDateArray.proposableQualifiableDate.day,description.item.productionDateArray.proposableQualifiableDate.month,description.item.productionDateArray.proposableQualifiableDate.year,description.item.title,naId,objects&rows=1&sort=naId asc&offset=' + str(offset)
try :
z = requests.get(url)
parse = json.loads(z.text)
except requests.exceptions.ConnectionError :
print requests.get(url).status_code
print requests.get(url).headers
except ValueError :
print 'Received 500 error! Sleeping...'
time.sleep(1205)
z = requests.get(url)
parse = json.loads(z.text)
print '\n----\n' + str(offset)
print url
try :
NAID = parse['opaResponse']['results']['result'][0]['naId']
title = parse['opaResponse']['results']['result'][0]['description']['item']['title']
year = parse['opaResponse']['results']['result'][0]['description']['item']['productionDateArray']['proposableQualifiableDate']['year']
except :
print z.text
pass
try :
fileurl = parse['opaResponse']['results']['result'][0]['objects']['object']['thumbnail']['@url'].replace('govOpaAPI', 'gov/OpaAPI')
# filename = parse['opaResponse']['results']['result'][0]['objects']['object']['file']['@name']
except TypeError :
try:
fileurl = parse['opaResponse']['results']['result'][0]['objects']['object'][0]['thumbnail']['@url'].replace('govOpaAPI', 'gov/OpaAPI')
# filename = parse['opaResponse']['results']['result'][0]['objects']['object'][0]['file']['@name']
print fileurl
except KeyError :
print 'No thumbnail found!\n----'
offset = offset + 1
continue
except KeyError :
print 'No thumbnail found!\n----'
offset = offset + 1
continue
print '\n' + str(month) + '-' + str(day) + '\n' + str(year) + '\n' + str(NAID) + '\n' + title.encode('utf-8') + '\n' + fileurl
if int(year[-1:]) == 0 or int(year[-1:]) == 5 :
print 'Match found!\n'
writefile = open(td, 'a')
writefile.write( ('\n <tr style="text-align:center">\n <td>' + str(month) + '-' + str(day) + '</td>\n <td>' + str(year) + '</td>\n <td><img src="' + fileurl + '"/></td>\n <td><a href="https://catalog.archives.gov/id/' + str(NAID) + '">' + str(NAID) + '</a></td>\n <td style="text-align:left">' + title.encode('utf-8') + '</td>\n </tr>') )
print '----'
offset = offset + 1
writefile = open(td, 'a')
writefile.write( ('</table>\n</body>\n</html>') )
writefile.close()