forked from 10REM/php-garmin-connect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.php
72 lines (65 loc) · 2.39 KB
/
sync.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
<?php
set_time_limit(0);
require "vendor/autoload.php";
use Symfony\Component\Dotenv\Dotenv;
(new Dotenv())->loadEnv(__DIR__.'/.env');
$file = 'data/sync.json';
$directory = 'data/sync';
$lastDate = null;
$nbExported = 0;
try {
$client = new \dawguk\GarminConnect([
'username' => $_ENV['GARMIN_USERNAME'],
'password' => $_ENV['GARMIN_PASSWORD'],
]);
} catch (Exception $exception) {
#dd($exception);
}
try {
$data = \json_decode(file_get_contents($file), true);
}
catch (Exception $e) {
$data = [];
}
if (empty($data) || (array_key_exists('lastDate', $data) && ($lastDate = new \DateTime($data['lastDate'])) < new \DateTime())) {
foreach (range(null !== $lastDate ? $lastDate->format('Y') : $_ENV['GARMIN_SINCE'], $currentYear = (int)date('Y')) as $index => $year) {
if (!file_exists($currentDirectory = ($directory . DIRECTORY_SEPARATOR . $year))) {
mkdir($currentDirectory);
}
$response = null;
if (null !== $client) {
try {
$response = $client->getActivityList(0, 1000, null, [
'startDate' => null !== $lastDate ? $lastDate->format('Y-m-d') : sprintf('%d-01-01', $year),
'endDate' => ($currentYear === $year) ? date('Y-m-d') : sprintf('%d-12-31', $year),
'sortBy' => 'startLocal',
'sortOrder' => 'asc',
]);
} catch (Exception $exception) {
$response = null;
}
}
if (!empty($response)) {
foreach($response as $activity) {
$fit = null;
if (null !== $client) {
try {
$fit = $client->getDataFile(\dawguk\GarminConnect::DATA_TYPE_FIT, $activity->activityId);
} catch (Exception $exception) {
$fit = null;
}
file_put_contents($file, \json_encode([
'lastId' => $activity->activityId,
'lastDate' => $activity->startTimeLocal,
]));
}
if (!empty($fit)) {
file_put_contents($currentDirectory . DIRECTORY_SEPARATOR . $activity->activityId . '.zip', $fit);
$nbExported++;
}
}
}
}
}
echo sprintf("%d files exported", $nbExported);
die();