Created
October 3, 2014 01:11
-
-
Save alexeckermann/829ff14c69675cba157b to your computer and use it in GitHub Desktop.
iPhone 6 Reservation Availability in Australia
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
# iPhone 6 Reservation Availability in Australia | |
# !! Please use responsibly. Personal use only !! | |
# `ruby iphone6.rb` - list all available models in all stores | |
# `ruby iphone6.rb R405` - list available models for a specific store, Rundle Place in this example. | |
require 'open-uri' | |
require 'json' | |
MODEL_NAMES = { | |
"MG4A2X/A" => "iPhone 6 Space Grey 128GB", | |
"MGAK2X/A" => "iPhone 6+ Gold 64GB", | |
"MGA82X/A" => "iPhone 6+ Space Grey 16GB", | |
"MGAF2X/A" => "iPhone 6+ Gold 128GB", | |
"MG472X/A" => "iPhone 6 Space Grey 16GB", | |
"MG4F2X/A" => "iPhone 6 Space Grey 64GB", | |
"MG482X/A" => "iPhone 6 Silver 16GB", | |
"MGA92X/A" => "iPhone 6+ Silver 16GB", | |
"MGAJ2X/A" => "iPhone 6+ Silver 64GB", | |
"MGAE2X/A" => "iPhone 6+ Silver 128GB", | |
"MG4J2X/A" => "iPhone 6 Gold 64GB", | |
"MG4E2X/A" => "iPhone 6 Gold 128GB", | |
"MGAH2X/A" => "iPhone 6+ Space Grey 64GB", | |
"MG4C2X/A" => "iPhone 6 Silver 128GB", | |
"MGAA2X/A" => "iPhone 6+ Gold 16GB", | |
"MG4H2X/A" => "iPhone 6 Silver 64GB", | |
"MG492X/A" => "iPhone 6 Gold 16GB", | |
"MGAC2X/A" => "iPhone 6+ Space Grey 128GB" | |
} | |
module Utils | |
def self.get_json(url) | |
JSON.parse(open(url, "User-Agent" => "Ruby/2.1.0; personal-use-only ([email protected]);", "Referer" => "http://alexeckermann.com").read) rescue nil | |
end | |
end | |
module Commands | |
def self.list_all(stores, availability) | |
availability.each do |store_identifier, model_availability| | |
next unless model_availability.is_a?(Hash) | |
available_models = [] | |
model_availability.each { |model_no, available| available_models.push(model_no) if available } | |
store_description = stores['stores'].detect { |s| s['storeNumber'] == store_identifier } | |
if available_models.length > 0 | |
puts "\033[1;39;49m#{store_description['storeName']}\033[0m (#{store_identifier})" | |
puts available_models.map { |model_no| "\033[0;32;49m#{MODEL_NAMES[model_no]}\033[0m" }.join(', ') | |
end | |
end | |
end | |
def self.list_store(store, availability) | |
availability.each do |model_no, available| | |
colour = available ? 32 : 31 | |
puts "#{MODEL_NAMES[model_no]}: \033[1;#{colour};49m#{available ? 'AVAILABLE' : 'NOPE'}\033[0m\n" | |
end | |
end | |
end | |
begin | |
stores = Utils::get_json("https://reserve.cdn-apple.com/AU/en_AU/reserve/iPhone/stores.json") | |
availability = Utils::get_json("https://reserve.cdn-apple.com/AU/en_AU/reserve/iPhone/availability.json") | |
rescue => e | |
puts "Exception: #{e.message}" | |
exit 1 | |
end | |
updated_at = Time.at(availability.delete('updated') / 1000).strftime('%d %b, %H:%M:%S %Z') rescue nil | |
case ARGV.length | |
when 0 | |
puts "\033[4;39;49mListing stores with stock availability\033[0m\n" | |
puts "-- Updated at: #{updated_at} --\n" | |
Commands::list_all(stores, availability) | |
when 1 | |
store_identifier = ARGV.shift | |
store_description = stores['stores'].detect { |s| s['storeNumber'] == store_identifier } | |
puts "\033[1;39;49m#{store_description['storeName']}\033[0m\n" | |
puts "-- Updated at: #{updated_at} --\n" | |
Commands::list_store(store_identifier, availability[store_identifier]) | |
else | |
puts '¯\_(ツ)_/¯' | |
end | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment