Skip to content

Commit d28d5d0

Browse files
committed
updates Fitbit API with ability to fetch activity steps for a historical period
1 parent b3ab151 commit d28d5d0

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/fitbit/api.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# Python Standard Library Imports
2+
import datetime
3+
14
# Third Party (PyPI) Imports
25
import requests
36
import rollbar
@@ -208,20 +211,38 @@ def revoke_access(self):
208211
# Activity
209212
# https://dev.fitbit.com/build/reference/web-api/activity/
210213

211-
def get_activity_steps_past_month(self):
212-
"""Get Steps for past month
214+
def get_activity_steps_for_period(
215+
self, end_date: datetime.date, period: str = '1y'
216+
):
217+
"""Get Steps for a given period
213218
214219
Requires the 'activity' permission'
215-
https://dev.fitbit.com/docs/activity/
220+
221+
References:
222+
- https://dev.fitbit.com/docs/activity/
223+
- https://dev.fitbit.com/build/reference/web-api/activity-timeseries/get-activity-timeseries-by-date/
216224
"""
217-
response = self.get('activity-steps-monthly')
225+
resource_args = (end_date.strftime('%Y-%m-%d'), period)
226+
response = self.get('activity-steps', resource_args=resource_args)
218227
if response.status_code == 200:
219228
activity = response.json()['activities-steps']
220229
activity = activity[::-1]
221230
else:
222231
activity = None
223232
return activity
224233

234+
def get_activity_steps_past_month(self):
235+
"""Get Steps for past month"""
236+
today = self.user.profile.localized_date
237+
activity = self.get_activity_steps_for_period(today, '1m')
238+
return activity
239+
240+
def get_activity_steps_past_year(self):
241+
"""Get Steps for past year"""
242+
today = self.user.profile.localized_date
243+
activity = self.get_activity_steps_for_period(today, '1y')
244+
return activity
245+
225246
##
226247
# Body & Weight
227248
# https://dev.fitbit.com/build/reference/web-api/body/

lib/fitbit/constants/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
'refresh': '/oauth2/token',
66
'revoke': '/oauth2/revoke',
77
# activity
8-
'activity-steps-monthly': '/1/user/-/activities/steps/date/today/1m.json',
8+
'activity-steps': lambda date, period: '/1/user/-/activities/steps/date/{}/{}.json'.format(
9+
date,
10+
period,
11+
),
912
# body & weight
1013
'fat': lambda date: '/1/user/-/body/log/fat/date/{}.json'.format(date),
1114
'weight': lambda date: '/1/user/-/body/log/weight/date/{}.json'.format(

0 commit comments

Comments
 (0)