Files
Gainsight/Scripts/API_Tests/get_courses.py

84 lines
2.5 KiB
Python
Raw Normal View History

2023-03-24 15:59:46 -04:00
import requests
import pandas as pd
2023-03-24 15:59:46 -04:00
import Apikeys
2023-05-24 16:56:19 -04:00
apiKey = Apikeys.terminus_employee
course_dict = {}
2023-03-24 15:59:46 -04:00
def get_course():
count = 0
2023-03-29 18:06:13 -04:00
courses = []
2023-03-24 15:59:46 -04:00
while True:
count += 1
2023-05-24 16:56:19 -04:00
url = f"https://api.northpass.com/v2/courses?page={count}"
print(url)
2023-03-24 15:59:46 -04:00
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"]
2023-03-29 18:06:13 -04:00
uuid = response["id"]
name = response["attributes"]["name"]
2023-05-12 16:45:50 -04:00
build_url = response["links"]["builder"]["href"]
course_dict = {
"id": uuid,
"name": name,
"status": status,
2023-05-24 16:56:19 -04:00
# "build_url": build_url
2023-05-12 16:45:50 -04:00
# "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.
2023-03-29 18:06:13 -04:00
cat_id = response["relationships"]["categories"]["data"]
2023-05-12 16:45:50 -04:00
get_cat_name(cat_id, course_dict, courses)
2023-03-24 15:59:46 -04:00
if "next" not in nextlink:
break
def get_cat_name(cat_id, course_dict, courses):
headers = {"accept": "application/json", "X-Api-Key": apiKey}
cats = []
if len(cat_id) == 0:
pass
elif len(cat_id) == 1:
categoryid = cat_id[0]["id"]
2023-05-24 16:56:19 -04:00
url = f"https://api.northpass.com/v2/categories/{categoryid}"
cat_resp = requests.get(url, headers=headers)
cat_data = cat_resp.json()
cat_name = cat_data["data"]["attributes"]["name"]
print(cat_name)
cats.append(cat_name)
course_dict.update({"categories": cats})
else:
for item in cat_id:
categoryid = item["id"]
2023-05-24 16:56:19 -04:00
url = f"https://api.northpass.com/v2/categories/{categoryid}"
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})
try:
courses.append(course_dict)
except TypeError as e:
print(f"Error: {e}")
finally:
write_to_csv(courses)
2023-03-24 15:59:46 -04:00
def write_to_csv(courses):
df = pd.DataFrame.from_dict(courses)
2023-05-24 16:56:19 -04:00
df.to_csv("/Users/normrasmussen/Downloads/terminus_courses.csv")
2023-03-24 15:59:46 -04:00
if __name__ == "__main__":
get_course()