Exemplo n.º 1
0
def get_survey_information(apiToken, dataCenter="ca1"):
    """Prints a list of the current survey names and corresponding Ids and asks
    the user which survey they want to recieve an export from.

    Args:
        apiToken(str): A string of characters that allows allows authentication
        when querying the Qualtrics API.
        dataCenter(str): Data center arguement for Qualtrics API.
    Returns:
        surveyID(str): A unique ID corresponding the to survey the user wants to
        retreieve survey responses for.
        Bool(False): If a surveyId isn't selected or found."""

    baseUrl = f"https://{dataCenter}.qualtrics.com/API/v3/surveys"

    headers = {
        "x-api-token": apiToken,
    }

    response = requests.get(baseUrl, headers=headers)
    try:
        survey_info = json.loads(response.text)['result']['elements']
    except KeyError:
        print(json.loads(response.text))
        print("You are presenting an invalid API token to qualtrics. "
              "Please make sure that your API token exactly matches"
              "the token listed on the Qualtrics account and try again.")
        sys.exit()

    print("\nBelow are the surveys listed under your qualtrics account:\n")
    print("-" * 80)
    if len(survey_info) == 0:
        print("There are no surveys associated with your Qualtrics account.")
        print("Quitting this program.. .. ..")
        sys.exit()

    for i, survey in enumerate(survey_info):
        print(f"{i + 1}. - Survey Name: {survey['name']}")
        print(f"Survey ID: {survey['id']}")
        print("-" * 80)

    prompt = (
        "Which survey would you like to export? \n(indicate the survey by typing the number listed next to the survey): "
    )
    survey_response = InputManager.get_numerical_input(prompt,
                                                       (len(survey_info)))
    surveyId = survey_info[survey_response - 1]['id']
    if surveyId:
        prompt = "Would you like to save this as your default survey? \n(Answering yes will save the id to '/.ids/surveyid.txt')"
        ans = InputManager.get_yes_or_no(prompt)
        if ans:
            if not os.path.exists(f"{os.getcwd()}/.ids/"):
                os.mkdir(f"{os.getcwd()}/.ids/")
            with open(f"{os.getcwd()}/.ids/surveyid.txt", "w") as out_file:
                out_file.write(surveyId)
        return surveyId
    return False
Exemplo n.º 2
0
def get_prelim_qs(df):
    """Asks the user to input the amount of questions in the survey that do not
	correspond to survey questions given to the participant (preliminary
	questions).

	Args:
		df: A cleaned DataFrame of the Qualtrics export.
	Returns:
		num_prelim_qs(int): The number of questions before the experiment began.
	"""

    num_prelim_qs = InputManager.get_numerical_input(
        "How many preliminary "
        "questions were asked "
        "before the experiment "
        "began?: ", 9)

    return num_prelim_qs