Skip to content

Instantly share code, notes, and snippets.

@yaakov-h
Forked from alexeckermann/iphone6.rb
Last active October 2, 2015 02:59
Show Gist options
  • Save yaakov-h/cd75c87029c3da008f06 to your computer and use it in GitHub Desktop.
Save yaakov-h/cd75c87029c3da008f06 to your computer and use it in GitHub Desktop.
iPhone 6s Reservation Availability in Australia
# 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 = {
"MKQT2X/A" => "iPhone 6s Space Grey 128GB",
"MKU52X/A" => "iPhone 6s+ Rose Gold 16GB",
"MKQK2X/A" => "iPhone 6s Silver 16GB",
"MKU92X/A" => "iPhone 6s+ Rose Gold 64GB",
"MKQW2X/A" => "iPhone 6s Rose Gold 128GB",
"MKU32X/A" => "iPhone 6s+ Gold 16GB",
"MKUD2X/A" => "iPhone 6s+ Space Grey 128GB",
"MKQR2X/A" => "iPhone 6s Rose Gold 64GB",
"MKUE2X/A" => "iPhone 6s+ Solver 128GB",
"MKQN2X/A" => "iPhone 6s Space Grey 64GB",
"MKQJ2X/A" => "iPhone 6s Space Grey 16GB",
"MKU82X/A" => "iPhone 6s+ Gold 64GB",
"MKU72X/A" => "iPhone 6s+ Silver 64GB",
"MKQV2X/A" => "iPhone 6s Gold 128GB",
"MKU22X/A" => "iPhone 6s+ Silver 16GB",
"MKQQ2X/A" => "iPhone 6s Gold 64GB",
"MKQM2X/A" => "iPhone 6s Rose Gold 16GB",
"MKUF2X/A" => "iPhone 6s+ Gold 128GB",
"MKQP2X/A" => "iPhone 6s Silver 64GB",
"MKUG2X/A" => "iPhone 6s+ Rose Gold 128GB",
"MKQU2X/A" => "iPhone 6s Silver 128GB",
"MKU12X/A" => "iPhone 6s+ Space Grey 16GB",
"MKQL2X/A" => "iPhone 6s Gold 16GB",
"MKU62X/A" => "iPhone 6s+ Space Grey 64GB",
}
module Utils
def self.get_json(url)
JSON.parse(open(url, "User-Agent" => "Ruby/2.1.0; personal-use-only (alex@alexeckermann.com);", "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 != 'NONE' }
available_models = available_models.select{ |model_no| model_no != 'timeSlot' && model_no != 'launchDate' }
if available_models.length > 0
store_description = stores['stores'].detect { |s| s['storeNumber'] == store_identifier }
store_description ||= { 'storeName' => 'Unknown Store' }
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, model_availability|
next if model_no == 'timeSlot'
available = model_availability != 'NONE'
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