Skip to content

Instantly share code, notes, and snippets.

@alyssais
Created May 15, 2013 20:12
Show Gist options
  • Save alyssais/5586979 to your computer and use it in GitHub Desktop.
Save alyssais/5586979 to your computer and use it in GitHub Desktop.
Calculates the time that Apple will reach 50 billion apps.
require 'net/http'
require 'json'
puts ""
MAX = 50 * 10 ** 9 # 50 billion
# Get the URL of the JSON in case Apple changes it.
url = URI.parse 'http://images.apple.com/v/itunes/shared/counter/a/scripts/counter.js'
req = Net::HTTP::Get.new url.path
res = Net::HTTP.start(url.host, url.port) { |http| http.request(req) }
dataURL = URI.parse "http://www.apple.com#{res.body.scan(/dataURL:"(\/itunes\/store\/counters\/.*?\.)/).flatten[0]}js"
dataReq = Net::HTTP::Get.new dataURL.path
dataRes = Net::HTTP.start(dataURL.host, dataURL.port) { |http| http.request(dataReq) }
data = JSON.parser.new(dataRes.body).parse
puts "###### AT TIME OF SAMPLE ######"
puts data["count"].to_s + " apps sold"
appsLeft = MAX - data["count"]
puts appsLeft.to_s + " apps to go"
hours = appsLeft.to_f / data["rate"]
hoursAgo = (Time.now - Time.at(data["sampleTimestamp"].to_f)) / 3600
puts "sample taken ~#{(hoursAgo * 60).to_i} minutes ago."
nowAppsSold = (data["count"] + hoursAgo * data["rate"])
puts ""
puts "###### NOW (#{Time.now}) ######"
puts "~#{nowAppsSold.to_i} apps sold"
puts "~#{(MAX - nowAppsSold).to_i} apps to go"
puts ""
puts "###### RESULTS ######"
puts "will be finished in ~#{(hours + 0.5).to_i}h at " + Time.at(data["sampleTimestamp"].to_f + hours * 3600).to_s
puts ""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment