I am using the Fitbit Python Library to connect to the fitbit api: https://github.com/orcasgit/python-fitbit
I am not very familiar with fitbit, but I believe I am on the right path for what i am trying to do.
I have data that looks like this:
{u'activities': [],
u'goals':
{u'activeMinutes': 30, u'distance': 5, u'caloriesOut': 2364, u'steps': 10000},
u'summary':
{u'distances':
[{u'distance': 3.49, u'activity': u'total'},
{u'distance': 3.49, u'activity': u'tracker'},
{u'distance': 0, u'activity': u'loggedActivities'},
{u'distance': 1.27, u'activity': u'veryActive'},
{u'distance': 0.22, u'activity': u'moderatelyActive'},
{u'distance': 2, u'activity': u'lightlyActive'},
{u'distance': 0, u'activity': u'sedentaryActive'}],
u'sedentaryMinutes': 394,
u'lightlyActiveMinutes': 153,
u'caloriesOut': 1547,
u'caloriesBMR': 942,
u'marginalCalories': 414,
u'fairlyActiveMinutes': 8,
u'veryActiveMinutes': 29,
u'activityCalories': 750,
u'steps': 8277,
u'activeScore': -1}}'
Not its normally all on one line but i returned each row to make it easier to read.
I am trying to return only a couple of the rows into columns into a csv that would look like this:
Here is the code I have, most of it is pulled from this website with me modifying it to pull activity instead of sleep summary: https://towardsdatascience.com/collect-your-own-fitbit-data-with-python-ff145fa10873
import fitbit
import gather_keys_oauth2 as Oauth2
import pandas as pd
import datetime
import csv
CLIENT_ID = '22CZ94'
CLIENT_SECRET = '06a52bc5d8239790f630ffdd19377ba2'
server = Oauth2.OAuth2Server(CLIENT_ID, CLIENT_SECRET)
server.browser_authorize()
ACCESS_TOKEN = str(server.fitbit.client.session.token['access_token'])
REFRESH_TOKEN = str(server.fitbit.client.session.token['refresh_token'])
auth2_client = fitbit.Fitbit(CLIENT_ID, CLIENT_SECRET, access_token='eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiI2V0gyTlAiLCJhdWQiOiIyMkNaOTQiLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJzY29wZXMiOiJyc29jIHJzZXQgcmFjdCBybG9jIHJ3ZWkgcmhyIHJwcm8gcm51dCByc2xlIiwiZXhwIjoxNTY5Mjc5OTAxLCJpYXQiOjE1Mzc3NDM5MDF9.1StrKUUJwidejZ2pbCZzkIBG8FztQiLMvBql6fgEpaY', refresh_token=REFRESH_TOKEN)
fit_statsSum = auth2_client.activities(date='2018-09-25')['activities'][0]
actsummarypdf = pd.DataFrame({'SedentaryMinutes':fit_statsSum[u'sedentaryMinutes'],
'lightlyActiveMinutes':fit_statsSum['lightlyActiveMinutes'],
'fairlyActiveMinutes':fit_statsSum['fairlyActiveMinutes'],
'veryActiveMinutes':fit_statsSum['veryActiveMinutes'],
'steps':fit_statsSum['steps']
})
actsummarypdf.to_csv('c:\python-fitbit-master\Activities' + '2018-09-25' + '.csv')
With the code like that I get:
Traceback (most recent call last):
File ".\autho2_activity_summary.py", line 28, in <module>
fit_statsSum = auth2_client.activities(date='2018-09-25')['activities'][0]
IndexError: list index out of range
If I remove the [0], i get:
Traceback (most recent call last):
File ".\autho2_activity_summary.py", line 30, in <module>
actsummarypdf =
pd.DataFrame({'SedentaryMinutes':fit_statsSum['sedentaryMinutes'],
TypeError: list indices must be integers or slices, not str
ive also tried using u'sedentaryMinutes' and "u'sedentaryMinutes'" but no change.
Any help on what I am missing would be truly appreciated.
The IndexError means that you are trying to access an item in a list at a index that doesn't exist.
This will presumably return an object that looks like the data at the beginning of your question.
And the value of the key
activities
is an empty list.So when you try to access the item at index
0
(the first item), you will get an error. There is no first item in an empty list.That's what the exception message means. But I can't tell you how to proceed, because you haven't told us anything about what you are trying to do.
If your fitbit activity data set is empty, you probably need to go for a run?