Skip to content

Instantly share code, notes, and snippets.

@bouk

bouk/asc-data.rb Secret

Created Jul 11, 2020
Embed
What would you like to do?
#!/usr/bin/env ruby
require 'date'
require 'fileutils'
require 'jwt'
require 'net/http'
Dir.chdir __dir__
VENDOR_ID = ENV.fetch('ASC_VENDOR_ID')
ISSUER_ID = ENV.fetch('ASC_ISSUER_ID')
KEY_ID = ENV.fetch('ASC_KEY_ID')
PRIVATE_KEY = OpenSSL::PKey.read(ENV.fetch('ASC_PRIVATE_KEY'))
def generate_jwt_token
JWT.encode(
{
iss: ISSUER_ID,
exp: Time.now.to_i + 20 * 60,
aud: 'appstoreconnect-v1'
},
PRIVATE_KEY,
'ES256',
{
kid: KEY_ID
}
)
end
data_dir = 'data'
FileUtils.mkdir_p data_dir
genesis = Date.new(2020, 05, 10)
range = genesis...Date.today
dates = range.reject {|d| File.exist? File.join(data_dir, "#{d}.tsv.gz") }
url = "https://api.appstoreconnect.apple.com/v1/salesReports?filter[reportType]=SALES&filter[reportSubType]=SUMMARY&filter[frequency]=DAILY&filter[vendorNumber]=#{VENDOR_ID}&filter[reportDate]="
token = generate_jwt_token
Net::HTTP.start('api.appstoreconnect.apple.com', 443, use_ssl: true) do |http|
dates.each do |date|
print "Getting #{date} "
request = Net::HTTP::Get.new("#{url}#{date}")
request['Authorization'] = "Bearer #{token}"
request['Accept-Encoding'] = "identity"
http.request(request) do |response|
if response.code == '404'
puts "not found"
next
end
throw response.body unless response.code == '200'
datafile = File.join(data_dir, "#{date}.tsv.gz")
open(datafile, 'w') do |f|
response.read_body do |chunk|
f.write chunk
end
end
puts "done!"
end
end
end
puts 'Done!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.