Skip to content

Instantly share code, notes, and snippets.

@stevejackson
Created September 14, 2011 00:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stevejackson/1215551 to your computer and use it in GitHub Desktop.
Save stevejackson/1215551 to your computer and use it in GitHub Desktop.
py script
#!/usr/bin/env python
# Steve Jackson - Macy's script for Milo!
# example usage:
# ./pymacy 94404 'http://www.macys.com/catalog/product/index.ognc?ID=355333'
import sys
import urllib2
import urllib
import json
import urlparse
# shell help
if sys.argv[1] == "-h" or sys.argv[1] == "--h" or sys.argv[1] == "--help":
print ''
print " ./pymacy [zipcode] [full-product-url] OPTIONAL: [size] [color]"
print ''
sys.exit()
# do we even have 3 parameters?
if len(sys.argv) < 3:
sys.exit("Incorrect number of arguments. Run ./pymacy --help for info.")
# grab the script parameters
zipCode = sys.argv[1]
size = ""
color = ""
productId = ""
# try to grab the product ID from the url passed
try:
parsed_url = urlparse.urlparse(sys.argv[2])
query_dict = urlparse.parse_qs(parsed_url[4])
productId = query_dict['ID'][0]
except:
print "Couldn't find a product ID from your link!"
sys.exit("")
url = 'http://www.macys.com/store/storeavailability/index.ognc'
# did they pass size and/or color?
if len(sys.argv) >= 4:
size = sys.argv[3]
if len(sys.argv) >= 5:
color = sys.argv[4]
else:
color = 'No Color'
#########################
# First HTTP Request - GET - Query to get upcID
data = {
'productID': productId,
'size': size,
'color': color,
'type': '',
'zipCode': zipCode }
values = urllib.urlencode(data)
response = urllib.urlopen(url + "?%s" % values)
# Evaluate our response to get the upcID.
json_data = json.loads(response.read())
try:
upcId = json_data['productThumbnailVB']['upcId']
except:
print "Couldn't retrieve the upcId for this item. Perhaps you have an incorrect product ID, size, or color?"
sys.exit("")
#########################
# Second HTTP Request - POST - Query to get store availability info
# HTTP request parameters to encode and send off
data = {
'productID': productId,
'upcID': upcId,
'size': size,
'color': color,
'zipCode': zipCode }
values = urllib.urlencode(data)
request = urllib2.Request(url, values)
response = urllib2.urlopen(request)
# awesome, let's check out our json response data now
json_data = json.loads(response.read())
try:
search_results = json_data['inStoreAvailabilityVB']['searchResults']
except:
print "Couldn't find store availability for this item."
sys.exit("")
###############################
# Now let's just parse the results and output them!
# represents a store location
class Location:
name = ""
address = ""
availability = ""
locations = []
# parse the results from our store availability query
for entry in search_results:
# create a Location to store them in
l = Location()
l.name = entry['storeName']
l.availability = entry['availabilityStatus']
# constructing our address takes a bit of work
address_object = entry['addressVB']
l.address = address_object['address1'].strip()
l.address += ' / '
l.address += address_object['city'].strip()
l.address += ', '
l.address += address_object['state'].strip()
l.address += ' '
l.address += address_object['zipCode'].strip()
# add it to our locations list
locations.append(l)
# output the locations.
for loc in locations:
print loc.name
print loc.address
print loc.availability
print ''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment