Skip to content

Instantly share code, notes, and snippets.

@etaubman
Created July 27, 2013 00:59
Show Gist options
  • Save etaubman/6093202 to your computer and use it in GitHub Desktop.
Save etaubman/6093202 to your computer and use it in GitHub Desktop.
Pull data from my last Gist into a single CSV. This doesn't handle station data yet.
<?php
/**
* Utility file for turning my JSON log files into usable data
* parse_log_files.php
*/
$start_time = time();
date_default_timezone_set("America/New_York");
function write_file ($contents, $name)
{
if(! $handle = fopen($name,"a+")) {
die();
}
else {
if (! fwrite($handle, $contents)) {
die();
}
fclose($handle);
}
}
function parse_file ($file)
{
$contents = file_get_contents($file);
$data = json_decode($contents);
return $data->stationBeanList;
}
$csv = "time,station_id,available_bikes,available_docks,total_docks,status_code\n";
$path_to_logs = "data/";
$file_count = 0;
foreach (glob($path_to_logs."*.log") as $log_file) {
$time = preg_match('/\d+/', $log_file, $match);
$file_count++;
foreach(parse_file($log_file) as $station) {
$csv .= $match[0].",";
$csv .= $station->id.",";
$csv .= $station->availableBikes.",";
$csv .= $station->availableDocks.",";
$csv .= $station->totalDocks.",";
$csv .= $station->statusKey."\n";
}
}
write_file($csv,"/home/ec2-user/citibike/parsed_data/data_".date('m-d-y_g:ia').".csv");
$end_time = time();
echo "Files parsed: ", $file_count,"\n";
echo "Time to parse data: ",$end_time - $start_time," seconds\n";
//End of File
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment