diff --git a/fitsync.py b/fitsync.py index 40db0dd..5f32242 100755 --- a/fitsync.py +++ b/fitsync.py @@ -21,6 +21,24 @@ POUNDS_PER_KILOGRAM = 2.20462 TIME_FORMAT = "%a, %d %b %Y %H:%M:%S" +def GetFitbitClient(filename): + logging.debug("Creating Fitbit client") + credentials = yaml.load(open(filename)) + client = fitbit.Fitbit(**credentials) + logging.debug("Fitbit client created") + return client, credentials + + +def UpdateFitbitCredentials(client, filename, credentials): + dump = False + for t in ('access_token', 'refresh_token'): + if client.client.token[t] != credentials[t]: + credentials[t] = client.client.token[t] + dump = True + if dump: + logging.debug("Updating Fitbit credentials") + yaml.dump(credentials, open(filename, 'w')) + def GetGoogleClient(filename): logging.debug("Creating Google client") @@ -55,41 +73,6 @@ def FitbitWeightToGoogleWeight(fitbitWeightLog, tzinfo): value=[dict(fpVal=logWeightKg)], ) -def GetBodyweight(filename): - logging.debug("Creating Fitbit client") - credentials = yaml.load(open(filename)) - client = fitbit.Fitbit(**credentials) - logging.debug("Fitbit client created") - - try: - logging.debug("Getting Fitbit data") - userProfile = client.user_profile_get() - tzinfo = dateutil.tz.gettz(userProfile['user']['timezone']) - - devices = client.get_devices() - (scale,) = (device for device in devices if device['type'] == 'SCALE') - - fitbitBodyweight = client.get_bodyweight(period='1m') - fitbitWeightLogs = fitbitBodyweight['weight'] - fitbitWeightLogTimes = [epochOfFitbitLog(log, tzinfo) for log in fitbitWeightLogs] - - minLogNs = nano(min(fitbitWeightLogTimes)) - maxLogNs = nano(max(fitbitWeightLogTimes)) - - googleWeightLogs = [FitbitWeightToGoogleWeight(log, tzinfo) - for log in fitbitWeightLogs] - logging.debug("Got Fitbit data") - finally: - dump = False - for t in ('access_token', 'refresh_token'): - if client.client.token[t] != credentials[t]: - credentials[t] = client.client.token[t] - dump = True - if dump: - logging.debug("Updating Fitbit credentials") - yaml.dump(credentials, open(filename, 'w')) - - return googleWeightLogs, minLogNs, maxLogNs, scale def GetDataSourceId(dataSource): projectNumber = Storage('google.json').get().client_id.split('-')[0] @@ -114,7 +97,28 @@ def main(): logging.basicConfig(level=max(debugLevel, 0)) logging.root.name = "fitsync" - googleWeightLogs, minLogNs, maxLogNs, scale = GetBodyweight(args.fitbit_creds) + fitbitClient, fitbitCreds = GetFitbitClient(args.fitbit_creds) + try: + logging.debug("Getting Fitbit data") + userProfile = fitbitClient.user_profile_get() + tzinfo = dateutil.tz.gettz(userProfile['user']['timezone']) + + devices = fitbitClient.get_devices() + (scale,) = (device for device in devices if device['type'] == 'SCALE') + + fitbitBodyweight = fitbitClient.get_bodyweight(period='1m') + logging.debug("Got Fitbit data") + finally: + UpdateFitbitCredentials(fitbitClient, args.fitbit_creds, fitbitCreds) + + fitbitWeightLogs = fitbitBodyweight['weight'] + fitbitWeightLogTimes = [epochOfFitbitLog(log, tzinfo) for log in fitbitWeightLogs] + + minLogNs = nano(min(fitbitWeightLogTimes)) + maxLogNs = nano(max(fitbitWeightLogTimes)) + + googleWeightLogs = [FitbitWeightToGoogleWeight(log, tzinfo) + for log in fitbitWeightLogs] googleClient = GetGoogleClient(args.google_creds)