Created
April 14, 2009 16:42
-
-
Save somebox/95279 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/ruby | |
# logs in to iTunes connect, downloads the day's report file and saves it in proper csv format. | |
# by Jeremy Seitz ~ http://fozworks.com | |
# | |
# REQUIRES Mechanize GEM: | |
# sudo gem install mechanize | |
require 'rubygems' | |
require 'mechanize' | |
require 'logger' | |
require "csv" | |
require 'optparse' | |
# ------------- | |
BASE = "https://phobos.apple.com" | |
LOGIN_URL = "#{BASE}/WebObjects/MZLabel.woa/wa/default" | |
def parse_options | |
@csv_path = "." | |
@verbose = false | |
@user = nil | |
@password = nil | |
@title = 'appstore' | |
ARGV.options do |opt| | |
opt.on("-u", "--username=username", "Apple iTunes Connect User Name") { |user| @user = user } | |
opt.on("-p", "--password=password", "Apple iTunes Connect Password") { |pass| @password = pass } | |
opt.on('-d', "--destination=#{@csv_path}", 'Specify a path for writing the csv file') { |path| @csv_path = path } | |
opt.on('-t', "--title=#{@title}", 'Specify a title for the csv file') { |title| @title = title } | |
opt.on('-v', '--verbose', 'Be verbose') { @verbose = true } | |
opt.on('-h', '--help', 'Show this message.') { puts opt; exit 0 } | |
opt.parse! | |
if (!@user or !@password) | |
puts "Error: must supply username and password #{@user}/#{@password}" | |
puts opt; exit 0; | |
end | |
end | |
end | |
def download_report | |
puts "Connecting to #{LOGIN_URL[0..30]} as #{@user}..." | |
agent = WWW::Mechanize.new #{|a| a.log = Logger.new(STDERR) } | |
agent.get(LOGIN_URL) do |login_page| | |
puts "Logging in..." | |
login_form = login_page.form_with(:name => 'appleConnectForm') do |form| | |
form.field_with(:name => 'theAccountName').value = @user | |
form.field_with(:name => 'theAccountPW').value = @password | |
end | |
login_form.submit | |
puts "Loading report page..." | |
report_page = agent.get("https://itts.apple.com/cgi-bin/WebObjects/Piano.woa") | |
report_form = report_page.form_with(:name => 'frmVendorPage') do |form| | |
if !form | |
puts "Error: Login failed, unable to download reports" | |
exit(0) | |
else | |
pp form.fields if @verbose | |
form['9.7'] = 'Summary' | |
form['9.9'] = 'Daily' | |
form['hiddenSubmitTypeName'] = 'ShowDropDown' | |
end | |
end | |
puts "Loading next report page..." | |
next_page = report_form.submit | |
puts next_page.body if @verbose | |
report_2_form = next_page.form_with(:name => 'frmVendorPage') do |form| | |
form['9.7'] = 'Summary' | |
form['9.9'] = 'Daily' | |
form['download'] = 'Download' | |
form['hiddenSubmitTypeName'] = 'Download' | |
date_select = form.field_with(:name => '9.11.1') | |
@date = date_select.options.first.value | |
form['9.11.1'] = @date | |
form['hiddenDayOrWeekSelection'] = @date | |
end | |
puts "Downloading report for #{@date}" | |
report = report_2_form.submit | |
pp report.body if @verbose | |
rows = report.body.split("\n") | |
@rows = rows.map{|r| r.split("\t")} | |
end | |
end | |
def write_csv | |
filename = @title + '-' + @date.gsub('/','-') + '.csv' | |
destination = File.join(@csv_path,filename) | |
puts "Writing CSV file to #{destination}" | |
CSV.open(destination, 'w') do |csv| | |
@rows.each do |result| | |
csv << result | |
end | |
end | |
end | |
def main | |
parse_options | |
download_report | |
write_csv | |
puts "Done." | |
end | |
main | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment