-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-weight.php
52 lines (44 loc) · 1.65 KB
/
sync-weight.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
<?php
require_once 'class-logger.php';
require_once 'class-health-check.php';
require_once 'class-secrets.php';
require_once 'class-fitbit.php';
require_once 'class-fatsecret.php';
require_once 'functions.php';
$script_args = getopt( '', [ 'date_start::', 'date_end::', 'date_tz::' ] );
$date_tz = new DateTimeZone( $script_args['date_tz'] ?? 'America/New_York' );
$start_date = new DateTimeImmutable( $script_args['date_start'] ?? 'today', $date_tz );
$end_date = new DateTimeImmutable( $script_args['date_end'] ?? 'today', $date_tz );
$logger = new Logger( $date_tz, get_flag_set( 'debug' ) );
$check = new HealthCheck();
$logger->log( 'Syncing weight between ' . $start_date->format( 'Y-m-d' ) . ' and ' . $end_date->format( 'Y-m-d' ) . ':' );
try {
$secrets = new Secrets( __DIR__ . '/secrets.json' );
$fitbit = new Fitbit( $secrets );
$fatsecret = new FatSecret( $secrets );
if ( get_flag_set( 'check' ) ) {
$check->init( $secrets );
}
$check->start();
$secrets->save();
$updated = 0;
$current_date = DateTime::createFromImmutable( $start_date );
do {
$weights = $fitbit->get_weight_for_date_period( $current_date, '1m' );
$current_date->add( new DateInterval( 'P1M' ) );
foreach ( $weights as $date => $kg ) {
$current_day = new DateTimeImmutable( $date, $date_tz );
if ( $start_date > $current_day ) {
continue;
}
$fatsecret->update_weight_for_date( $current_day, $kg );
$updated++;
$logger->log( "Updated weight on {$date}." );
}
} while ( $current_date < $end_date );
$check->finish();
$logger->log( "Updated {$updated} days." );
} catch ( Exception $err ) {
$check->fail();
$logger->error( $err );
}