Worked on Walmart script. This will also be useful for the Flask app
This commit is contained in:
@ -1,14 +1,12 @@
|
|||||||
import requests
|
import requests
|
||||||
import json
|
import pandas as pd
|
||||||
import Apikeys
|
import Apikeys
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_course():
|
def get_course():
|
||||||
count = 0
|
count = 0
|
||||||
apiKey = Apikeys.walmartprod
|
apiKey = Apikeys.walmartprod
|
||||||
courses = []
|
courses = []
|
||||||
category_ids = []
|
course_dict = {}
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
count += 1
|
count += 1
|
||||||
@ -18,31 +16,60 @@ def get_course():
|
|||||||
data = response.json()
|
data = response.json()
|
||||||
nextlink = data["links"]
|
nextlink = data["links"]
|
||||||
|
|
||||||
jsonResponse = response.json()
|
|
||||||
jsonResponse = json.dumps(jsonResponse, indent=2)
|
|
||||||
|
|
||||||
for response in data["data"]:
|
for response in data["data"]:
|
||||||
status = response["attributes"]["status"]
|
status = response["attributes"]["status"]
|
||||||
uuid = response["id"]
|
uuid = response["id"]
|
||||||
name = response["attributes"]["name"]
|
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"]
|
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:
|
for item in cat_id:
|
||||||
|
cats = []
|
||||||
categoryid = item["id"]
|
categoryid = item["id"]
|
||||||
print(categoryid)
|
# TODO: Maybe split the api call to a separate function?
|
||||||
courses.append(name)
|
url = f"https://api2.northpass.com/v2/categories/{categoryid}"
|
||||||
with open("/Users/normrasmussen/Downloads/courses.csv", "a") as write:
|
headers = {"accept": "application/json", "X-Api-Key": apiKey}
|
||||||
write.write("\n")
|
cat_resp = requests.get(url, headers=headers)
|
||||||
write.write(name+","+status+","+uuid)
|
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:
|
if "next" not in nextlink:
|
||||||
break
|
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
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def write_to_csv(courses):
|
||||||
|
df = pd.DataFrame.from_dict(courses)
|
||||||
|
df.to_csv("/Users/normrasmussen/Downloads/walmart_courses.csv")
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
get_course()
|
get_course()
|
||||||
|
|||||||
Reference in New Issue
Block a user