Walmart supplier courses and categories script. bug with endpoint for groups not associated with a course.
This commit is contained in:
121
Scripts/API_Tests/get_courses_and_associated_groupscategories.py
Normal file
121
Scripts/API_Tests/get_courses_and_associated_groupscategories.py
Normal file
@ -0,0 +1,121 @@
|
||||
import requests
|
||||
from collections import Counter
|
||||
import pandas as pd
|
||||
import Apikeys
|
||||
import pprint
|
||||
|
||||
pp = pprint.PrettyPrinter(indent=4)
|
||||
apiKey = Apikeys.SUPPLIERPROD
|
||||
course_dict = {}
|
||||
HEADERS = {"accept": "application/json", "X-Api-Key": apiKey}
|
||||
|
||||
def get_groups():
|
||||
grouple = []
|
||||
count = 0
|
||||
|
||||
while True:
|
||||
count += 1
|
||||
url = f"https://api2.northpass.com/v2/groups?page={count}"
|
||||
gresponse = requests.get(url, headers=HEADERS).json()
|
||||
nextlink = gresponse["links"]
|
||||
|
||||
for gresp in gresponse["data"]:
|
||||
grouple.append(( gresp["id"], gresp["attributes"]["name"] ))
|
||||
|
||||
if "next" not in nextlink:
|
||||
break
|
||||
|
||||
return grouple
|
||||
|
||||
def compare_groups(course_uuid):
|
||||
compcount = 0
|
||||
complist = []
|
||||
|
||||
while True:
|
||||
compcount += 1
|
||||
# Endpoint is for list groups not yet associated with course
|
||||
compareurl = f"https://api2.northpass.com/v2/courses/{course_uuid}/available_groups?page={compcount}"
|
||||
comparedata = requests.get(compareurl, headers=HEADERS).json()
|
||||
nextlink = comparedata["links"]
|
||||
|
||||
for cresp in comparedata["data"]:
|
||||
complist.append(( cresp["id"], cresp["attributes"]["name"] ))
|
||||
tup = ( cresp["id"], cresp["attributes"]["name"] )
|
||||
print(tup)
|
||||
|
||||
if "next" not in nextlink:
|
||||
break
|
||||
|
||||
return complist
|
||||
|
||||
def merge_groups(associated, master):
|
||||
name_list = []
|
||||
combined = associated + master
|
||||
set_list = Counter(combined)
|
||||
duplicates_removed = [k for k,v in set_list.items() if v == 1]
|
||||
if len(duplicates_removed) > 0:
|
||||
for groups in duplicates_removed:
|
||||
name = groups[1]
|
||||
name_list.append(name)
|
||||
else:
|
||||
name_list = "None"
|
||||
return name_list
|
||||
|
||||
def get_course():
|
||||
groups = get_groups()
|
||||
count = 0
|
||||
courses = []
|
||||
|
||||
while True:
|
||||
count += 1
|
||||
url = f"https://api2.northpass.com/v2/courses?page={count}"
|
||||
data = requests.get(url, headers=HEADERS).json()
|
||||
nextlink = data["links"]
|
||||
|
||||
for response in data["data"]:
|
||||
status = response["attributes"]["status"]
|
||||
if status == "live":
|
||||
uuid = response["id"]
|
||||
name = response["attributes"]["name"]
|
||||
if name.startswith('Category Advisor'):
|
||||
print("COURSE NAME")
|
||||
print(name)
|
||||
print("----------")
|
||||
associated_list = compare_groups(uuid)
|
||||
final_groups = merge_groups(associated_list, groups)
|
||||
course_dict = {
|
||||
"id": uuid,
|
||||
"name": name,
|
||||
"status": status,
|
||||
"groups" : final_groups
|
||||
}
|
||||
courses.append(course_dict)
|
||||
cat_id = response["relationships"]["categories"]["data"]
|
||||
get_cat_name(cat_id, course_dict, courses)
|
||||
|
||||
if "next" not in nextlink:
|
||||
break
|
||||
|
||||
def get_cat_name(cat_id, course_dict, courses):
|
||||
if len(cat_id) == 0:
|
||||
cats = ["None"]
|
||||
else:
|
||||
cats = []
|
||||
for item in cat_id:
|
||||
categoryid = item["id"]
|
||||
url = f"https://api2.northpass.com/v2/categories/{categoryid}"
|
||||
cat_data = requests.get(url, headers=HEADERS).json()
|
||||
cat_name = cat_data["data"]["attributes"]["name"]
|
||||
cats.append(cat_name)
|
||||
course_dict.update({"categories": cats})
|
||||
|
||||
write_to_csv(courses)
|
||||
|
||||
|
||||
def write_to_csv(courses):
|
||||
df = pd.DataFrame.from_dict(courses)
|
||||
df.to_csv("/Users/normrasmussen/Downloads/supplier_courses.csv")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
get_course()
|
||||
Reference in New Issue
Block a user