import requests import zipfile import json import io, os import sys # Setting User Parameters apiToken = "{insert APITOKEN here - remove brackets, keep double quotes}" surveyId = "{insert SURVEYID here - remove brackets, keep double quotes}" dataCenter = '{insert DATACENTER value here - remove brackets, keep single quotes}' # Setting Static Parameters requestCheckProgress = 0.0 progressStatus = "inProgress" url = "https://{0}.qualtrics.com/API/v3/surveys/{1}/export-responses/".format(dataCenter, surveyId) headers = { "content-type": "application/json", "x-api-token": apiToken, } # Step 1: Creating Data Export data = { "format": "csv", "seenUnansweredRecode": 2 } downloadRequestResponse = requests.request("POST", url, json=data, headers=headers) print(downloadRequestResponse.json()) try: progressId = downloadRequestResponse.json()["result"]["progressId"] except KeyError: print(downloadRequestResponse.json()) sys.exit(2) isFile = None # Step 2: Checking on Data Export Progress and Waiting Until Export is Ready while progressStatus != "complete" and progressStatus != "failed" and isFile is None: if isFile is None: print ("file not ready") else: print ("progressStatus=", progressStatus) requestCheckUrl = url + progressId requestCheckResponse = requests.request("GET", requestCheckUrl, headers=headers) try: isFile = requestCheckResponse.json()["result"]["fileId"] except KeyError: 1==1 print(requestCheckResponse.json()) requestCheckProgress = requestCheckResponse.json()["result"]["percentComplete"] print("Download is " + str(requestCheckProgress) + " complete") progressStatus = requestCheckResponse.json()["result"]["status"] # Step 3: Check for Error if progressStatus is "failed": raise Exception("export failed") fileId = requestCheckResponse.json()["result"]["fileId"] # Step 4: Downloading File requestDownloadUrl = url + fileId + '/file' requestDownload = requests.request("GET", requestDownloadUrl, headers=headers, stream=True) # Step 5: Unzipping the File zipfile.ZipFile(io.BytesIO(requestDownload.content)).extractall(r"{insert directory pathway here - remove brackets, keep double quotes}") print('Complete')