import pprint
import requests
import Apikeys

APIKEY = Apikeys.CHUBB
URL = "https://api.northpass.com/v1/media"
pp = pprint.PrettyPrinter(indent=4)


def getMedia(APIKEY, URL):
    list_data = []
    headers = {"accept": "application/json", "X-Api-Key": APIKEY}
    URL = f"{URL}?limit=100"
    response = requests.get(URL, headers=headers)
    datas = response.json()

    for data in datas["data"]:
        file_type = data["attributes"]["file_type"]
        id_val = data["id"]
        create_link = data["links"]["create_course_from_file"]
        if "scorm" in file_type:
            data_tuple = (id_val, file_type, create_link)
            list_data.append(data_tuple)
    pp.pprint(list_data)
    print(len(list_data))

# def toCsv(json):
#     js = pd.json_normalize(json, "data", ["data"])
#     # csv = pd.Series(js)
#     js.to_csv("/Users/normrasmussen/Documents/Northpass/Scripts/API_Tests/spsmedia3.csv")


if __name__ == "__main__":
    getMedia(APIKEY, URL)
