import Apikeys import requests import pprint import csv pp = pprint.PrettyPrinter(indent=4) APIKEY = Apikeys.SPS HEADERS = { "accept": "application/json", "X-Api-Key": APIKEY, } def get_courses(): """ Function to get courses and their IDs """ count = 0 list_of_ids = [] while True: # for course_name in COURSES: count += 1 # url = f"https://api.northpass.com/v2/courses/?filter[name][eq]={course_name}" url = f"https://api.northpass.com/v2/courses/?limit=100&page={count}" response = requests.get(url, headers=HEADERS) response = response.json() nextlink = response["links"] for item in response["data"]: status = item ['attributes']['status'] if status == 'live': cname = item['attributes']['name'] # print(item["id"]) idict = {item["attributes"]["name"]: item["id"]} # id = item["id"] # name = item["attributes"]["name"] list_of_ids.append(idict) if "next" not in nextlink: break pp.pprint(list_of_ids) # print(len(list_of_ids)) with open( "/Users/normrasmussen/Downloads/SPS-Courses.csv", "a+", newline='\n' ) as csvfile: for group in list_of_ids: for key, value in group.items(): csvwriter = csv.writer(csvfile) csvwriter.writerow([key, value]) if __name__ == "__main__": get_courses()