-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
81 lines (65 loc) · 2.19 KB
/
index.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
<?php
namespace Causal\F2GC;
require_once('config.php');
require_once('AbstractClient.php');
require_once('GarminConnectClient.php');
require_once('FitbitClient.php');
echo <<<EOT
----------------------------------------------------------------
Fitbit to Garmin Connect Synchronization
----------------------------------------------------------------
EOT;
echo "Initializing connection to Garmin Connect ... ";
$gcClient = new GarminConnectClient(GARMIN_CONNECT_USERNAME, GARMIN_CONNECT_PASSWORD);
if ($gcClient->connect()) {
echo "success\n";
} else {
echo "fail\n";
exit(1);
}
echo "Fetching weight data points ... ";
$values = $gcClient->getWeightValues();
echo count($values) . " data points\n";
$weightTarget = [];
foreach ($values as $data) {
$date = date('Y-m-d', $data['date'] / 1000);
$weight = $data['weight'] / 1000;
if (!isset($weightTarget[$date])) {
$weightTarget[$date] = $weight;
}
}
$syncAll = count($weightTarget) < 10;
$minDate = date('Y-m-d', strtotime('-1 year'));
echo "Synchronization period ... " . ($syncAll ? "all" : "since $minDate") . "\n";
echo "Initializing connection to Fitbit ... ";
$fitbitClient = new FitbitClient(FITBIT_USERNAME, FITBIT_PASSWORD);
if ($fitbitClient->connect()) {
echo "success\n";
} else {
echo "fail\n";
exit(2);
}
echo "Fetching weight data points ... ";
$values = $fitbitClient->getWeightValues();
echo count($values) . " data points\n";
$weightSource = [];
foreach ($values as $data) {
list($date, ) = explode('T', $data['dateTime'], 2);
if (!$syncAll && $date < $minDate) continue;
if (!isset($weightSource[$date])) {
$weightSource[$date] = $data['weight'];
}
}
echo "Looking for new weight data points ... ";
$newDates = array_diff_key($weightSource, $weightTarget);
echo count($newDates) . " new data points\n";
foreach ($newDates as $date => $weight) {
$gcClient->addWeight($date, $weight);
}
// Remove local cookies
echo "Disconnecting from Fitbit ... ";
$fitbitClient->disconnect();
echo "success\n";
echo "Disconnecting from Garmin Connect ... ";
$gcClient->disconnect();
echo "success\n\n";