54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
import Apikeys
|
|
import requests
|
|
import pprint
|
|
import csv
|
|
|
|
pp = pprint.PrettyPrinter(indent=4)
|
|
APIKEY = Apikeys.CIN7
|
|
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://api.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']
|
|
if "[Core]" in name:
|
|
idict = {item["attributes"]["name"]: item["id"]}
|
|
list_of_ids.append(idict)
|
|
|
|
if "next" not in nextlink:
|
|
break
|
|
|
|
pp.pprint(list_of_ids)
|
|
# print(len(list_of_ids))
|
|
with open(
|
|
"/Users/normrasmussen/Downloads/Cin7-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()
|