-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync-day.php
48 lines (39 loc) · 1.25 KB
/
sync-day.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
<?php
/**
* Syncs nutrition and weight for a single day.
*/
require_once 'class-secrets.php';
require_once 'class-logger.php';
require_once 'class-health-check.php';
require_once 'class-fitbit.php';
require_once 'class-fatsecret.php';
require_once 'functions.php';
$script_args = getopt( '', [ 'date::', 'date_tz::' ] );
$date_tz = new DateTimeZone( $script_args['date_tz'] ?? 'America/New_York' );
$date = new DateTimeImmutable( $script_args['date'] ?? 'today', $date_tz );
$logger = new Logger( $date_tz, get_flag_set( 'debug' ) );
$check = new HealthCheck();
$logger->log( 'Syncing weight and food diary for ' . $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();
$weight = sync_weight_for_date( $fitbit, $fatsecret, $date, $logger );
if ( $weight ) {
$logger->log( 'Synced weight.' );
}
$food = sync_food_for_date( $fitbit, $fatsecret, $date, $logger );
if ( $food ) {
$logger->log( 'Synced food.' );
}
$check->finish();
$logger->log( 'Complete!' );
} catch ( Exception $err ) {
$check->fail();
$logger->error( $err );
}