From dd7ba23d9a01116e66286b10fc078a98d2aa18b3 Mon Sep 17 00:00:00 2001 From: Petru Paler Date: Mon, 30 May 2016 21:49:27 +0200 Subject: [PATCH 1/8] Save Fitbit refresh token when it changes. --- fitsync.py | 59 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/fitsync.py b/fitsync.py index 1754de9..0555c66 100755 --- a/fitsync.py +++ b/fitsync.py @@ -21,13 +21,6 @@ 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 - def GetGoogleClient(filename): logging.debug("Creating Google client") @@ -62,6 +55,40 @@ 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: + print credentials + print client.client.token + if client.client.token['access_token'] != credentials['access_token'] or \ + client.client.token['refresh_token'] != credentials['refresh_token']: + logging.debug("Updating Fitbit credentials") + credentials.update(client.client.token) + yaml.dump(credentials, open(filename, 'w')) + + return googleWeightLogs, minLogNs, maxLogNs, scale def GetDataSourceId(dataSource): projectNumber = Storage('google.json').get().client_id.split('-')[0] @@ -86,23 +113,7 @@ def main(): logging.basicConfig(level=max(debugLevel, 0)) logging.root.name = "fitsync" - fitbitClient = GetFitbitClient(args.fitbit_creds) - - 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') - 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] + googleWeightLogs, minLogNs, maxLogNs, scale = GetBodyweight(args.fitbit_creds) googleClient = GetGoogleClient(args.google_creds) From bdd47d0369ff73a7b3fa8d0fa97482aac4e9cbdc Mon Sep 17 00:00:00 2001 From: Petru Paler Date: Tue, 31 May 2016 08:10:38 +0200 Subject: [PATCH 2/8] Remove some debuggings prints. --- fitsync.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/fitsync.py b/fitsync.py index 0555c66..dd74beb 100755 --- a/fitsync.py +++ b/fitsync.py @@ -80,8 +80,6 @@ def GetBodyweight(filename): for log in fitbitWeightLogs] logging.debug("Got Fitbit data") finally: - print credentials - print client.client.token if client.client.token['access_token'] != credentials['access_token'] or \ client.client.token['refresh_token'] != credentials['refresh_token']: logging.debug("Updating Fitbit credentials") From 8c3589545784e847cb5145b978b0dd2b2b06d1da Mon Sep 17 00:00:00 2001 From: Petru Paler Date: Tue, 31 May 2016 08:10:49 +0200 Subject: [PATCH 3/8] Refactor fitbit credential update code. --- fitsync.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/fitsync.py b/fitsync.py index dd74beb..40db0dd 100755 --- a/fitsync.py +++ b/fitsync.py @@ -80,10 +80,13 @@ def GetBodyweight(filename): for log in fitbitWeightLogs] logging.debug("Got Fitbit data") finally: - if client.client.token['access_token'] != credentials['access_token'] or \ - client.client.token['refresh_token'] != credentials['refresh_token']: + 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") - credentials.update(client.client.token) yaml.dump(credentials, open(filename, 'w')) return googleWeightLogs, minLogNs, maxLogNs, scale From 6704e423885c71429058e671380d3ef434cb6e7f Mon Sep 17 00:00:00 2001 From: Petru Paler Date: Tue, 31 May 2016 08:22:24 +0200 Subject: [PATCH 4/8] Refactor credential update flow to further minimize changes. --- fitsync.py | 76 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 36 deletions(-) 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) From 70b68111fe6f03274104a92ff40645a532bcc1d8 Mon Sep 17 00:00:00 2001 From: Petru Paler Date: Mon, 30 May 2016 21:49:27 +0200 Subject: [PATCH 5/8] Save Fitbit refresh token when it changes. --- fitsync.py | 59 ++++++++++++++++++++++++++++++++---------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/fitsync.py b/fitsync.py index 1754de9..0555c66 100755 --- a/fitsync.py +++ b/fitsync.py @@ -21,13 +21,6 @@ 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 - def GetGoogleClient(filename): logging.debug("Creating Google client") @@ -62,6 +55,40 @@ 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: + print credentials + print client.client.token + if client.client.token['access_token'] != credentials['access_token'] or \ + client.client.token['refresh_token'] != credentials['refresh_token']: + logging.debug("Updating Fitbit credentials") + credentials.update(client.client.token) + yaml.dump(credentials, open(filename, 'w')) + + return googleWeightLogs, minLogNs, maxLogNs, scale def GetDataSourceId(dataSource): projectNumber = Storage('google.json').get().client_id.split('-')[0] @@ -86,23 +113,7 @@ def main(): logging.basicConfig(level=max(debugLevel, 0)) logging.root.name = "fitsync" - fitbitClient = GetFitbitClient(args.fitbit_creds) - - 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') - 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] + googleWeightLogs, minLogNs, maxLogNs, scale = GetBodyweight(args.fitbit_creds) googleClient = GetGoogleClient(args.google_creds) From 1723795899bf1366000e105d95d210b3aef93ccd Mon Sep 17 00:00:00 2001 From: Petru Paler Date: Tue, 31 May 2016 08:10:38 +0200 Subject: [PATCH 6/8] Remove some debuggings prints. --- fitsync.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/fitsync.py b/fitsync.py index 0555c66..dd74beb 100755 --- a/fitsync.py +++ b/fitsync.py @@ -80,8 +80,6 @@ def GetBodyweight(filename): for log in fitbitWeightLogs] logging.debug("Got Fitbit data") finally: - print credentials - print client.client.token if client.client.token['access_token'] != credentials['access_token'] or \ client.client.token['refresh_token'] != credentials['refresh_token']: logging.debug("Updating Fitbit credentials") From ebf339b9676d9ecc08d8342686a3fc29b8adbb8d Mon Sep 17 00:00:00 2001 From: Petru Paler Date: Tue, 31 May 2016 08:10:49 +0200 Subject: [PATCH 7/8] Refactor fitbit credential update code. --- fitsync.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/fitsync.py b/fitsync.py index dd74beb..40db0dd 100755 --- a/fitsync.py +++ b/fitsync.py @@ -80,10 +80,13 @@ def GetBodyweight(filename): for log in fitbitWeightLogs] logging.debug("Got Fitbit data") finally: - if client.client.token['access_token'] != credentials['access_token'] or \ - client.client.token['refresh_token'] != credentials['refresh_token']: + 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") - credentials.update(client.client.token) yaml.dump(credentials, open(filename, 'w')) return googleWeightLogs, minLogNs, maxLogNs, scale From b25899ae84d1ad9684bd408557d2127fd25fc8e4 Mon Sep 17 00:00:00 2001 From: Petru Paler Date: Tue, 31 May 2016 08:22:24 +0200 Subject: [PATCH 8/8] Refactor credential update flow to further minimize changes. --- fitsync.py | 76 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 40 insertions(+), 36 deletions(-) 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)