File size: 2,997 Bytes
8a79f2e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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():
    # Qualtrics API Token
    apiToken = "giId4AaEUZa4PIHNI49TR1g4tzEs3zrm6GRcyVcr"
    # Qualtrics User ID
    userId = "UR_2sPkRisvCCNPi73"
    # Qualtrics Response Export File Format
    fileFormat = "csv"
    # Qualtrics Data Center (defaulting to 'ca1' is fine)
    dataCenter = "ca1"

    def pull_qualtrics(self,path,survey_name):
        from lib.experiment_specs import study_config
        from lib.data_helpers import qualtricsapi2

        # Get Responses in Progress and Completes
        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)

            # get the new raw file name by matching it to the clean file name,after removing spaces and hyphens from raw file
            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 directory of github repo
    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)