diff --git a/Scripts/API_Tests/get_courses.py b/Scripts/API_Tests/get_courses.py index 935068f7..cc47c68e 100644 --- a/Scripts/API_Tests/get_courses.py +++ b/Scripts/API_Tests/get_courses.py @@ -1,14 +1,12 @@ import requests -import json +import pandas as pd import Apikeys - - def get_course(): count = 0 apiKey = Apikeys.walmartprod courses = [] - category_ids = [] + course_dict = {} while True: count += 1 @@ -18,31 +16,60 @@ def get_course(): data = response.json() nextlink = data["links"] - jsonResponse = response.json() - jsonResponse = json.dumps(jsonResponse, indent=2) - for response in data["data"]: status = response["attributes"]["status"] uuid = response["id"] name = response["attributes"]["name"] + course_dict = { + "id": uuid, + "name": name, + "status": status, + "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. - course_url = f"https://walmart.northpass.com/app/courses/{uuid}" cat_id = response["relationships"]["categories"]["data"] + cats = [] + + # Pseudo-code + if len(cat_id) == 0: + pass + elif len(cat_id) == 1: + get_cat_name(cat_id) + course_dict(update({"categories": cat})) + else: + for item in cat_id: + # make api call + # return the name + update the dictionary + for item in cat_id: + cats = [] categoryid = item["id"] - print(categoryid) - courses.append(name) - with open("/Users/normrasmussen/Downloads/courses.csv", "a") as write: - write.write("\n") - write.write(name+","+status+","+uuid) + # TODO: Maybe split the api call to a separate function? + url = f"https://api2.northpass.com/v2/categories/{categoryid}" + headers = {"accept": "application/json", "X-Api-Key": apiKey} + 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}) + courses.append(course_dict) if "next" not in nextlink: break - #print(courses) -# https://walmart.northpass.com/app/courses/ -def get_category_name(category_ids): + write_to_csv(courses) + +def get_cat_name(item): pass + +def write_to_csv(courses): + df = pd.DataFrame.from_dict(courses) + df.to_csv("/Users/normrasmussen/Downloads/walmart_courses.csv") + if __name__ == "__main__": get_course()