-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactivity.py
95 lines (80 loc) · 3.18 KB
/
activity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import fitbit
import time
from datetime import timedelta, date, datetime
import json
from convertor import Convertor
class FitbitActivity(object):
"""Fitbit Activity class"""
def __init__(self, client_id, client_secret, access_token, refresh_token, token_expires_at=None,
types=['Run']):
self.fitbit_client = fitbit.Fitbit(client_id, client_secret, access_token=access_token,
refresh_token=refresh_token, expires_at=token_expires_at,
refresh_cb=self.refresh_token_cb)
self.token = self.fitbit_client.client.session.token
self.activity_types = types
def refresh_token_cb(self, token):
print('Access token updated')
self.token = token
def get_activity_distances(self, dist_data, start_date=None, callurl=None):
"""
Get distances from activities data starting from a given day from Fitbit
dist_data -- known distance so far (all zeros initially)
start_date -- timestamp in yyyy-mm-dd format of the start day
callurl -- url to fetch activities from (optional)
"""
# Fitbit activities list endpoint is in beta stage. It may break in the future and not directly supported
# by the python client library.
if not callurl:
callurl = '{}/user/-/activities/list.json?afterDate={}&sort=asc&offset=0&limit=20' \
.format('https://api.fitbit.com/1', start_date)
activities_raw = self.fitbit_client.make_request(callurl)
activities = activities_raw['activities']
for activity in activities:
# Only interested in activities of certain types
if activity['activityName'] not in self.activity_types:
continue
# get distance for a particular day since start
days_since = self.convertor.daysSinceStart(activity['startTime'][:10])
day_km_dist = self.convertor.distance_in_kms(activity['distance'],
activity['distanceUnit'])
dist_data[days_since] += day_km_dist
if activities_raw['pagination']['next'] != '':
return self.get_activity_distances(dist_data,
callurl=activities_raw['pagination']['next'])
return dist_data
def get_distances(self, start_date='2017-01-01'):
"""
Get distances since the given start date.
"""
self.convertor = Convertor(start_date)
# Init expected steps per day and actual steps to 0 (updated later)
dist_data = []
for single_date in self.convertor.daterange():
dist_data.insert(self.convertor.daysSinceStart(single_date), 0)
# Get distance data in kms for each day since start date
dist_data = self.get_activity_distances(dist_data, start_date=start_date)
# find cummulative distances now
cumm_dist_data = dist_data
cumm_val = 0
for i,dist in enumerate(cumm_dist_data):
cumm_val += dist
cumm_dist_data[i] = cumm_val
return cumm_dist_data
def access_token(self):
"""
Fitbit library takes care of updating access_token based on expiry. This returns the new
access_token value.
"""
return self.token['access_token']
def refresh_token(self):
"""
Fitbit library takes care of updating access_token based on expiry. This returns the new
access_token value.
"""
return self.token['refresh_token']
def token_expires_at(self):
"""
Fitbit library takes care of updating access_token based on expiry. This returns the new
access_token value.
"""
return self.token['expires_at']