Files
Gainsight/Scripts/API_Tests/get_courses.py

76 lines
2.3 KiB
Python

import requests
import pandas as pd
import Apikeys
def get_course():
count = 0
apiKey = Apikeys.walmartprod
courses = []
course_dict = {}
while True:
count += 1
url = f"https://api2.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"]
uuid = response["id"]
name = response["attributes"]["name"]
course_dict = {
"id": uuid,
"name": name,
"status": status,
"url": f"https://walmart.northpass.com/app/courses/{uuid}"
}
# FIX: Up until here, each course gets read to the terminal.
# FIX: After this, something is being overwritten.
cat_id = response["relationships"]["categories"]["data"]
cats = []
# Pseudo-code
if len(cat_id) == 0:
pass
elif len(cat_id) == 1:
get_cat_name(cat_id)
course_dict(update({"categories": cat}))
else:
for item in cat_id:
# make api call
# return the name
update the dictionary
for item in cat_id:
cats = []
categoryid = item["id"]
# TODO: Maybe split the api call to a separate function?
url = f"https://api2.northpass.com/v2/categories/{categoryid}"
headers = {"accept": "application/json", "X-Api-Key": apiKey}
cat_resp = requests.get(url, headers=headers)
cat_data = cat_resp.json()
cat_name = cat_data["data"]["attributes"]["name"]
cats.append(cat_name)
course_dict.update({"categories": cats})
courses.append(course_dict)
if "next" not in nextlink:
break
write_to_csv(courses)
def get_cat_name(item):
pass
def write_to_csv(courses):
df = pd.DataFrame.from_dict(courses)
df.to_csv("/Users/normrasmussen/Downloads/walmart_courses.csv")
if __name__ == "__main__":
get_course()