Files
Gainsight/Scripts/API_Tests/get_course_ids.py

54 lines
1.4 KiB
Python

import Apikeys
import requests
import pprint
import csv
pp = pprint.PrettyPrinter(indent=4)
APIKEY = Apikeys.WALMARTPROD
HEADERS = {
"accept": "application/json",
"X-Api-Key": APIKEY,
}
def get_courses():
"""
Function to get courses and their IDs
"""
count = 0
list_of_ids = []
while True:
# for course_name in COURSES:
count += 1
# url = f"https://api.northpass.com/v2/courses/?filter[name][eq]={course_name}"
url = f"https://api2.northpass.com/v2/courses/?limit=100&page={count}"
response = requests.get(url, headers=HEADERS)
response = response.json()
nextlink = response["links"]
for item in response["data"]:
status = item['attributes']['status']
if status == 'live':
name = item['attributes']['name']
idict = {item["attributes"]["name"]: item["id"]}
list_of_ids.append(idict)
# if "[Core]" in name:
if "next" not in nextlink:
break
pp.pprint(list_of_ids)
# print(len(list_of_ids))
with open(
"/Users/normrasmussen/Downloads/Walmart-Live-Courses.csv", "a+", newline='\n'
) as csvfile:
for group in list_of_ids:
for key, value in group.items():
csvwriter = csv.writer(csvfile)
csvwriter.writerow([key, value])
if __name__ == "__main__":
get_courses()