|
|
import requests |
|
|
import zipfile |
|
|
import json |
|
|
import io, os |
|
|
import sys |
|
|
import re |
|
|
|
|
|
|
|
|
def export_survey(apiToken, surveyId, dataCenter, fileFormat,downloadDir, exportResponsesInProgress): |
|
|
surveyId = surveyId |
|
|
fileFormat = fileFormat |
|
|
dataCenter = dataCenter |
|
|
|
|
|
|
|
|
requestCheckProgress = 0.0 |
|
|
progressStatus = "inProgress" |
|
|
baseUrl = "https://{0}.qualtrics.com/API/v3/surveys/{1}/export-responses/".format(dataCenter, surveyId) |
|
|
headers = { |
|
|
"content-type": "application/json", |
|
|
"x-api-token": apiToken, |
|
|
} |
|
|
|
|
|
|
|
|
downloadRequestUrl = baseUrl |
|
|
downloadRequestPayload = '{"format":"' + fileFormat + '",' '"useLabels": true, "exportResponsesInProgress":' + exportResponsesInProgress +'}' |
|
|
downloadRequestResponse = requests.request("POST", downloadRequestUrl, data=downloadRequestPayload, headers=headers) |
|
|
progressId = downloadRequestResponse.json()["result"]["progressId"] |
|
|
print(downloadRequestResponse.text) |
|
|
|
|
|
|
|
|
while progressStatus != "complete" and progressStatus != "failed": |
|
|
print("progressStatus=", progressStatus) |
|
|
requestCheckUrl = baseUrl + progressId |
|
|
requestCheckResponse = requests.request("GET", requestCheckUrl, headers=headers) |
|
|
requestCheckProgress = requestCheckResponse.json()["result"]["percentComplete"] |
|
|
print("Download is " + str(requestCheckProgress) + " complete") |
|
|
progressStatus = requestCheckResponse.json()["result"]["status"] |
|
|
|
|
|
|
|
|
if progressStatus is "failed": |
|
|
raise Exception("export failed") |
|
|
|
|
|
fileId = requestCheckResponse.json()["result"]["fileId"] |
|
|
|
|
|
|
|
|
requestDownloadUrl = baseUrl + fileId + '/file' |
|
|
requestDownload = requests.request("GET", requestDownloadUrl, headers=headers, stream=True) |
|
|
|
|
|
|
|
|
zipfile.ZipFile(io.BytesIO(requestDownload.content)).extractall(downloadDir) |
|
|
print('Complete') |