-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata_download.py
46 lines (44 loc) · 1.6 KB
/
metadata_download.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
import time
import os
import requests
import pandas as pd
# get access and refresh tokens
DC_USER = os.getenv('MR_USER')
DC_PSWD = os.getenv('MR_PSWD')
access_endpoint = "https://accounts.muckrock.com/api/token/"
params = {'username': DC_USER, 'password': DC_PSWD}
response = requests.post(access_endpoint, data = params )
refresh_token = response.json()["refresh"]
access_token = response.json()["access"]
# download metadata
DC_API = "https://api.www.documentcloud.org/api"
# download list of documents belonging to the project
DC_C19_project_id = "213211"
search_endpoint = f"{DC_API}/projects/{DC_C19_project_id}/documents/"
headers = {'Authorization': f'Bearer {access_token}'}
docid_list = []
while search_endpoint:
response = requests.get(search_endpoint, headers=headers)
results = response.json()["results"]
for r in results:
docid_list.append(r["document"])
search_endpoint = response.json()["next"]
print(len(docid_list))
# retrieve details for each document
EXPANSION = "?expand=user,organization,projects, sections, notes"
doc_list = []
i = 1
for d in docid_list:
search_endpoint = f"{DC_API}/documents/{d}/{EXPANSION}"
response = requests.get(search_endpoint, headers=headers)
results = response.json()
doc_list.append(results)
print(f'{i}: {results}')
i += 1
time.sleep(.2)
# TODO: On 6/8 load there were two metadata records with newlines
# embedded in a field. This breaks data load downstream. Manually
# removed, but do so programatically going forward.
df = pd.DataFrame(doc_list)
print(df.head())
df.to_csv('tmp/muckrock-covid19.csv', index=False, header=True)