""" Small script to grab the list of courses from Northpass API along with their status & description and write them to a CSV. """ import requests import pandas as pd import Apikeys APIKEY = Apikeys.talkspace_1099 COURSES = ["Getting Started at Talkspace (ICP - 2023)", "Getting Started at Talkspace (Prescriber)", "Getting Started at Talkspace (Associate ICP)", "Talkspace Managed Care Plans"] def get_course(): """ This function paginates through API responses to get the courses information """ count = 0 courses = [] course_dict = {} while True: count += 1 url = f"https://api.northpass.com/v2/courses?page={count}" headers = {"accept": "application/json", "X-Api-Key": APIKEY} response = requests.get(url, headers=headers) data = response.json() nextlink = data["links"] for response in data["data"]: status = response["attributes"]["status"] if status == "live": uuid = response["id"] name = response["attributes"]["name"] if name in COURSES: # full_description = response["attributes"]["full_description"] course_dict = { "id": uuid, "name": name, # "status": status, # "full_description": full_description, } try: courses.append(course_dict) except TypeError as e: print(f"Error: {e}") finally: # write_to_csv(courses) pass else: pass print(courses) if "next" not in nextlink: break def write_to_csv(courses): """ Function to write the list of dictionaries to a CSV using pandas. Takes on parameter, the list of courses. """ df = pd.DataFrame.from_dict(courses) df.to_csv("/Users/normrasmussen/Downloads/courses_descriptions.csv") if __name__ == "__main__": get_course()