Script for Anthology and getting courses & props

This commit is contained in:
Norm Rasmussen
2024-06-21 15:52:31 -04:00
parent 2e82486eb8
commit c17daddc44
3 changed files with 194 additions and 1 deletions

View File

@ -0,0 +1,82 @@
"""
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.ANTHOLOGY
COURSES = []
HEADERS = {"accept": "application/json", "X-Api-Key": APIKEY}
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}"
response = requests.get(url, headers=HEADERS)
data = response.json()
nextlink = data["links"]
for response in data["data"]:
status = response["attributes"]["status"]
# if status == "live":
# if name in COURSES:
uuid = response["id"]
name = response["attributes"]["name"]
# full_description = response["attributes"]["full_description"]
cprop = get_props(uuid)
course_dict = {
"id": uuid,
"name": name,
"status": status,
"product_names": cprop,
# "full_description": full_description,
}
print(course_dict)
try:
courses.append(course_dict)
except TypeError as e:
print(f"Error: {e}")
finally:
write_to_csv(courses)
pass
else:
pass
if "next" not in nextlink:
break
def get_props(uuid):
cprop_url = f"https://api.northpass.com/v2/properties/courses/{uuid}?filter[]"
cprop_resp = requests.get(cprop_url, headers=HEADERS)
cdata = cprop_resp.json()
for ckey, cval in cdata["data"]["attributes"]["properties"].items():
if ckey == "product_names_for_course_cards":
return cval
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_with_property.csv")
if __name__ == "__main__":
get_course()