Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import json
- from urllib.request import urlopen
- """
- Not a web guy, but here's my best shot!
- This program gets 1 of each product of your choice.
- For this example, the default keywords are Watches
- and Clocks. The total is 2290.74$ if you buy one
- of each of these items.
- Author: Eric Tang
- Date: 2016-09-27
- Run using Python 3
- """
- # Store all JSON files into one array
- # Choose the number of pages you want to search
- def shopicruit_pages(number_of_pages):
- url = "http://shopicruit.myshopify.com/products.json?page=PAGE_NUMBA"
- json_array = []
- for x in range(1, number_of_pages):
- url_pages = url.replace("PAGE_NUMBA", str(x))
- response = urlopen(url_pages).read().decode('utf8')
- json_array.append(json.loads(response))
- return json_array
- # Sum the cost for any item on each page
- def cost_per_page(json_page, item):
- cost = 0
- for _ in json_page:
- for x in range(0, len(json_page["products"])):
- # Find item name in the tag
- if item in json_page["products"][x]["tags"]:
- # Sum the price of every variant
- for i in range(0, len(json_page["products"][x]["variants"])):
- cost += float(json_page["products"][x]["variants"][i]["price"])
- return cost
- def total_cost(keywords):
- # Search the first 5 JSON pages
- # I noticed that the rest of the pages were empty O.O
- pages = shopicruit_pages(6)
- price = 0
- while pages:
- page = pages.pop()
- for word in keywords:
- price += cost_per_page(page, word)
- return price
- # List of keywords to filter by
- keyword_list = ['Clock', 'Watch']
- print("I think what you heard was, \"I want a lot of watches and clocks\".")
- print("What I really said was, I want ALL OF YOUR WATCHES AND CLOCKS.")
- print("Total price of Watches and Clocks: {}".format(total_cost(keyword_list)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement