Skip to content

Instantly share code, notes, and snippets.

@ltddev
Created July 14, 2015 03:44
Show Gist options
  • Select an option

  • Save ltddev/60442e91b2fb15038c6a to your computer and use it in GitHub Desktop.

Select an option

Save ltddev/60442e91b2fb15038c6a to your computer and use it in GitHub Desktop.
FitbitSleepHandler.py
'''
FitbitSleepHandler.py (class)
This module is meant to utilize all of the capability of the exposed python sleep api to analyze sleep patterns perhaps above and beyond what fitbit itself does.
These are my current 4 keys:
consumer: "66febeae096fe9442d10dd3e92d54de2"
consumer secret: "b8b002ddf50dd3525f57b9a350051b97"
reource owner: resource_owner_key='20eb22828f652f729002ba0f855d07f3'
resource owner secret: resource_owner_secret='48eaa2e1e11ea551d1e3579c3274cccd')
There is only one single sleep-related call made from an authorized client and that function signature is get_sleep(Datetime dt). This function returns a conglomerate data type of embedded lists and dictionaries.
If sleepUnitsDict is the main dictionary returned it has two keys, 'sleep' and 'summary'. sleepUnitsDict['sleep'] returns a list of one element which contains a number of dictionaries.
sleepUnitsDict['sleep']
sleepUnitsDict['summary']
The 'sleep' is a dictionary with these 17 keys:
logId
dateOfSleep
minutesToFallAsleep
awakeningsCount
minutesAwake
timeInBed
minutesAsleep
awakeDuration
efficiency
isMainSleep
startTime
restlessCount
duration
restlessDuration
minuteData
minutesAfterWakeup
awakeCount
Each key has a single value except 'minuteData' which is a dictionary of time values presumably used by other calculations. The 'summary'is another dictionary with these 3 keys:
totalTimeInBed
totalMinutesAsleep
totalSleepRecords
Therefore once you get the two parts of returned value you can get at the data directly by using these keys.
sleepUnitsDict = authd_client.get_sleep(datetime)
sleepList = sleepUnitsDict['sleep']
summaryDict = sleepUnitsDict['summary']
x = sleepList[0]
dataitem = x['one of the sleep keys']
dataitem = summaryDict['one of the 3 summary keys']
'''
class FitBitImpl:
def __init__(self,ownerKey,ownerSecret,resourceOwnerKey,resourceOwnerSecret):
self.ownerKey = ownerKey
self.ownerSecret = ownerSecret
self.resourceOwnerKey = resourceOwnerKey
self.resourceOwnerSecret =resourceOwnerSecret
def printDeviceInfo(self):
import fitbit
authd_client = fitbit.Fitbit(ownerKey,ownerSecret,resource_owner_key=resourceOwnerKey,resource_owner_secret=resourceOwnerSecret)
devicesList = authd_client.get_devices()
print 'Number of devices:', len(devicesList)
myDevice = devicesList[0]
print 'Device Info'
print '==========='
# more useful means to populate data fields is to reference by name.
batteryLevel = myDevice['battery']
print 'Battery level:',batteryLevel
lastSyncTime = myDevice['lastSyncTime']
print 'Last sync time:',lastSyncTime
macAddress = myDevice['mac']
print 'MAC Address:', macAddress
deviceType = myDevice['type']
print 'Device Type:',deviceType
deviceId = myDevice['id']
print 'Device Id:', deviceId
deviceVersion = myDevice['deviceVersion']
print 'Device Version: ', deviceVersion
# looping useful in case where you don't assume any keys
# or you want to process all keys-values
for key in myDevice:
print key + ': ' + myDevice[key]
# Returns the sleep portion of result for a given datetime
def sleepData(self,datetime):
import fitbit
authd_client = fitbit.Fitbit(ownerKey,ownerSecret,resource_owner_key=resourceOwnerKey,resource_owner_secret=resourceOwnerSecret)
sleepUnitsDict = authd_client.get_sleep(datetime)
sleepList = sleepUnitsDict['sleep']
sleepDict = sleepList[0] #there only is one item in this list, a dictionar
return sleepDict
# Returns the summary part of result for a given datetime
def sleepSummary(self, datetime):
import fitbit
authd_client = fitbit.Fitbit(ownerKey,ownerSecret,resource_owner_key=resourceOwnerKey,resource_owner_secret=resourceOwnerSecret)
sleepDataDict = authd_client.get_sleep(datetime)
summaryDict = sleepDataDict['summary']
return summaryDict
# Diagnostic simply prints to screen demonstrating api
def printSleepData(self,datetime):
import fitbit
authd_client = fitbit.Fitbit(ownerKey,ownerSecret,resource_owner_key=resourceOwnerKey,resource_owner_secret=resourceOwnerSecret)
sleepUnitsDict = authd_client.get_sleep(datetime)
sleepList = sleepUnitsDict['sleep']
summaryDict = sleepUnitsDict['summary']
print 'sleepList length: ',len(sleepList)
print 'sleepList type:', type(sleepList)
print 'summaryDict length: ',len(summaryDict)
print 'summaryDict type ',type(summaryDict)
sleepDict = sleepList[0] #there only is one item in this list, a dictionary.
print
for key in sleepDict:
#print key + ':' + str(sleepDict[key])
print key, str(sleepDict[key])
print
print '++++++++++++++++++++++++++++++++++'
print
for key in summaryDict:
print key, summaryDict[key]
print
#print key + ':' + str(summaryDict[key])
# #print sleepUnitsDict
#print
#___________________________________________________________________________
# Here we instantiate the class then invoke class methods
ownerKey = '66febeae096fe9442d10dd3e92d54de2'
ownerSecret = 'b8b002ddf50dd3525f57b9a350051b97'
resourceOwnerKey = '20eb22828f652f729002ba0f855d07f3'
resourceOwnerSecret = '48eaa2e1e11ea551d1e3579c3274cccd'
import datetime
import console
date = datetime.date(2015,7,4) # July 4 2015, night of Shira's wedding.
# instantiate class then invoke some void methods (no return types)
fitbitimpl = FitBitImpl(ownerKey,ownerSecret,resourceOwnerKey,resourceOwnerSecret)
console.clear()
fitbitimpl.printSleepData(date)
fitbitimpl.sleepSummary(date)
console.hide_output()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment