-
Notifications
You must be signed in to change notification settings - Fork 0
/
RetrieveDataset.py
73 lines (62 loc) · 1.62 KB
/
RetrieveDataset.py
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
import json
import requests
import sys
def downloadDataset():
print('Updating dataset...')
#saveData(updateJuristictions(), './juristictions.json')
#saveData(updateAgencies(), './agencies.json')
saveData(updateFoiaRequests(), './foiaRequests.json')
print('Dataset saved.')
return
def updateJuristictions():
return retrieveData('https://www.muckrock.com/api_v1/jurisdiction/', None)
def updateAgencies():
return retrieveData('https://www.muckrock.com/api_v1/agency/', None)
def updateFoiaRequests():
return retrieveData('https://www.muckrock.com/api_v1/foia/', None)
def retrieveData(baseUrl, params):
responses = []
page = 1
t = False
while True:
retryNum = 0
while True:
url = baseUrl + '?page=' + str(page)
if params != None:
url = url + '&' + params
try:
resp = requests.get(url)
data = resp.json()
page = page + 1
print(url)
break
except:
pass
retryNum = retryNum + 1
if retryNum == 3:
retryNum = 0
page = page + 1
for result in data['results']:
responses.append(result)
url = data['next']
if url == None:
break
return responses
def loadData():
print('Loading data...')
juristictions = loadDataByName('./juristictions.json')
agencies = loadDataByName('./agencies.json')
foiaRequests = loadDataByName('./foiaRequests.json')
return juristictions, agencies, foiaRequests
def loadDataByName(fileName):
file = open(fileName, 'r', encoding='utf8')
data = json.load(file)
file.close()
return data
def saveData(data, fileName):
file = open(fileName, 'w')
json.dump(data, file)
file.close()
return
def column(matrix, i):
return [row[i] for row in matrix]