Advertisement
Guest User

ClawksClawksClawks

a guest
Sep 27th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. import json
  2. from urllib.request import urlopen
  3. """
  4. Not a web guy, but here's my best shot!
  5. This program gets 1 of each product of your choice.
  6.  
  7. For this example, the default keywords are Watches
  8. and Clocks. The total is 2290.74$ if you buy one
  9. of each of these items.
  10.  
  11. Author: Eric Tang
  12. Date: 2016-09-27
  13. Run using Python 3
  14. """
  15.  
  16.  
  17. # Store all JSON files into one array
  18. # Choose the number of pages you want to search
  19. def shopicruit_pages(number_of_pages):
  20.     url = "http://shopicruit.myshopify.com/products.json?page=PAGE_NUMBA"
  21.     json_array = []
  22.  
  23.     for x in range(1, number_of_pages):
  24.         url_pages = url.replace("PAGE_NUMBA", str(x))
  25.         response = urlopen(url_pages).read().decode('utf8')
  26.         json_array.append(json.loads(response))
  27.  
  28.     return json_array
  29.  
  30.  
  31. # Sum the cost for any item on each page
  32. def cost_per_page(json_page, item):
  33.     cost = 0
  34.  
  35.     for _ in json_page:
  36.         for x in range(0, len(json_page["products"])):
  37.             # Find item name in the tag
  38.             if item in json_page["products"][x]["tags"]:
  39.                 # Sum the price of every variant
  40.                 for i in range(0, len(json_page["products"][x]["variants"])):
  41.                     cost += float(json_page["products"][x]["variants"][i]["price"])
  42.  
  43.     return cost
  44.  
  45.  
  46. def total_cost(keywords):
  47.     # Search the first 5 JSON pages
  48.     # I noticed that the rest of the pages were empty O.O
  49.     pages = shopicruit_pages(6)
  50.     price = 0
  51.  
  52.     while pages:
  53.         page = pages.pop()
  54.         for word in keywords:
  55.             price += cost_per_page(page, word)
  56.  
  57.     return price
  58.  
  59. # List of keywords to filter by
  60. keyword_list = ['Clock', 'Watch']
  61.  
  62.  
  63. print("I think what you heard was, \"I want a lot of watches and clocks\".")
  64. print("What I really said was, I want ALL OF YOUR WATCHES AND CLOCKS.")
  65. print("Total price of Watches and Clocks: {}".format(total_cost(keyword_list)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement