-
Notifications
You must be signed in to change notification settings - Fork 2
/
fitbitWrapper.py
83 lines (66 loc) · 3.25 KB
/
fitbitWrapper.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
import ConfigParser
import fitbit
from RecordClasses import Record
import datetime as dt
import time
config_filename = 'config.ini'
# start_time and end_time should both be empty or both set
# returns heartrate stats for a dayrange with start_date included, end_date excluded
def get_heartrate_series(base_date, detail_level, start_time=None, end_time=None):
if detail_level not in ['1sec', '1min', '15min']:
raise ValueError("Period must be either '1sec', '1min', or '15min'")
client = __get_client()
beauty_stats = []
if start_time:
stats = client.intraday_time_series('activities/heart', base_date=base_date, detail_level=detail_level,
start_time=start_time, end_time=end_time)
else:
stats = client.intraday_time_series('activities/heart', base_date=base_date, detail_level=detail_level)
for elem in stats['activities-heart-intraday']['dataset']:
# get the time in datetime format
timestamp = dt.datetime.strptime(elem['time'], '%H:%M:%S')
beauty_stats.append(Record(elem['value'], dt.datetime.combine(base_date, timestamp.time())))
return beauty_stats
# returns sleeping ranges (start-end time of the sleep) for given dates, end date excluded
# includes both night sleep and naps
def get_sleep_ranges(start_date, end_date):
client = __get_client()
beauty_stats = []
for day_number in range((end_date - start_date).days):
base_date = start_date + dt.timedelta(day_number)
stats = client.sleep(date=base_date)['sleep']
for elem in stats:
start = dt.datetime.strptime(elem['startTime'].split('.')[0], "%Y-%m-%dT%H:%M:%S")
duration = dt.timedelta(minutes=elem['timeInBed'])
end = start + duration
beauty_stats.append((start, end))
return beauty_stats
# Private methods #
def __store_token(token_dict):
# Function for refreshing access_token, refresh_token, and expires_at.
config = ConfigParser.SafeConfigParser()
config.read(config_filename)
file_config = open(config_filename, 'w')
config.set('Login Parameters', 'ACCESS_TOKEN', token_dict['access_token'])
config.set('Login Parameters', 'REFRESH_TOKEN', token_dict['refresh_token'])
config.set('Login Parameters', 'EXPIRES_AT', str(int(token_dict['expires_at'])))
config.write(file_config)
file_config.close()
return
def __read_config():
parser = ConfigParser.SafeConfigParser()
parser.read(config_filename)
CLIENT_ID = parser.get('Login Parameters', 'C_ID')
CLIENT_SECRET = parser.get('Login Parameters', 'C_SECRET')
ACCESS_TOKEN = parser.get('Login Parameters', 'ACCESS_TOKEN')
REFRESH_TOKEN = parser.get('Login Parameters', 'REFRESH_TOKEN')
EXPIRES_AT = parser.get('Login Parameters', 'EXPIRES_AT')
return CLIENT_ID, CLIENT_SECRET, ACCESS_TOKEN, REFRESH_TOKEN, EXPIRES_AT
def __get_client():
CLIENT_ID, CLIENT_SECRET, ACCESS_TOKEN, REFRESH_TOKEN, EXPIRES_AT = __read_config()
client = fitbit.Fitbit(CLIENT_ID, CLIENT_SECRET, oauth2=True,
access_token=ACCESS_TOKEN,
refresh_token=REFRESH_TOKEN,
expires_at=EXPIRES_AT,
refresh_cb=__store_token)
return client