| import os |
| import pandas as pd |
|
|
| """ |
| Input: |
| - str path = directory to store the raw survey |
| - str surveyname = name of survey. must be a survey in the study_config |
| """ |
|
|
| class PullSurvey(): |
| |
| apiToken = "giId4AaEUZa4PIHNI49TR1g4tzEs3zrm6GRcyVcr" |
| |
| userId = "UR_2sPkRisvCCNPi73" |
| |
| fileFormat = "csv" |
| |
| dataCenter = "ca1" |
|
|
| def pull_qualtrics(self,path,survey_name): |
| from lib.experiment_specs import study_config |
| from lib.data_helpers import qualtricsapi2 |
|
|
| |
| for sub_name, bool_str in {"Finished":"false","InProgress":"true"}.items(): |
|
|
| qualtricsapi2.export_survey(apiToken=self.apiToken, |
| surveyId=study_config.surveys[survey_name]["QualtricsID"], |
| fileFormat=self.fileFormat, |
| dataCenter=self.dataCenter, |
| downloadDir = path, |
| exportResponsesInProgress=bool_str) |
|
|
|
|
| new_path = os.path.join(path,survey_name + sub_name + ".csv") |
| if os.path.isfile(new_path): |
| os.remove(new_path) |
|
|
| |
| old_file = [] |
| for file in os.listdir(path): |
| matching_file = file.replace(" ","").replace("-","") |
| if (survey_name in matching_file) & \ |
| ("Finished" not in matching_file) &\ |
| ("InProgress" not in matching_file) &\ |
| (survey_name+".csv" != matching_file): |
| old_file.append(file) |
| try: |
| assert len(old_file) == 1 |
| except: |
| print(f" either 0 or more than one files with {survey_name} in file name in {path}") |
| sys.exit() |
| os.rename(os.path.join(path,old_file[0]),new_path) |
|
|
| finished = pd.read_csv(os.path.join(path,survey_name+"Finished.csv")) |
| in_progress = pd.read_csv(os.path.join(path,survey_name+"InProgress.csv")) |
| in_progress = in_progress.iloc[2:,] |
| full = finished.append(in_progress) |
| full = full.replace({r'\r': ''}, regex=True) |
| full.to_csv(os.path.join(path,survey_name+".csv"), index = False) |
| print("\t Successful Download!") |
|
|
| if __name__ == "__main__": |
| import sys |
| import git |
| |
| root = git.Repo('.', search_parent_directories = True).working_tree_dir |
| os.chdir(root) |
|
|
| sys.path.append(root) |
| survey_name = input("Which Survey to Pull for Main Pipeline? ") |
| path = os.path.join("data","external","dropbox_confidential","Surveys") |
| PullSurvey().pull_qualtrics(path,survey_name) |