Skip to content

Instantly share code, notes, and snippets.

@ianbarber
Created August 10, 2012 10:57
Show Gist options
  • Save ianbarber/3313381 to your computer and use it in GitHub Desktop.
Save ianbarber/3313381 to your computer and use it in GitHub Desktop.
<?php
function write_moments_from_file($file, $client) {
$plus = new Google_PlusService($client);
$history = new Google_PlusMomentsService($client);
$log = array();
$fh = fopen($file, 'r');
while(($record = fgetcsv($fh)) && $record !== false) {
// Skip the header row
if($record[0] === "Date") {
continue;
}
// Create the moment and target
$moment = new Google_Moment();
$moment->setType("http://schemas.google.com/CheckInActivity");
$target = new Google_ItemScope();
// Extract fields from the record
list($date, $start, $end, $journey, $rest) = $record;
// Extract dates
$startdt = strtotime($date . " " . $start);
$enddt = strtotime($date . " " . $end);
// If we cross over midnight
if($enddt < $startdt) {
$enddt = strtotime("+1 day", $enddt);
}
// If it looks like a Bus journey
if($end == '' && substr($journey, 0, 3) == "Bus" ) {
$route = array();
if( preg_match("/route ([\dA-Z]+)/", $journey, $route) ) {
$target->setUrl("http://en.wikipedia.org/wiki/London_Buses_route_" . strtoupper($route[1]));
$log[] = sprintf("Took bus %s", $route[1]);
} else {
$log[] = "Couldn't parse route from " . $journey;
continue;
}
$moment->setStartDate(date("c", $startdt));
}
// If it looks like a train journey.
else {
if( preg_match("/ to ([^\(\[]+)[^\[]*(?:\[([^\]]*)\])?/", $journey, $route) ) {
$type = 'tube';
if($route[2] == 'National Rail') {
$type = 'railway';
}
$destination = str_replace(" ", "_", trim($route[1]));
$target->setUrl(sprintf("http://en.wikipedia.org/wiki/%s_%s_station", $destination, $type));
$log[] = sprintf("Travelled from %s in %d.0 minutes", $journey, (($enddt - $startdt)/60));
} else {
$log[] = "Couldn't parse route from " . $journey;
continue;
}
$moment->setStartDate(date("c", $enddt));
}
$moment->setTarget($target);
try {
$history->moments->insert('me', 'vault', $moment);
$log[] = "Inserted moment";
} catch (Google_ServiceException $e) {
$log[] = "Inserting moment failed: " . $e->getMessage();
}
}
fclose($fh);
return $log;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment