Script for Anthology and getting courses & props
This commit is contained in:
82
Scripts/API_Tests/get_courses_w_descriptions_or_props.py
Normal file
82
Scripts/API_Tests/get_courses_w_descriptions_or_props.py
Normal 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()
|
||||
Reference in New Issue
Block a user