/
example_update.php
78 lines (63 loc) · 2.25 KB
/
example_update.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
<?php
declare(strict_types=1);
use picasticks\Strava\Club;
use picasticks\Strava\ClubException;
//use Strava\API\Client;
//use Strava\API\Service\REST;
use picasticks\Strava\Client;
use picasticks\Strava\REST;
use Strava\API\OAuth;
require_once '../lib/vendor/autoload.php';
// Define list of Strava Club IDs to track
$clubs = array(
123456,
123456,
123456,
123456,
123456,
);
// Set a TZ for date calculations
date_default_timezone_set('America/New_York');
// Set start and end date for tracking
$startDate = '2021-01-01';
$endDate = '2021-01-03';
// Replace with your Strava API credentials and the URI of this script
$oauth = new OAuth([
'clientId' => 12345,
'clientSecret' => 'your-client-secret',
'redirectUri' => 'https://your.example.org/example_update.php'
]);
if (!isset($_GET['code'])) {
echo '<p><a href="'.$oauth->getAuthorizationUrl([
'scope' => [
'read',
]
]).'">Connect to Strava API and and download updates</a><p>';
} else {
$token = $oauth->getAccessToken('authorization_code', ['code' => $_GET['code']]);
$adapter = new \GuzzleHttp\Client(['base_uri' => 'https://www.strava.com/api/v3/']);
$service = new REST($token->getToken(), $adapter);
$club = new Club(dirname(__DIR__).'/json');
$club->setClient(new Client($service));
// Uncomment to override library's default Strava API request limit (default is 100)
//$club->requestLimit = 42;
// Uncomment to set logger to null to skip logging
//$club->logger = null;
// Compute start/end timestamps from start/end dates. Set end date to no later than yesterday.
$start = strtotime($startDate);
$end = min(strtotime($endDate), (strtotime(date('Y-m-d')) - 86400));
$club->log('Updating using date range '.date('Y-m-d', $start).' to '.date('Y-m-d', $end));
// Download data from Strava. Only downloads when local files aren't already present.
try {
foreach ($clubs as $clubId) {
// Get club info
$club->downloadClub($clubId);
// Get club activities for each day between $start and $end
$club->downloadClubActivities($clubId, $start, $end);
}
$club->log(sprintf("Done! Made %s API requests to Strava", $club->getRequestCount()));
} catch (ClubException $e) {
$club->log('Configured API limit reached: '.$e->getMessage());
}
}
# vim: tabstop=4:shiftwidth=4:noexpandtab