-
-
Save kccheung/5b6e658de5a7c819e187095067c1954f to your computer and use it in GitHub Desktop.
Script to check stock at apple stores
This file contains 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
""" | |
For more part numbers for other phone models etc, look here: | |
http://www.everyi.com/by-identifier/ipod-iphone-ipad-specs-by-apple-order-number-part-number.html | |
""" | |
import requests | |
import urllib | |
BASE_URL = "http://www.apple.com/shop/retailStore/availabilitySearch?" | |
PARTS = {'verizon': {'silver': {'16G': 'MLM02LL/A', '64G': 'MLMG2LL/A'}, | |
'gray': {'16G': 'MLLY2LL/A', '64G': 'MLMF2LL/A'}, | |
'gold': {'16G': 'MLY52LL/A', '64G': 'MLY72LL/A'}, | |
'rose': {'16G': 'MLY62LL/A', '64G': 'MLY82LL/A'}}, | |
'at&t': {'silver': {'16G': 'MLLM2LL/A', '64G': 'MLM52LL/A'}, | |
'gray': {'16G': 'MLLL2LL/A', '64G': 'MLM42LL/A'}, | |
'gold': {'16G': 'MLXH2LL/A', '64G': 'MLXK2LL/A'}, | |
'rose': {'16G': 'MLXJ2LL/A', '64G': 'MLXL2LL/A'}}, | |
'unlocked': {'silver': {'16G': 'MLLX2LL/A', '64G': 'MLME2LL/A'}, | |
'gray': {'16G': 'MLLW2LL/A', '64G': 'MLMD2LL/A'}, | |
'gold': {'16G': 'MLY12LL/A', '64G': 'MLY32LL/A'}, | |
'rose': {'16G': 'MLY22LL/A', '64G': 'MLY42LL/A'}}} | |
def make_reverse_lookup(): | |
lookup = {} | |
for carrier in PARTS: | |
for color in PARTS[carrier]: | |
for storage in PARTS[carrier][color]: | |
lookup[PARTS[carrier][color][storage]] = "{} {} {}".format(carrier, color, storage) | |
return lookup | |
def build_url(part_number_list, zipcode): | |
query_dict = {'location': zipcode} | |
for i, part in enumerate(part_number_list): | |
query_dict['parts.'+str(i)] = part | |
query = urllib.parse.urlencode(query_dict) | |
return BASE_URL + query | |
def check_stock(url, max_stores=5): | |
lookup = make_reverse_lookup() | |
response = requests.get(url) | |
data = response.json() | |
for store in data['body']['stores'][:max_stores]: | |
for part, status in store['partsAvailability'].items(): | |
print("{:20} {:10} at {}".format(lookup[part], status['pickupDisplay'], store['storeDisplayName'])) | |
if __name__ == "__main__": | |
zipcode = '02138' | |
partslist = [PARTS['verizon']['silver']['64G'], | |
PARTS['verizon']['gray']['64G']] | |
url = build_url(partslist, zipcode) | |
check_stock(url, max_stores=5) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment