-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogleCall.php
108 lines (86 loc) · 2.99 KB
/
googleCall.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?PHP
include_once('common.php');
function googleCall($params)
{
$url = 'https://www.googleapis.com/latitude/v1/location?oauth_token='.$_COOKIE[GOOGLE_TOKEN]."&granularity=best&$params";
$xmlresponse = gzipCall($url);
$count = 0;
$locks = false;
if ($xmlresponse !== false)
{
$locks = json_decode($xmlresponse);
if (is_object($locks) && property_exists($locks->data, 'items'))
{
$count = count($locks->data->items);
}
}
return array($locks, $count);
}
function testLatitude()
{
if (cookiesSet(array(GOOGLE_TOKEN)))
{
list ( $rsp, $count ) = googleCall('max-results=1');
return $count == 1;
}
return false;
}
function googleAuthLink($class)
{
return '<a class="'.$class.'" href="https://accounts.google.com/o/oauth2/auth?client_id=60961182481.apps.googleusercontent.com&redirect_uri='.baseHttpPath().'gotLatitude.php&scope=https://www.googleapis.com/auth/latitude.all.best&response_type=code">Authorise Google Latitude</a>';
}
function getLatPoints($statFile, $maxGap, $photos)
{
writeStat("Confirming still connected to Google Latitude.", $statFile);
if (!testLatitude())
errorExit('Please re-'.googleAuthLink('').'.');
$first = $photos[0][UTIME] + $maxGap;
$lastPhoto = end($photos);
$last = $lastPhoto[UTIME] - $maxGap;
$msg = 'Getting Google Latitude data from '.strong(formatDate($last)).' to '.strong(formatDate($first));
writeProgress($msg, 0, $statFile);
$realLast = $last * 1000;
$diff = $first * 1000 - $realLast;
$allPoints = array();
for ($i = 0; $i < count($photos); $i++)
{
// get the initial date, +- 24 hours
$first = ($photos[$i][UTIME] + $maxGap) * 1000;
// while the gap between photos is less than 2 * maxGap
while (($i < count($photos) - 1) && ($photos[$i][UTIME] - $photos[$i + 1][UTIME] < $maxGap * 2))
$i ++;
// get the last date before the gap
$last = ($photos[$i][UTIME] - $maxGap) * 1000;
$points = getWholeDuration($first, $last, $statFile, $realLast, $diff, $msg);
$allPoints = array_merge($allPoints, $points);
}
writeStat("Got all Google Latitude data.", $statFile);
return $allPoints;
}
function getWholeDuration($first, $last, $statFile, $realLast, $diff, $msg)
{
$rsp = array();
// loop through pages of google latitude points
while (true)
{
list($locks, $count) = googleCall("max-results=1000&max-time=$first&min-time=$last&fields=items(timestampMs,latitude,longitude,accuracy)");
if (($count > 0) && ($locks !== false))
{
// max-time for next page of latitude data
$first = end($locks->data->items)->timestampMs - 1;
$p = round(100 * ($realLast - $first + $diff) / $diff);
writeProgress($msg, $p, $statFile);
// go through latitude points
foreach ($locks->data->items as $item)
{
if (isset($item->accuracy) && $item->accuracy < DEF_ACCURACY)
$rsp[] = array(($item->timestampMs / 1000), $item->longitude, $item->latitude);
}
}
else
{
return $rsp;
}
}
}
?>