Last active
July 18, 2016 11:38
-
-
Save montycheese/ba2db151d82fe63a4b76 to your computer and use it in GitHub Desktop.
Requires Python Libraries: requests, json.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python2.7 | |
# Author:**** | |
# Education: **** | |
# Email: **** | |
# Phone: 808-633-**** | |
# Purpose: ZapposGiftProgram.py is my submission for Zappos' 2015 Summer | |
# Software Engineering Internship application. The program prompts the user for two | |
# inputs: the amount of money they want to spend, and how many gifts they want to purchase. | |
# The program searches through Zappos's website using its API and returns a series of | |
# gift "packages" containing the desired amount of gifts the user inputted, with its | |
# total price close or equal to the user's desired budget. | |
import requests | |
import json | |
api = "52ddafbe3ee659bad97fcce7c53592916a6bfd73" #unique api key | |
json_url = "https://api.zappos.com/Search?term=" | |
# the most popular searches on zappos.com as of 9/22/2014 | |
# this list may be also modified to narrow results to specific interests or to accept user's submitted input | |
popular_searches = [ | |
"UGG Boots", | |
"Zumba Shoes", | |
"Combat Boots", | |
"Bridal Shoes", | |
"Jeans for Women", | |
"Running Shoes", | |
"Aldo Shoes", | |
"Walking Shoes", | |
"Boots for Women", | |
"Dresses", | |
"Dansko Shoes", | |
"Booties", | |
"NYDJ Jeans", | |
"Shoes", | |
"Handbags", | |
"Uggs on Sale", | |
"Ecco Shoes", | |
"Shoes for Women", | |
"Clarks Shoes" | |
] | |
catalog = [] | |
item_brand_list = [] | |
master_price_list = [] | |
spread = 10 # Adjust to fine tune results. The lower the number, the closer the gift package | |
# will be to desired budget, and inversely. | |
# However, increasing the number will usually return more gift packages, and inversely. | |
#Grabs JSON data containing products and metadata from Zappos.com and converts to output-able type. | |
def find_products(budget, number_of_items, search_input): | |
search_term = search_input | |
print search_term | |
search = requests.get(json_url + '%s&limit=100&key=' % search_term + api) | |
js = search.json() | |
our_result = js['results'] | |
for item_data in our_result: | |
#typecasting each price from UNICODE STRING to INTEGER | |
string_removed_commas = str(item_data['price'])[1:].replace( ",", "") #removing commas found in prices over a thousand | |
master_price_list.append(int(float(string_removed_commas))) | |
#typecasting each product name from UNICODE STRING to String | |
catalog.append(str(item_data['productName'])) | |
#creating global list containing each item's brandname | |
item_brand_list.append(str(item_data['brandName'])) | |
return | |
# catching TypeErrors that arise when certain search terms do not throw back any products. | |
def output_products(search_input): | |
try: | |
find_products(budget, number_of_items, search_input) | |
except TypeError: | |
return | |
# Loop through entire catalog of Zappos' most popular products and create a list containing indicies of the | |
# items whose prices are most narrowly tailored to user budget. | |
# function returns and calls another function (output_grouped_products) to print results | |
def loop_through_entire_catalog(): | |
list_of_random_indicies = [] | |
budget_per_item = budget / number_of_items | |
for price_of_item in master_price_list: | |
if (price_of_item <= (budget_per_item - spread)) and (price_of_item >= budget_per_item + spread): | |
pass | |
elif (price_of_item >= budget_per_item - spread) and (price_of_item <= budget_per_item + spread): | |
list_of_random_indicies.append(master_price_list.index(price_of_item)) | |
else: | |
pass | |
return output_grouped_products(delete_duplicates(list_of_random_indicies)) | |
# delete duplicate indicies in list | |
def delete_duplicates(list): | |
seen = {} | |
position = 0 | |
for item in list: | |
if item not in seen: | |
seen[item] = True | |
list[position] = item | |
position += 1 | |
del list[position:] | |
return list | |
# Print out final groups of 'n' number of gifts that are closest to the budget. | |
def output_grouped_products(list_of_indicies): | |
total_cost = 0 | |
item_tally = 0 # keeps track of how many items are placed in each gift package | |
print "\n" | |
for indicies in list_of_indicies: | |
if item_tally == 0 and ((len(list_of_indicies) - list_of_indicies.index(indicies) + 1) <= number_of_items): | |
return | |
elif item_tally == 0: | |
total_cost += master_price_list[indicies] | |
item_tally += 1 | |
print "Next set of gifts:" | |
print "%s- %s: $%d.00" % (item_brand_list[indicies], catalog[indicies], master_price_list[indicies]) | |
elif item_tally <= number_of_items: | |
total_cost += master_price_list[indicies] | |
item_tally += 1 | |
print "%s- %s: $%d.00" % (item_brand_list[indicies], catalog[indicies], master_price_list[indicies]) | |
if item_tally == number_of_items: | |
print "Your budget for %d items was $%6.2f" % (number_of_items, budget_float) | |
print "The total cost of these %d Zappos products is $%d.00 \n" % (item_tally, total_cost) | |
total_cost = 0 | |
item_tally = 0 | |
else: | |
pass | |
else: | |
print "Program has encountered an error" | |
return | |
# Loops through all the search terms, call on output_products method to parse www.zappos.com | |
def loop_searches(budget, number_of_items): | |
print "Searching most popular keywords on www.zappos.com ... " | |
for search_input in popular_searches: | |
if search_input == popular_searches[len(popular_searches) - 1]: #last index of search list | |
output_products(search_input) | |
return loop_through_entire_catalog() #final loop through catalog | |
else: | |
output_products(search_input) | |
if __name__ == "__main__": | |
print "Welcome to ***'s Zappo.com Gift Combination Creator." | |
print "This program will use Zappos.com's most popular search terms to find you amazing gifts!" | |
budget_float = float(raw_input("What is your budget? \n>>$")) | |
budget = int(budget_float) | |
number_of_items = int(raw_input("How many gifts are you purchasing? \n>>")) | |
while number_of_items <= 1: | |
print "Please enter a number greater than 1" | |
number_of_items = int(raw_input("How many gifts are you purchasing? \n>>")) | |
loop_searches(budget, number_of_items) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run in python 2.7
Requires Python Libraries: requests, json