-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPull_fitbit_data.py
192 lines (143 loc) · 5.89 KB
/
Pull_fitbit_data.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# coding: utf-8
# # Tutorial: How to acquire your data from Fitbit's API.
# Jan 21, 2018
# Below are examples of the types of data you can aquire from Fitbit's API. To [register your app](https://dev.fitbit.com/apps/new) and set up an API, see this [helpful tutorial](http://pdwhomeautomation.blogspot.com/2015/03/using-fitbit-api-on-raspberry-pi-with.html).
#
# The codes below were modified from [Paul's Geek Dad Blog](http://pdwhomeautomation.blogspot.com/2015/03/using-fitbit-api-on-raspberry-pi-with.html)
# Get heart rate data in second intervals.
# In[1]:
import requests
import json
import pandas as pd
from time import sleep
# put the token for your app in between the single quotes
token = '<enter your token here>'
# make a list of dates
# ref: http://stackoverflow.com/questions/993358/creating-a-range-of-dates-in-python
# You can change the start and end date as you want
# Just make sure to use the yyyy-mm-dd format
start_date = '<start date>'
end_date = '<end date>'
datelist = pd.date_range(start = pd.to_datetime(start_date),
end = pd.to_datetime(end_date)).tolist()
for ts in datelist:
date = ts.strftime('%Y-%m-%d')
url = 'https://api.fitbit.com/1/user/-/activities/heart/date/' + date + '/1d/1sec/time/00:00/23:59.json'
filename = 'HR'+ date +'.json'
response = requests.get(url=url, headers={'Authorization':'Bearer ' + token})
if response.ok:
with open(filename, 'w') as f:
json.dump(response.content, f)
print (date + ' is saved!')
sleep(30)
else:
print ('The file of %s is not saved due to error!' % date)
sleep(30)
# Get sleep data.
# In[ ]:
# put the token for your app in between the single quotes
token = '<enter your token>'
# make a list of dates
# ref: http://stackoverflow.com/questions/993358/creating-a-range-of-dates-in-python
# You can change the start and end date as you want
# Just make sure to use the yyyy-mm-dd format
start_date = '<start date>'
end_date = '<end date>'
datelist = pd.date_range(start = pd.to_datetime(start_date),
end = pd.to_datetime(end_date)).tolist()
for ts in datelist:
date = ts.strftime('%Y-%m-%d')
url = 'https://api.fitbit.com/1/user/-/sleep/date/' + date + '.json'
filename = 'sleep'+ date +'.json'
response = requests.get(url=url, headers={'Authorization':'Bearer ' + token})
if response.ok:
with open(filename, 'w') as f:
json.dump(response.content, f)
print (date + ' is saved!')
sleep(30)
else:
print ('The file of %s is not saved due to error!' % date)
sleep(30)
# In[ ]:
Get step data.
# In[ ]:
# put the token for your app in between the single quotes
token = '<enter your token>'
# make a list of dates
# ref: http://stackoverflow.com/questions/993358/creating-a-range-of-dates-in-python
# You can change the start and end date as you want
# Just make sure to use the yyyy-mm-dd format
start_date = '<start date>'
end_date = '<end date>'
datelist = pd.date_range(start = pd.to_datetime(start_date),
end = pd.to_datetime(end_date)).tolist()
'''
The codes below use a for loop to generate one URL for each day in the datelist,
and then request each day's data and save the data into individual json files.
Because Fitbit limit 150 request per hour, I let the code sleep for 30 seconds
between each request, to meet this limitation.
'''
for ts in datelist:
date = ts.strftime('%Y-%m-%d')
url = 'https://api.fitbit.com/1/user/-/activities/steps/date/' + date + '/today/1d.json'
filename = 'step'+ date +'.json'
response = requests.get(url=url, headers={'Authorization':'Bearer ' + token})
if response.ok:
with open(filename, 'w') as f:
json.dump(response.content, f)
print (date + ' is saved!')
sleep(30)
else:
print ('The file of %s is not saved due to error!' % date)
sleep(30)
# In[ ]:
Get frequent activity data.
# In[ ]:
# put the token for your app in between the single quotes
token = '<enter your token>'
# make a list of dates
# ref: http://stackoverflow.com/questions/993358/creating-a-range-of-dates-in-python
# You can change the start and end date as you want
# Just make sure to use the yyyy-mm-dd format
start_date = '<start date>'
end_date = '<end date>'
datelist = pd.date_range(start = pd.to_datetime(start_date),
end = pd.to_datetime(end_date)).tolist()
for ts in datelist:
date = ts.strftime('%Y-%m-%d')
url = 'https://api.fitbit.com/1/user/-/activities/frequent.json'
filename = 'freq'+ date +'.json'
response = requests.get(url=url, headers={'Authorization':'Bearer ' + token})
if response.ok:
with open(filename, 'w') as f:
json.dump(response.content, f)
print (date + ' is saved!')
sleep(30)
else:
print ('The file of %s is not saved due to error!' % date)
sleep(30)
# Get activity data.
# In[ ]:
# put the token for your app in between the single quotes
token = '<enter your token>'
# make a list of dates
# ref: http://stackoverflow.com/questions/993358/creating-a-range-of-dates-in-python
# You can change the start and end date as you want
# Just make sure to use the yyyy-mm-dd format
start_date = '<start date>'
end_date = '<end date>'
datelist = pd.date_range(start = pd.to_datetime(start_date),
end = pd.to_datetime(end_date)).tolist()
for ts in datelist:
date = ts.strftime('%Y-%m-%d')
url = 'https://api.fitbit.com/1/user/-/activities.json'
filename = 'best'+ date +'.json'
response = requests.get(url=url, headers={'Authorization':'Bearer ' + token})
if response.ok:
with open(filename, 'w') as f:
json.dump(response.content, f)
print (date + ' is saved!')
sleep(30)
else:
print ('The file of %s is not saved due to error!' % date)
sleep(30)