import threading
import requests
from datetime import datetime

def get_results():
    # threading.Timer(5.0, get_results).start()
    url = "https://ondemand.questionmark.com/deliveryodata/406287/Results"
    params = "?$filter=ParticipantName eq 'norm@northpass.com'"
    # params = "?$filter=ParticipantName eq 'tom.leggett@questionmark.com'"
    # params = "?$filter=AssessmentID eq 352630000352630L"
    # Alexa Test 1 AssesmentID = 0346128000346128
    url_params = f"{url}{params}"

    headers = {
        "Accept": "application/json",
        "Content-Type": "application/json",
        "Authorization": "Basic bm9ydGhwYXNzX2FwaTpobWUycXl4KlRGVSphcHkuZnBr"
    }

    # response = requests.get(url, headers=headers)
    response = requests.get(url_params, headers=headers)
    data = response.json()
    print(data)
    data = data["value"]

    now = datetime.now()
    current_time = now.strftime("%H:%M:%S")

    if data == []:
        logfile = open("qm-log.txt", "a")  # append mode
        logfile.write(f"No Data - {current_time} \n")
        logfile.close()
    else:
        datalist = []
        for item in data:
            email = item["ParticipantName"]
            passval = item["ScoreBandTitle"]
            score = item["PercentageScore"]
            assesmentid = item["AssessmentID"]
            finished = item["WhenFinished"]

            datalist = [email, assesmentid, score, passval, finished]
            # print(datalist)
            logfile = open("qm-log.txt", "a")  # append mode
            logfile.write(str(datalist))
            logfile.write(f" - {current_time} \n")
            logfile.close()


if __name__ == "__main__":
    get_results()
