Last active
December 23, 2015 17:49
-
-
Save scelis/6671834 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# | |
# This script can be used to check the in-store availability of the iPhone 5S. | |
# By default, it searches for all AT&T models. You can change this by editing | |
# the MY_DEVICES array below. While the Apple API supports searching for | |
# multiple devices at once, there is a limit. | |
# | |
# Once you have properly configured the MY_DEVICES array, you can run this script | |
# from the terminal. It takes a single parameter, which should be the zip code you | |
# wish to use for your search. | |
# | |
# Example: | |
# $ ./availability.rb 94102 | |
# | |
require 'cgi' | |
require 'json' | |
require 'net/http' | |
require 'uri' | |
BASE_URL = 'http://store.apple.com/us/retail/availabilitySearch' | |
ALL_DEVICES = [ | |
{'name' => 'Gold 16GB AT&T', 'part_number' => 'ME307LL/A'}, | |
{'name' => 'Gold 32GB AT&T', 'part_number' => 'ME310LL/A'}, | |
{'name' => 'Gold 64GB AT&T', 'part_number' => 'ME313LL/A'}, | |
{'name' => 'Silver 16GB AT&T', 'part_number' => 'ME306LL/A'}, | |
{'name' => 'Silver 32GB AT&T', 'part_number' => 'ME309LL/A'}, | |
{'name' => 'Silver 64GB AT&T', 'part_number' => 'ME312LL/A'}, | |
{'name' => 'Space Gray 16GB AT&T', 'part_number' => 'ME305LL/A'}, | |
{'name' => 'Space Gray 32GB AT&T', 'part_number' => 'ME308LL/A'}, | |
{'name' => 'Space Gray 64GB AT&T', 'part_number' => 'ME311LL/A'}, | |
{'name' => 'Gold 16GB Sprint', 'part_number' => 'ME352LL/A'}, | |
{'name' => 'Gold 32GB Sprint', 'part_number' => 'ME355LL/A'}, | |
{'name' => 'Gold 64GB Sprint', 'part_number' => 'ME358LL/A'}, | |
{'name' => 'Silver 16GB Sprint', 'part_number' => 'ME351LL/A'}, | |
{'name' => 'Silver 32GB Sprint', 'part_number' => 'ME354LL/A'}, | |
{'name' => 'Silver 64GB Sprint', 'part_number' => 'ME357LL/A'}, | |
{'name' => 'Space Gray 16GB Sprint', 'part_number' => 'ME350LL/A'}, | |
{'name' => 'Space Gray 32GB Sprint', 'part_number' => 'ME353LL/A'}, | |
{'name' => 'Space Gray 64GB Sprint', 'part_number' => 'ME356LL/A'}, | |
{'name' => 'Gold 16GB Verizon', 'part_number' => 'ME343LL/A'}, | |
{'name' => 'Gold 32GB Verizon', 'part_number' => 'ME346LL/A'}, | |
{'name' => 'Gold 64GB Verizon', 'part_number' => 'ME349LL/A'}, | |
{'name' => 'Silver 16GB Verizon', 'part_number' => 'ME342LL/A'}, | |
{'name' => 'Silver 32GB Verizon', 'part_number' => 'ME345LL/A'}, | |
{'name' => 'Silver 64GB Verizon', 'part_number' => 'ME348LL/A'}, | |
{'name' => 'Space Gray 16GB Verizon', 'part_number' => 'ME341LL/A'}, | |
{'name' => 'Space Gray 32GB Verizon', 'part_number' => 'ME344LL/A'}, | |
{'name' => 'Space Gray 64GB Verizon', 'part_number' => 'ME347LL/A'}, | |
{'name' => 'Gold 16GB T-Mobile / Unlocked GSM', 'part_number' => 'ME325LL/A'}, | |
{'name' => 'Gold 32GB T-Mobile / Unlocked GSM', 'part_number' => 'ME328LL/A'}, | |
{'name' => 'Gold 64GB T-Mobile / Unlocked GSM', 'part_number' => 'ME331LL/A'}, | |
{'name' => 'Silver 16GB T-Mobile / Unlocked GSM', 'part_number' => 'ME324LL/A'}, | |
{'name' => 'Silver 32GB T-Mobile / Unlocked GSM', 'part_number' => 'ME327LL/A'}, | |
{'name' => 'Silver 64GB T-Mobile / Unlocked GSM', 'part_number' => 'ME330LL/A'}, | |
{'name' => 'Space Gray 16GB T-Mobile / Unlocked GSM', 'part_number' => 'ME323LL/A'}, | |
{'name' => 'Space Gray 32GB T-Mobile / Unlocked GSM', 'part_number' => 'ME326LL/A'}, | |
{'name' => 'Space Gray 64GB T-Mobile / Unlocked GSM', 'part_number' => 'ME329LL/A'}, | |
] | |
MY_DEVICES = [ | |
ALL_DEVICES[0], | |
ALL_DEVICES[1], | |
ALL_DEVICES[2], | |
ALL_DEVICES[3], | |
ALL_DEVICES[4], | |
ALL_DEVICES[5], | |
ALL_DEVICES[6], | |
ALL_DEVICES[7], | |
ALL_DEVICES[8], | |
] | |
def search(devices, zip) | |
url = "#{BASE_URL}?zip=#{zip}" | |
i = 0 | |
for device in devices | |
url = url + "&parts.#{i}=#{CGI::escape(device["part_number"])}" | |
i = i + 1 | |
end | |
uri = URI(url) | |
http = Net::HTTP.new(uri.host, uri.port) | |
request = Net::HTTP::Get.new(uri.request_uri) | |
response = http.request(request) | |
data = JSON.parse(response.body) | |
for item in data["body"]["stores"] | |
for device in devices | |
part_info = item["partsAvailability"][device["part_number"]] | |
if part_info["pickupSearchQuote"] != "Unavailable for Pickup" | |
puts "#{device["name"]} -- #{item["storeDisplayName"]} -- #{part_info["pickupSearchQuote"]}" | |
end | |
end | |
end | |
end | |
if ARGV.length == 0 | |
puts "Usage: availability.rb ZIPCODE" | |
else | |
search(MY_DEVICES, ARGV[0]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment