-
Notifications
You must be signed in to change notification settings - Fork 7
/
export_assignment_responses.py
executable file
·62 lines (49 loc) · 1.59 KB
/
export_assignment_responses.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
#!/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'}
assignmentID = raw_input('What assignment are you trying to export? Look at the numbers at the end of the URL: ')
next_ = url + "assignment-responses/?crowdsource=" + str(assignmentID)
basic_fields = [
"id",
"user",
"datetime",
"tags",
"skip",
"number",
"flag",
"gallery",
"crowdsource"
]
assignment_fields = []
page = 1
csv_file = open('assignment ' + str(assignmentID) + ' export.csv', 'w')
csv_file.seek(0)
csv_writer = unicodecsv.writer(csv_file)
r = requests.get(next_, headers=headers)
json = r.json()
for column in json['results'][0]['values']:
assignment_fields.append(column['field'])
csv_writer.writerow(basic_fields + assignment_fields)
while next_ is not None:
r = requests.get(next_, headers=headers)
try:
json = r.json()
next_ = json['next']
for datum in json['results']:
submissions = []
for field in basic_fields:
submissions.append(datum[field])
for entry in datum["values"]:
submissions.append(entry["value"])
csv_writer.writerow(submissions)
# csv_writer.writerow(datum[field] for field in basic_fields + )
# + datum['values'][assignment_fields] for field in assignment_fields
print 'Page %d of %d' % (page, json['count'] / 40)
page += 1
except Exception as e:
print e