format whitespace
This commit is contained in:
+8
-8
@@ -5,13 +5,13 @@ from oauth2client.client import OAuth2WebServerFlow
|
|||||||
from oauth2client.tools import run_flow, argparser
|
from oauth2client.tools import run_flow, argparser
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
client_id = sys.argv[1]
|
client_id = sys.argv[1]
|
||||||
client_secret = sys.argv[2]
|
client_secret = sys.argv[2]
|
||||||
scope = sys.argv[3]
|
scope = sys.argv[3]
|
||||||
flow = OAuth2WebServerFlow(client_id, client_secret, scope)
|
flow = OAuth2WebServerFlow(client_id, client_secret, scope)
|
||||||
storage = Storage('google.json')
|
storage = Storage('google.json')
|
||||||
flags = argparser.parse_args([])
|
flags = argparser.parse_args([])
|
||||||
run_flow(flow, storage, flags)
|
run_flow(flow, storage, flags)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
main()
|
main()
|
||||||
|
|||||||
+94
-94
@@ -14,128 +14,128 @@ POUNDS_PER_KILOGRAM = 2.20462
|
|||||||
|
|
||||||
|
|
||||||
def GetFitbitClient():
|
def GetFitbitClient():
|
||||||
credentials = yaml.load(open('fitbit.yaml'))
|
credentials = yaml.load(open('fitbit.yaml'))
|
||||||
client = fitbit.Fitbit(**credentials)
|
client = fitbit.Fitbit(**credentials)
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
def GetGoogleClient():
|
def GetGoogleClient():
|
||||||
credentials = Storage('google.json').get()
|
credentials = Storage('google.json').get()
|
||||||
http = credentials.authorize(httplib2.Http())
|
http = credentials.authorize(httplib2.Http())
|
||||||
client = build('fitness', 'v1', http=http)
|
client = build('fitness', 'v1', http=http)
|
||||||
return client
|
return client
|
||||||
|
|
||||||
|
|
||||||
def nano(val):
|
def nano(val):
|
||||||
"""Converts a number to nano (str)."""
|
"""Converts a number to nano (str)."""
|
||||||
return '%d' % (val * 1e9)
|
return '%d' % (val * 1e9)
|
||||||
|
|
||||||
|
|
||||||
def FitbitWeightToGoogleWeight(fitbitWeightLog):
|
def FitbitWeightToGoogleWeight(fitbitWeightLog):
|
||||||
logSecs = fitbitWeightLog['logId'] / 1000
|
logSecs = fitbitWeightLog['logId'] / 1000
|
||||||
logWeightLbs = fitbitWeightLog['weight']
|
logWeightLbs = fitbitWeightLog['weight']
|
||||||
logWeightKg = logWeightLbs / POUNDS_PER_KILOGRAM
|
logWeightKg = logWeightLbs / POUNDS_PER_KILOGRAM
|
||||||
return dict(
|
return dict(
|
||||||
dataTypeName='com.google.weight',
|
dataTypeName='com.google.weight',
|
||||||
endTimeNanos=nano(logSecs),
|
endTimeNanos=nano(logSecs),
|
||||||
startTimeNanos=nano(logSecs),
|
startTimeNanos=nano(logSecs),
|
||||||
value=[dict(fpVal=logWeightKg)],
|
value=[dict(fpVal=logWeightKg)],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def GetDataSourceId(dataSource):
|
def GetDataSourceId(dataSource):
|
||||||
projectNumber = Storage('google.json').get().client_id.split('-')[0]
|
projectNumber = Storage('google.json').get().client_id.split('-')[0]
|
||||||
return ':'.join((
|
return ':'.join((
|
||||||
dataSource['type'],
|
dataSource['type'],
|
||||||
dataSource['dataType']['name'],
|
dataSource['dataType']['name'],
|
||||||
projectNumber,
|
projectNumber,
|
||||||
dataSource['device']['manufacturer'],
|
dataSource['device']['manufacturer'],
|
||||||
dataSource['device']['model'],
|
dataSource['device']['model'],
|
||||||
dataSource['device']['uid']))
|
dataSource['device']['uid']))
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
fitbitClient = GetFitbitClient()
|
fitbitClient = GetFitbitClient()
|
||||||
|
|
||||||
devices = fitbitClient.get_devices()
|
devices = fitbitClient.get_devices()
|
||||||
(scale,) = (device for device in devices if device['type'] == 'SCALE')
|
(scale,) = (device for device in devices if device['type'] == 'SCALE')
|
||||||
|
|
||||||
fitbitBodyweight = fitbitClient.get_bodyweight(period='30d')
|
fitbitBodyweight = fitbitClient.get_bodyweight(period='30d')
|
||||||
fitbitWeightLogs = fitbitBodyweight['weight']
|
fitbitWeightLogs = fitbitBodyweight['weight']
|
||||||
fitbitWeightLogTimes = [log['logId'] / 1000 for log in fitbitWeightLogs]
|
fitbitWeightLogTimes = [log['logId'] / 1000 for log in fitbitWeightLogs]
|
||||||
|
|
||||||
minLogNs = nano(min(fitbitWeightLogTimes))
|
minLogNs = nano(min(fitbitWeightLogTimes))
|
||||||
maxLogNs = nano(max(fitbitWeightLogTimes))
|
maxLogNs = nano(max(fitbitWeightLogTimes))
|
||||||
|
|
||||||
googleWeightLogs = [FitbitWeightToGoogleWeight(log)
|
googleWeightLogs = [FitbitWeightToGoogleWeight(log)
|
||||||
for log in fitbitWeightLogs]
|
for log in fitbitWeightLogs]
|
||||||
|
|
||||||
googleClient = GetGoogleClient()
|
googleClient = GetGoogleClient()
|
||||||
|
|
||||||
dataSource = dict(
|
dataSource = dict(
|
||||||
type='raw',
|
type='raw',
|
||||||
application=dict(name='fitsync'),
|
application=dict(name='fitsync'),
|
||||||
dataType=dict(
|
dataType=dict(
|
||||||
name='com.google.weight',
|
name='com.google.weight',
|
||||||
field=[dict(format='floatPoint', name='weight')]
|
field=[dict(format='floatPoint', name='weight')]
|
||||||
),
|
),
|
||||||
device=dict(
|
device=dict(
|
||||||
type='scale',
|
type='scale',
|
||||||
manufacturer='unknown',
|
manufacturer='unknown',
|
||||||
model='unknown',
|
model='unknown',
|
||||||
uid=scale['id'],
|
uid=scale['id'],
|
||||||
version=scale['deviceVersion'],
|
version=scale['deviceVersion'],
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
dataSourceId = GetDataSourceId(dataSource)
|
dataSourceId = GetDataSourceId(dataSource)
|
||||||
|
|
||||||
# Ensure datasource exists for the device.
|
# Ensure datasource exists for the device.
|
||||||
try:
|
try:
|
||||||
googleClient.users().dataSources().get(
|
googleClient.users().dataSources().get(
|
||||||
userId='me',
|
userId='me',
|
||||||
dataSourceId=dataSourceId)
|
dataSourceId=dataSourceId)
|
||||||
except HttpError, error:
|
except HttpError, error:
|
||||||
if not 'DataSourceId not found' in str(error):
|
if not 'DataSourceId not found' in str(error):
|
||||||
raise error
|
raise error
|
||||||
# Doesn't exist, so create it.
|
# Doesn't exist, so create it.
|
||||||
googleClient.users().dataSources().create(
|
googleClient.users().dataSources().create(
|
||||||
userId='me',
|
userId='me',
|
||||||
body=dataSource)
|
body=dataSource)
|
||||||
|
|
||||||
datasetId = '%s-%s' % (minLogNs, maxLogNs)
|
datasetId = '%s-%s' % (minLogNs, maxLogNs)
|
||||||
|
|
||||||
command = 'patch'
|
command = 'patch'
|
||||||
if len(sys.argv) > 1:
|
if len(sys.argv) > 1:
|
||||||
command = sys.argv[1]
|
command = sys.argv[1]
|
||||||
|
|
||||||
# Get weight dataset.
|
# Get weight dataset.
|
||||||
if command == 'get':
|
if command == 'get':
|
||||||
print googleClient.users().dataSources().datasets().get(
|
print googleClient.users().dataSources().datasets().get(
|
||||||
userId='me',
|
userId='me',
|
||||||
dataSourceId=dataSourceId,
|
dataSourceId=dataSourceId,
|
||||||
datasetId=datasetId).execute()
|
datasetId=datasetId).execute()
|
||||||
|
|
||||||
# Delete weight dataset.
|
# Delete weight dataset.
|
||||||
if command == 'delete':
|
if command == 'delete':
|
||||||
googleClient.users().dataSources().datasets().delete(
|
googleClient.users().dataSources().datasets().delete(
|
||||||
userId='me',
|
userId='me',
|
||||||
dataSourceId=dataSourceId,
|
dataSourceId=dataSourceId,
|
||||||
datasetId=datasetId).execute()
|
datasetId=datasetId).execute()
|
||||||
print "deleted data"
|
print "deleted data"
|
||||||
|
|
||||||
# Upload weight dataset.
|
# Upload weight dataset.
|
||||||
if command == 'patch':
|
if command == 'patch':
|
||||||
print googleClient.users().dataSources().datasets().patch(
|
print googleClient.users().dataSources().datasets().patch(
|
||||||
userId='me',
|
userId='me',
|
||||||
dataSourceId=dataSourceId,
|
dataSourceId=dataSourceId,
|
||||||
datasetId=datasetId,
|
datasetId=datasetId,
|
||||||
body=dict(
|
body=dict(
|
||||||
dataSourceId=dataSourceId,
|
dataSourceId=dataSourceId,
|
||||||
maxEndTimeNs=maxLogNs,
|
maxEndTimeNs=maxLogNs,
|
||||||
minStartTimeNs=minLogNs,
|
minStartTimeNs=minLogNs,
|
||||||
point=googleWeightLogs,
|
point=googleWeightLogs,
|
||||||
)).execute()
|
)).execute()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|||||||
Reference in New Issue
Block a user