/
GUI-3-1.py
114 lines (90 loc) · 4.32 KB
/
GUI-3-1.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from tkinter import *
import requests
import zipfile
import io
import json
import re
import os
import shutil
# Setting user Parameters
apiToken = "8NUQ1BwOxdONDWTtUXIaqXIQ0ka582wdEOJ2XkSv"
dataCenter = "co1"
baseUrl = "https://{0}.qualtrics.com/API/v3/surveys".format(dataCenter)
headers = {
"x-api-token": apiToken,
}
response = requests.get(baseUrl, headers=headers)
data = response.json()
root = Tk()
root.geometry("500x600")
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side = BOTTOM)
def download(id):
apiToken = "8NUQ1BwOxdONDWTtUXIaqXIQ0ka582wdEOJ2XkSv"
surveyId = id
fileFormat = "json"
dataCenter = 'co1'
# Setting static parameters
requestCheckProgress = 0
progressStatus = "in progress"
baseUrl = "https://{0}.qualtrics.com/API/v3/responseexports/".format(dataCenter)
headers = {
"content-type": "application/json",
"x-api-token": apiToken,
}
# Step 1: Creating Data Export
downloadRequestUrl = baseUrl
downloadRequestPayload = '{"format":"' + fileFormat + '","surveyId":"' + surveyId + '"}'
downloadRequestResponse = requests.request("POST", downloadRequestUrl, data=downloadRequestPayload,
headers=headers)
progressId = downloadRequestResponse.json()["result"]["id"]
print(downloadRequestResponse.text)
# Step 2: Checking on Data Export Progress and waiting until export is ready
isFile = None
while requestCheckProgress < 100 and progressStatus is not "complete" and isFile is None:
requestCheckUrl = baseUrl + progressId
requestCheckResponse = requests.request("GET", requestCheckUrl, headers=headers)
isFile = (requestCheckResponse.json()["result"]["file"])
if isFile is None:
print("file not ready")
else:
print("file created:", requestCheckResponse.json()["result"]["file"])
requestCheckProgress = requestCheckResponse.json()["result"]["percentComplete"]
print("Download is " + str(requestCheckProgress) + " complete")
# Step 3: Downloading file
requestDownloadUrl = baseUrl + progressId + '/file'
requestDownload = requests.request("GET", requestDownloadUrl, headers=headers, stream=True)
regex = r"([a-zA-Z0-9,.\-|;!_?\[\]&\ \(\)]+.json)"
text = str(requestDownload.content, 'utf-8', errors='replace')
filename = re.findall(regex, text)
# Step 4: Unzipping the file
zipfile.ZipFile(io.BytesIO(requestDownload.content)).extractall("MyQualtricsDownload")
# os.chdir(os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop'))
dir = os.getcwd()
# open(os.mkdir(dir + "/MyQualtricsDownload/") + str(filename[0]), 'w')
with open(str(dir) + "/MyQualtricsDownload/" + str(filename[0]), 'r') as json_file:
json_decoded = json.load(json_file)
for element in json_decoded['responses']:
element['surveyID'] = id
element['surveyName'] = str(filename[0])
with open(str(dir) + "/MyQualtricsDownload/" + str(filename[0]), 'w') as json_out_file:
json.dump(json_decoded, json_out_file, indent=4, separators=(',', ': '))
print('Complete')
isExists_path = os.path.exists(os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop') + "/MyQualtricsDownload/")
if not isExists_path:
os.mkdir(os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop') + "/MyQualtricsDownload/")
isExists_file = os.path.exists(os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop') + "/MyQualtricsDownload/" + str(filename[0]))
if isExists_file:
os.remove(os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop') + "/MyQualtricsDownload/" + str(filename[0]))
shutil.move((str(dir)+ "/MyQualtricsDownload/" + str(filename[0])),
(os.path.join(os.path.join(os.path.expanduser('~')), 'Desktop')+ "/MyQualtricsDownload/"))
number_of_surveys = len(data["result"]["elements"])
list_of_surveys = []
var_bottoms = []
for i in range(number_of_surveys):
list_of_surveys.append([data["result"]["elements"][i]["name"], data["result"]["elements"][i]["id"]])
var_bottoms.append("b" + str(i))
var_bottoms[i] = Button(topFrame, text = list_of_surveys[i][0], width = 30, wraplength = 150, command = lambda i = i: download(list_of_surveys[i][1])).pack()
root.mainloop()