progressId = downloadRequestResponse.json()["result"]["id"]
If I run a check on ownerId, I don't get the KeyError. However, not all of my surveys are extracted. It's super weird and I'm a bit at a loss. Anyone have any ideas of how I might fix this?
~~~~
import os
import requests
import json
import zipfile
try: import simplejson as json
except ImportError: import json
Setting user Parameters
apiToken = 'APITOKEN'
dataCenter = 'DATACENTER'
Constructing the URL
baseUrl = "https://{0}.qualtrics.com/API/v3/surveys".format(dataCenter)
headers = {
"x-api-token": apiToken,
}
Getting the json data
json_data = requests.get(baseUrl,headers=headers).json()
Set the empty array
surveyIds = []
i = 0
Create the array; prints are for debugging only all active survey names and IDs
for each in json_data['result']['elements']:
If I don't include the OwnerID check, I get the KeyError, but if I do include the OwnerID check, I don't get all of my surveys
if each['isActive'] == True and each['ownerId'] == 'OWNERID':
surveyIds.append(each['id'])
print(each['name'])
print(each['id'])
i = i + 1
For debugging
print "There are ", i, " active surveys"
print(surveyIds)
Construct the survey extracts
for surveyId in surveyIds:
fileFormat = "csv"
# This the data center our qualtrics site uses.
dataCenter = 'DATACENTER'
# 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)
This is where the error is thrown if I don't include the OwnerID check
progressId = downloadRequestResponse.json()["result"]["id"]
print downloadRequestResponse.text
# Step 2: Checking on Data Export Progress and waiting until export is ready
while requestCheckProgress < 100 and progressStatus is not "complete":
requestCheckUrl = baseUrl + progressId
requestCheckResponse = requests.request("GET", requestCheckUrl, headers=headers)
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)
# Step 4: Unzipping file
with open("RequestFile.zip", "wb") as f:
for chunk in requestDownload.iter_content(chunk_size=1024):
f.write(chunk)
zipfile.ZipFile("RequestFile.zip").extractall("SurveyExports")