A simple way to archive iTC sales reports

Published on 17/06/2012

Although there are already a number of great reporting tools available for iTunes Connect, I wanted a simple script that I could use to fetch and archive hardcopies of daily sales data. So, in an attempt to re-invent the wheel (but hopefully learning something in the process), I decided to write one myself. You’ll find it attached below and also over at GitHub.

I’m fairly green when it comes to shell scripting, so I’d welcome any criticism or feedback.

#! /bin/sh
# ------------------------------------------------------------
#
# Uses Apple's Autoingestion Java class to fetch
# daily iTunes Connect reports from X days ago
# by Nial Giacomell (http://nial.me)
#
# requires: Autoingestion.class found at:
# http://www.apple.com/itunesnews/docs/AppStoreReportingInstructions.pdf
#
# usage: sh itcreportfetch.sh days_ago
# 
# ------------------------------------------------------------
 
# Use the following iTunes Connect credentials
USERNAME='email'
PASSWORD='password'
VENDOR_ID='8XXXXXXX'
 
fetchReport(){
	# Check that we're not being passed a bogus days_ago value
	if [ $1 -gt 0 -a $1 -lt 16 ]
	then
		export SECONDS=`expr $1 \\* 86400`
		local TIMESTAMP=$(perl -e 'use POSIX;print strftime "%Y%m%d",localtime time-$ENV{"SECONDS"};')
 
		local TSYEAR=${TIMESTAMP:0:4}
		local TSMONTH=${TIMESTAMP:4:2}
		local TSDAY=${TIMESTAMP:6:2}
 
		# Query auto-ingest
		cd $(dirname $0)
		$(java Autoingestion $USERNAME $PASSWORD $VENDOR_ID Sales Daily Summary $TIMESTAMP > /dev/null)
 
		COMPRESSED_FILENAME=S_D_$VENDOR_ID\_$TIMESTAMP.txt.gz
		UNCOMPRESSED_FILENAME=S_D_$VENDOR_ID\_$TIMESTAMP.txt
 
		# Uncompress and archive the report in question
		if [ -f $COMPRESSED_FILENAME ]
		then
			gzip --uncompress $COMPRESSED_FILENAME -f
			mkdir -p $TSYEAR/$TSMONTH
			mv $UNCOMPRESSED_FILENAME $TSYEAR/$TSMONTH/$TSDAY.txt
		fi
	else
		echo "Invalid days_ago value specified. Must be within valid range (1 - 15)"
		exit 1
	fi
}
 
# Fetch our report
fetchReport $1