Permalink
Browse files

First commit

  • Loading branch information...
0 parents commit ed481015adf5d808e4e612fbb29c3f1a2017a765 @kmonaghan committed Sep 18, 2011
Showing with 464 additions and 0 deletions.
  1. 0 README
  2. +105 −0 public/ajax/daily.php
  3. +70 −0 public/ajax/region.php
  4. +14 −0 public/boot.php
  5. +102 −0 public/ingest.php
  6. +146 −0 public/stats.html
  7. +27 −0 schema.sql
0 README
No changes.
@@ -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)));
@@ -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));
@@ -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);
@@ -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;
+}
Oops, something went wrong.

0 comments on commit ed48101

Please sign in to comment.