-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport-answers.py
56 lines (42 loc) · 1.43 KB
/
export-answers.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
#!/usr/bin/env python
import requests
import csv
import sys
from datetime import datetime
reload(sys)
sys.setdefaultencoding('utf-8')
SERVER_NAME = 'rgi.nrgi-assessment.org'
DESTINATION_PATH = "/Users/cperry/Box Sync/RAD/RGI/raw_data"
PORTION_SIZE = 100
csv_header = []
answers = []
loaded_completely = False
page = 0
while not loaded_completely:
res = requests.get('http://' + SERVER_NAME + '/api/raw_answers/' + str(PORTION_SIZE) + '/' + str(page))
page += 1
portion_data = res.json()
loaded_completely = len(portion_data["data"]) < PORTION_SIZE
answers += portion_data["data"]
for field in portion_data["header"]:
if not (field in csv_header):
csv_header.append(field)
sorted_header = []
for field in csv_header:
if not ("comment" in field) and not ("flag" in field):
sorted_header.append(field)
for sorted_field in ["comment", "flag"]:
for field in csv_header:
if sorted_field in field:
sorted_header.append(field)
with open('raw_data' + datetime.now().strftime('%Y-%m-%d-%H:%M:%S') + '.csv', "w") as csv_file:
csv_writer = csv.writer(csv_file, delimiter=',', quotechar='"')
csv_writer.writerow(sorted_header)
for answer_data in answers:
answer = []
for key in sorted_header:
try:
answer.append(answer_data[key])
except KeyError:
answer.append("")
csv_writer.writerow(answer)