Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

First commit

  • Loading branch information...
commit ed481015adf5d808e4e612fbb29c3f1a2017a765 0 parents
@kmonaghan authored
0  README
No changes.
105 public/ajax/daily.php
@@ -0,0 +1,105 @@
+<?php
+include '../boot.php';
+
+$sql = 'SELECT DISTINCT apple_identifier, title FROM daily_raw ORDER BY title';
+
+foreach ($dbh->query($sql) as $row) {
+ $apps[$row['apple_identifier']] = $row['title'];
+}
+
+$params = array();
+$data = array();
+
+$productType = (isset($_GET['product_type_identifier']) && $_GET['product_type_identifier']) ? (int)$_GET['product_type_identifier'] : 1;
+
+$params[] = $productType;
+
+$downloadType = ($productType == 1) ? 'downloads' : 'updates';
+
+if (isset($_GET['from']))
+{
+ $parts = explode('/', $_GET['from']);
+ $startTime = strtotime($parts[2] . '-' . $parts[1] . '-' . $parts[0]);
+}
+else
+{
+ $startTime = strtotime('32 days ago');
+}
+
+$startDate = date('Y-m-d', $startTime);
+
+if (isset($_GET['to']))
+{
+ $parts = explode('/', $_GET['to']);
+ $endTime = strtotime($parts[2] . '-' . $parts[1] . '-' . $parts[0]);
+}
+else
+{
+ $endTime = strtotime('yesterday');
+}
+
+$endDate = date('Y-m-d', $endTime);
+
+$title = "App $downloadType between $startDate and $endDate";
+
+$params[] = $startDate;
+$params[] = $endDate;
+
+$query = '';
+$where = ' product_type_identifier = ? AND (begin_date >= ? AND begin_date <= ?) ';
+
+if (isset($_GET['apple_identifier']) && isset($apps[$_GET['apple_identifier']]))
+{
+ $where .= ' AND apple_identifier = ?';
+
+ $params[] = $_GET['apple_identifier'];
+
+ $apps = array($_GET['apple_identifier'] => $apps[$_GET['apple_identifier']]);
+}
+
+//We need an entry for each app for each day or the graphs will look patchy
+$currentTime = $startTime;
+while($currentTime <= $endTime)
+{
+ $currentDate = date('Y-m-d', $currentTime);
+
+ foreach($apps as $appleIdentifier => $value)
+ {
+ $data[$currentDate][$appleIdentifier] = array('units' => 0);
+ }
+
+ $currentTime = strtotime('+1 day', $currentTime);
+}
+$where = ($where) ? ' WHERE ' . $where : '';
+
+$group = ' GROUP BY apple_identifier, begin_date';
+
+$order = ' ORDER BY begin_date';
+
+
+$query = "SELECT sum(units) as units, apple_identifier, begin_date FROM daily_raw " . $where . $group . $order;
+
+$sth_total = $dbh->prepare($query);
+
+if (count($params))
+{
+ $count = 1;
+ foreach ($params as $value)
+ {
+ $sth_total->bindValue($count, $value);
+ $count++;
+ }
+}
+
+$sth_total->execute();
+
+while ($row = $sth_total->fetch(PDO::FETCH_ASSOC))
+{
+ $data[$row['begin_date']][$row['apple_identifier']] = array('units' => $row['units']);
+}
+
+header('Cache-Control: no-cache, must-revalidate');
+header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
+header('Content-type: application/json');
+
+echo json_encode(array('apps' => $apps, 'data' => $data, 'total' => count($data), 'title' => $title, 'start_date' => date('d/m/Y',$startTime), 'end_date' => date('d/m/Y',$endTime)));
70 public/ajax/region.php
@@ -0,0 +1,70 @@
+<?php
+include '../boot.php';
+
+$sql = 'SELECT DISTINCT daily_summary.apple_identifier, title FROM daily_summary INNER JOIN daily_raw ON (daily_summary.apple_identifier = daily_raw.apple_identifier) ORDER BY title';
+
+foreach ($dbh->query($sql) as $row) {
+ $apps[$row['apple_identifier']] = $row['title'];
+}
+
+$params = array();
+$data = array();
+
+//By default, we're only going to look back 2 weeks, not including today
+$startTime = strtotime('15 days ago');
+$startDate = date('Y-m-d', $startTime);
+$endTime = strtotime('yesterday');
+$endDate = date('Y-m-d', $endTime);
+
+$params[] = $startDate;
+$params[] = $endDate;
+
+$query = '';
+$where = ' (begin_date >= ? AND begin_date < ?) ';
+
+if (isset($_GET['apple_identifier']))
+{
+ $where .= ' AND apple_identifier = ?';
+
+ $params[] = $_GET['apple_identifier'];
+}
+
+//By default we will pull out the new downloads
+$where .= ' AND product_type_identifier = ?';
+
+$params[] = (isset($_GET['download_type'])) ? $_GET['download_type'] : 1;
+
+
+$where = ($where) ? ' WHERE ' . $where : '';
+
+$group = ' GROUP BY country_code';
+
+$query = "SELECT sum(units) as units, country_code FROM daily_raw " . $where . $group;
+
+//echo $query;
+//print_r($params);
+
+$sth_total = $dbh->prepare($query);
+
+if (count($params))
+{
+ $count = 1;
+ foreach ($params as $value)
+ {
+ $sth_total->bindValue($count, $value);
+ $count++;
+ }
+}
+
+$sth_total->execute();
+
+while ($row = $sth_total->fetch(PDO::FETCH_ASSOC))
+{
+ $data[] = $row;
+}
+
+header('Cache-Control: no-cache, must-revalidate');
+header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
+header('Content-type: application/json');
+
+echo json_encode(array('apps' => $apps, 'data' => $data));
14 public/boot.php
@@ -0,0 +1,14 @@
+<?php
+define('HOST', 'YOUR-HOST');
+define('DATABASE', 'itunes_connect_stats');
+define('DB_USER', 'YOUR-DBUSER');
+define('DB_PASSWORD', 'YOUR-DBPASS');
+
+$accounts = array(array('username' => 'YOUR-USERNAME',
+ 'password' => 'YOUR-PASSWORD',
+ 'vndnumber' => 'YOUR-VND',
+ ),
+
+ );
+
+$dbh = new PDO("mysql:host=" . HOST . ";dbname=" . DATABASE, DB_USER, DB_PASSWORD);
102 public/ingest.php
@@ -0,0 +1,102 @@
+<?php
+include 'boot.php';
+
+$ch = curl_init();
+
+$row = $dbh->query("SELECT begin_date FROM daily_raw ORDER BY begin_date DESC LIMIT 1")->fetch();
+
+if ($row)
+{
+ $starttime = strtotime('+1 day', strtotime($row['begin_date']));
+}
+else
+{
+ $starttime = strtotime('-14 days');
+}
+
+$today = time();
+
+while ($starttime < $today)
+{
+ process($starttime);
+
+ $starttime = strtotime('+1 day', $starttime);
+}
+
+curl_close ($ch);
+
+function process($time)
+{
+ $date = date('Ymd', $time);
+
+ echo 'Processing ' . $date . PHP_EOL;
+
+ global $dbh, $ch, $accounts;
+
+ //open connection
+
+ $sth = $dbh->prepare ("INSERT INTO `daily_raw` (`provider`, `provider_country`, `sku`, `developer`, `title`, `version`, `product_type_identifier`, `units`, `developer_proceeds`, `begin_date`, `end_date`, `customer_currency`, `country_code`, `currency_proceeds`, `apple_identifier`, `customer_price`, `promo_code`, `parent_identifier`, `subscription`, `period`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
+
+ foreach($accounts as $account)
+ {
+ $fields_string = "USERNAME=" . urlencode($account['username']);
+ $fields_string .= "&PASSWORD=" . urlencode($account['password']);
+ $fields_string .= "&VNDNUMBER=" . $account['vndnumber'];
+
+ $fields_string .= "&TYPEOFREPORT=Sales";
+ $fields_string .= "&DATETYPE=Daily";
+ $fields_string .= "&REPORTTYPE=Summary";
+ $fields_string .= "&REPORTDATE=$date";
+
+ $fp = fopen("$date.gz", 'w');
+
+ //set the url, number of POST vars, POST data
+ curl_setopt($ch,CURLOPT_URL, 'https://reportingitc.apple.com/autoingestion.tft');
+ curl_setopt($ch,CURLOPT_POST, 7);
+ curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);
+
+ curl_setopt($ch, CURLOPT_FILE, $fp);
+
+ //execute post
+ $contents = curl_exec ($ch);
+
+ //close connection
+
+ fclose($fp);
+
+ if (filesize("$date.gz"))
+ {
+ exec("gunzip $date.gz");
+
+
+ if (($handle = fopen("$date", "r")) !== FALSE)
+ {
+ //throw away first line
+ fgetcsv($handle, 1000, ",");
+
+ while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE)
+ {
+ $count = 1;
+ foreach($data as $value)
+ {
+ if (($count == 10) || ($count == 11))
+ {
+ $parts = explode('/', $value);
+ $value = $parts[2] . '-' . $parts[0] . '-' . $parts[1];
+ }
+
+ $sth->bindValue($count, $value);
+ $count++;
+ }
+ $sth->execute();
+ echo '.';
+ }
+ fclose($handle);
+ }
+
+ unlink("$date");
+ }
+ }
+
+ echo 'Done' . PHP_EOL;
+}
146 public/stats.html
@@ -0,0 +1,146 @@
+<html>
+ <head>
+ <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.14/themes/base/jquery-ui.css"></link>
+ <script type="text/javascript" src="https://www.google.com/jsapi"></script>
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
+ <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js"></script>
+ <script type="text/javascript">
+ google.load("visualization", "1", {packages:["corechart", "geochart"]});
+
+ function buildOptions(items)
+ {
+ var options = '<option value="0">All</option>';
+
+ $.each(items.apps, function(i,item){
+ options += '<option value="' + i + '">' + item + '</option>';
+ });
+
+ $('#app-selection').append('<select id="app-select">' + options + '</select>');
+ $('#app-selection').append('<select id="download-type"><option value="0">All</option><option value="1" selected>Downloads</option><option value="7">Updates</option></select>');
+ }
+
+ function drawChart(items)
+ {
+ var data = new google.visualization.DataTable();
+ data.addColumn('string', 'Day');
+
+ var appPosition = new Array();
+
+ var count = 1;
+ $.each(items.apps, function(i,item){
+ data.addColumn('number', item);
+ appPosition[i] = count;
+ count++;
+ });
+
+ data.addRows(items.total);
+
+ count = 0;
+ $.each(items.data, function(i,dateitem){
+ data.setValue(count, 0, i);
+
+ $.each(dateitem, function(i,item){
+ data.setValue(count, appPosition[i], parseInt(item.units));
+ });
+
+ count++;
+ });
+
+ $('#from').val(items.start_date);
+ $('#to').val(items.end_date);
+
+ var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
+ chart.draw(data, {width: 980, height: 480, title: items.title});
+ }
+
+ function drawRegionsMap(items)
+ {
+ var data = new google.visualization.DataTable();
+ data.addRows(items.data.length);
+ data.addColumn('string', 'Country');
+ data.addColumn('number', 'Popularity');
+
+ var count = 0;
+ $.each(items.data, function(i,item){
+ data.setValue(i, 0, item.country_code);
+ data.setValue(i, 1, parseInt(item.units));
+ count++;
+ });
+
+ var options = {};
+
+ var container = document.getElementById('map_canvas');
+ var geochart = new google.visualization.GeoChart(container);
+ geochart.draw(data, {width: 600, height: 480});
+ };
+
+ function getAppGraph(data)
+ {
+ $.getJSON('ajax/daily.php', data, function(data) {
+ buildOptions(data);
+ drawChart(data);
+ });
+ }
+
+ function updateChart()
+ {
+ var params = {apple_identifier: $('#app-select').val(),
+ product_type_identifier:$('#download-type').val(),
+ from: $('#from').val(),
+ to: $('#to').val(),
+ };
+
+ $.getJSON('ajax/daily.php', params, function(data) {
+ drawChart(data);
+ });
+ }
+
+ $(document).ready(function() {
+ getAppGraph({});
+
+ /*
+ $.getJSON('ajax/region.php', function(data) {
+ drawRegionsMap(data);
+ });
+ */
+ $('#app-select, #download-type').live('change', function() {
+ updateChart();
+ });
+
+ $('#view').live('click', function() {
+ updateChart();
+ });
+ });
+
+ $(function() {
+ var dates = $( "#from, #to" ).datepicker({
+ defaultDate: "+1w",
+ maxDate: "-1D",
+ changeMonth: true,
+ numberOfMonths: 1,
+ dateFormat: 'dd/mm/yy',
+ onSelect: function( selectedDate ) {
+ var option = this.id == "from" ? "minDate" : "maxDate",
+ instance = $( this ).data( "datepicker" ),
+ date = $.datepicker.parseDate(
+ instance.settings.dateFormat ||
+ $.datepicker._defaults.dateFormat,
+ selectedDate, instance.settings );
+ dates.not( this ).datepicker( "option", option, date );
+ }
+ });
+ });
+ </script>
+ </head>
+
+ <body>
+ <div id="app-selection"></div>
+ <label for="from">From</label>
+ <input type="text" id="from" name="from"/>
+ <label for="to">to</label>
+ <input type="text" id="to" name="to"/>
+ <input type="button" id="view" value="View" />
+ <div id="chart_div"></div>
+ <div id='map_canvas'></div>
+ </body>
+</html>
27 schema.sql
@@ -0,0 +1,27 @@
+CREATE TABLE `daily_raw` (
+ `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+ `provider` char(5) DEFAULT 'APPLE',
+ `provider_country` char(2) DEFAULT 'US',
+ `sku` varchar(100) DEFAULT NULL,
+ `developer` varchar(4000) DEFAULT NULL,
+ `title` varchar(600) DEFAULT NULL,
+ `version` varchar(100) DEFAULT NULL,
+ `product_type_identifier` varchar(20) DEFAULT NULL,
+ `units` decimal(18,2) DEFAULT NULL,
+ `developer_proceeds` decimal(18,2) DEFAULT NULL,
+ `begin_date` date DEFAULT NULL,
+ `end_date` date DEFAULT NULL,
+ `customer_currency` char(3) DEFAULT NULL,
+ `country_code` char(2) DEFAULT NULL,
+ `currency_proceeds` char(3) DEFAULT NULL,
+ `apple_identifier` decimal(18,0) DEFAULT NULL,
+ `customer_price` decimal(18,2) DEFAULT NULL,
+ `promo_code` varchar(10) DEFAULT NULL,
+ `parent_identifier` varchar(100) DEFAULT NULL,
+ `subscription` varchar(10) DEFAULT NULL,
+ `period` varchar(30) DEFAULT NULL,
+ PRIMARY KEY (`id`),
+ KEY `apple_identifier` (`apple_identifier`),
+ KEY `begin_date` (`begin_date`),
+ KEY `product_type_identifier` (`product_type_identifier`)
+) ENGINE=MyISAM AUTO_INCREMENT=305 DEFAULT CHARSET=latin1;
Please sign in to comment.
Something went wrong with that request. Please try again.