-
Notifications
You must be signed in to change notification settings - Fork 7
/
export_file_data.py
executable file
·47 lines (40 loc) · 1.04 KB
/
export_file_data.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
#!/usr/bin/env python2
# -- coding: utf-8 --
import requests
import unicodecsv
from utils import get_api_key
token = get_api_key()
url = 'https://www.muckrock.com/api_v1/'
headers = {'Authorization': 'Token %s' % token, 'content-type': 'application/json'}
next_ = url + 'communication'
fields = (
"id",
"ffile",
"title",
"date",
"source",
"description",
"access",
"doc_id",
"pages"
)
page = 1
csv_file = open('doccloudfiles.csv', 'w')
csv_file.seek(0)
csv_writer = unicodecsv.writer(csv_file)
csv_writer.writerow(fields)
while next_ is not None:
r = requests.get(next_, headers=headers)
try:
json = r.json()
next_ = json['next']
for datum in json['results']:
print "datum"
for docfile in datum['files']:
print docfile
csv_writer.writerow([docfile[field] for field in fields])
print "docfile"
print 'Page %d of %d' % (page, json['count'] / 20 + 1)
page += 1
except Exception as e:
print e