iTunes Connect sales report script in c#

We needed to pump (or pimp) our BI environment with some sales data from the downloads of a recently released iPhone App. In order to do that I found the follow python script which worked just fine: http://code.google.com/p/appdailysales/. Now since we have a Microsoft SSIS environment, I ported (part) of this code to C# so it can be run in a .NET environment. Note that you might have to change the bold parts to suit your needs.

Here’s the code:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;

namespace ConsoleApplication1
{
class Program
{
static int daysToDownload = 1;
static readonly String urlBase = “https://itts.apple.com”;
static String fieldNameReportType = “”;
static String fieldNameReportPeriod = “”;
static String fieldNameDayOrWeekSelection = “”;
static String fieldNameSubmitTypeName = “”;
static String fieldNameDayOrWeekDropdown = “”;

static void Main(string[] args)
{

Console.WriteLine(“fetching data”);

// First start up a session and get the sessionpage
String SessionPage = GetSessionPage();

// After this navigate to the vendor page
String VendorPage = GetVendorPage( SessionPage );

// After this get the ID for the dayOrWeekDropdwon
String UrlDownload = GetDayOrWeekUrl(VendorPage);

DownloadFiles( UrlDownload );

Console.WriteLine(“done fetching data, press enter to continue”);
Console.ReadLine();
}

public static String GetSessionPage()
{
WebClient client = new WebClient();
String data = client.DownloadString(urlBase + “/cgi-bin/WebObjects/Piano.woa”);

Match match = Regex.Match(data, “\” action=\”(.*)\”");

WebClient c2 = new WebClient();
NameValueCollection myQueryStringCollection = new NameValueCollection();
myQueryStringCollection.Add(“theAccountName”, “itunesaccountname“);
myQueryStringCollection.Add(“theAccountPW”, “itunespassword“);
myQueryStringCollection.Add(“1.Continue.x”, “0″);
myQueryStringCollection.Add(“1.Continue.y”, “0″);
c2.QueryString = myQueryStringCollection;

String downloadUrl = urlBase + match.Groups[1];
Console.WriteLine(“Logging in on”);
Console.WriteLine( downloadUrl );
return c2.DownloadString( downloadUrl );
}

public static String GetVendorPage( String SessionPage )
{
Match match = Regex.Match(SessionPage, “name=\”frmVendorPage\” action=\”(.*)\”");

String UrlDownload = urlBase + match.Groups[1];

MatchCollection matchCollection = Regex.Matches(SessionPage, “name=\”(.*?)\”");

fieldNameReportType = matchCollection[6].Groups[1].ToString();
fieldNameReportPeriod = matchCollection[7].Groups[1].ToString();
fieldNameDayOrWeekSelection = matchCollection[10].Groups[1].ToString();
fieldNameSubmitTypeName = matchCollection[11].Groups[1].ToString();

WebClient client = new WebClient();
NameValueCollection myQueryStringCollection = new NameValueCollection();
myQueryStringCollection.Add(fieldNameReportType, “Summary”);
myQueryStringCollection.Add(fieldNameReportPeriod, “Daily”);
myQueryStringCollection.Add(fieldNameDayOrWeekSelection, “Daily”);
myQueryStringCollection.Add(fieldNameSubmitTypeName, “ShowDropDown”);

client.QueryString = myQueryStringCollection;
Console.WriteLine(“fetching vendor page on”);
Console.WriteLine(UrlDownload);
return client.DownloadString(UrlDownload);
}

public static String GetDayOrWeekUrl(String VendorPage)
{
// Get the new session Url
Match match = Regex.Match(VendorPage, “name=\”frmVendorPage\” action=\”(.*)\”");
String UrlDownload = urlBase + match.Groups[1];

// Also fill in the DayOrWeek variable
MatchCollection matchCollection = Regex.Matches(VendorPage, “name=\”(.*?)\”");
fieldNameDayOrWeekDropdown = matchCollection[8].Groups[1].ToString();

Console.WriteLine(“fetching dayOrWeek variable: ” + fieldNameDayOrWeekDropdown);

return UrlDownload;
}

public static void DownloadFiles( String UrlDownload )
{
ArrayList reportDates = new ArrayList();
for (int i = 1; i <= daysToDownload; i++)
{
TimeSpan timespan = TimeSpan.FromDays(i);
DateTime reportDate = DateTime.Today.Subtract(timespan);
reportDates.Add(reportDate);
}

foreach (DateTime date in reportDates)
{
WebClient client = new WebClient();
NameValueCollection myQueryStringCollection = new NameValueCollection();
myQueryStringCollection.Add(fieldNameReportType, “Summary”);
myQueryStringCollection.Add(fieldNameReportPeriod, “Daily”);
myQueryStringCollection.Add(fieldNameDayOrWeekDropdown, appleDate(date));
myQueryStringCollection.Add(“download”, “Download”);
myQueryStringCollection.Add(fieldNameDayOrWeekSelection, appleDate(date));
myQueryStringCollection.Add(fieldNameSubmitTypeName, “Download”);
client.QueryString = myQueryStringCollection;

String filename = “file.” + date.ToString(“dd_MM_yyyy”).Replace(“/”, “_”) + “.txt.gz”;
Console.WriteLine(“fetching file: ” + filename);
client.DownloadFile(UrlDownload, “c:/” + filename );
Console.WriteLine(“fetched file: ” + filename + ” – apple filename: ” + client.ResponseHeaders.Get(“content-disposition”) );
}
}

public static String appleDate( DateTime date )
{
return System.Web.HttpUtility.UrlEncode( date.ToString(“MM/dd/yyyy”) );
}
}
}

Comments are closed.