Files
Gainsight/Scripts/API_Tests/get_courses_w_descriptions.py

67 lines
1.8 KiB
Python

"""
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.walmartprod
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://api2.northpass.com/v2/courses?page={count}"
print(url)
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"]
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)
else:
pass
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()