Skip to content

Commit

Permalink
* make filename unique per vnd and date * report there was an error e…
Browse files Browse the repository at this point in the history
…xecuting
  • Loading branch information
kmonaghan committed Jan 4, 2013
1 parent 9ffcf15 commit d874ecd
Showing 1 changed file with 34 additions and 19 deletions.
53 changes: 34 additions & 19 deletions public/ingest.php
Expand Up @@ -2,9 +2,7 @@
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']));
Expand Down Expand Up @@ -33,8 +31,6 @@ function process($time)

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)
Expand All @@ -47,29 +43,34 @@ function process($time)
$fields_string .= "&DATETYPE=Daily";
$fields_string .= "&REPORTTYPE=Summary";
$fields_string .= "&REPORTDATE=$date";

$fp = fopen("$date.gz", 'w');

$filename = "{$date}-{$account['vndnumber']}";

$fp = fopen("$filename.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


if ($contents === false)
{
echo 'Curl error: ' . curl_error($ch);
}

fclose($fp);

if (filesize("$date.gz"))
if (filesize("$filename.gz"))
{
exec("gunzip $date.gz");
exec("gunzip $filename.gz");


if (($handle = fopen("$date", "r")) !== FALSE)
if (($handle = fopen("$filename", "r")) !== FALSE)
{
//throw away first line
fgetcsv($handle, 1000, ",");
Expand All @@ -88,15 +89,29 @@ function process($time)
$sth->bindValue($count, $value);
$count++;
}
$sth->execute();
echo '.';

if ($sth->execute())
{
echo '.';
}
else
{
echo 'Error executing' . PHP_EOL;
}
}
fclose($handle);
}

unlink("$date");
}
else
{
echo "Could not open $filename for reading" . PHP_EOL;
}
unlink("$filename");
}
else
{
echo 'File is of size 0' . PHP_EOL;
}
}

echo 'Done' . PHP_EOL;
}
}

0 comments on commit d874ecd

Please sign in to comment.