Luminate templates. Categories script to account for error handling.

This commit is contained in:
Norm Rasmussen
2024-06-26 16:44:00 -04:00
parent 01b709c14f
commit 232247df8d
5 changed files with 214 additions and 14 deletions

99
Scripts/API_Tests/; Normal file
View File

@ -0,0 +1,99 @@
import requests
import pandas as pd
import Apikeys
import pprint
pp = pprint.PrettyPrinter(indent=4)
apiKey = Apikeys.SKUID
course_dict = {}
COURSES= [
"Shopping an order",
"Delivery FAQs",
"Checking out Shopping & Delivery orders",
"Shopping & Delivery FAQs",
"Communicating with customers",
"Delivering an order",
]
def get_course():
count = 0
courses = []
while True:
count += 1
url = f"https://api.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"]
# build_url = response["links"]["builder"]["href"]
# if name in COURSES:
print(uuid)
course_dict = {
"id": uuid,
"name": name,
"status": status,
# "build_url": build_url
# "url": f"https://walmart.northpass.com/app/courses/{uuid}",
}
courses.append(course_dict)
# print(courses)
# FIX: Up until here, each course gets read to the terminal.
# FIX: After this, something is being overwritten.
# cat_id = response["relationships"]["categories"]["data"]
# get_cat_name(cat_id, course_dict, courses)
if "next" not in nextlink:
break
# write_to_csv(courses)
# pp.pprint(courses)
# print(len(courses))
def get_cat_name(course_dict):
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"]
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"]
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)
def write_to_csv(courses):
df = pd.DataFrame.from_dict(courses)
df.to_csv("/Users/normrasmussen/Downloads/sps_courses.csv")
if __name__ == "__main__":
get_course()

View File

@ -4,7 +4,7 @@ import Apikeys
import pprint
pp = pprint.PrettyPrinter(indent=4)
apiKey = Apikeys.SPS
apiKey = Apikeys.SKUID
course_dict = {}
COURSES= [
@ -48,13 +48,13 @@ def get_course():
# FIX: Up until here, each course gets read to the terminal.
# FIX: After this, something is being overwritten.
# cat_id = response["relationships"]["categories"]["data"]
# get_cat_name(cat_id, course_dict, courses)
cat_id = response["relationships"]["categories"]["data"]
get_cat_name(cat_id, course_dict, courses)
if "next" not in nextlink:
break
write_to_csv(courses)
# write_to_csv(courses)
# pp.pprint(courses)
# print(len(courses))
@ -68,10 +68,14 @@ def get_cat_name(cat_id, course_dict, courses):
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})
try:
cat_name = cat_data["data"]["attributes"]["name"]
cats.append(cat_name)
course_dict.update({"categories": cats})
except KeyError:
print(f"key error for cat {cat_id}")
finally:
pass
else:
for item in cat_id:
categoryid = item["id"]