Finished Walmart script. Agriwebb notes.

This commit is contained in:
Norm Rasmussen
2023-03-30 21:18:23 -04:00
parent c74649e16b
commit 478b21c8ef
3 changed files with 75 additions and 34 deletions

View File

@ -2,11 +2,12 @@ import requests
import pandas as pd
import Apikeys
apiKey = Apikeys.walmartprod
course_dict = {}
def get_course():
count = 0
apiKey = Apikeys.walmartprod
courses = []
course_dict = {}
while True:
count += 1
@ -31,40 +32,42 @@ def get_course():
# FIX: After this, something is being overwritten.
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"]
# 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)
get_cat_name(cat_id, course_dict, courses)
if "next" not in nextlink:
break
write_to_csv(courses)
def get_cat_name(cat_id, course_dict, courses):
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://api2.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://api2.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})
def get_cat_name(item):
pass
try:
courses.append(course_dict)
except TypeError as e:
print(f"Error: {e}")
finally:
write_to_csv(courses)
def write_to_csv(courses):