-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdump.py
executable file
·138 lines (102 loc) · 5.17 KB
/
dump.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
import fitbit
import ConfigParser
import json
import datetime
import requests.packages.urllib3
requests.packages.urllib3.disable_warnings()
def daterange(start_date, end_date):
for n in range(int ((end_date - start_date).days)):
yield start_date + datetime.timedelta(n)
#Load Settings
parser = ConfigParser.SafeConfigParser()
parser.read('config.ini')
CI_id = parser.get('Login Parameters', 'CLIENT_ID')
CI_client_secret = parser.get('Login Parameters', 'CLIENT_SECRET')
CI_access_token = parser.get('Login Parameters', 'ACCESS_TOKEN')
CI_refresh_token = parser.get('Login Parameters', 'REFRESH_TOKEN')
name = parser.get('Storage Parameters', 'NAME')
start_date_string = parser.get('Storage Parameters', 'START_DATE')
end_date_string = parser.get('Storage Parameters', 'END_DATE')
# Parse date
start_date = datetime.datetime.strptime(start_date_string, "%Y-%m-%d").date()
end_date = datetime.datetime.strptime(end_date_string, "%Y-%m-%d").date()
date_selected = start_date
for date_selected in daterange(start_date, end_date):
date_string = date_selected.strftime('%Y-%m-%d')
authd_client = fitbit.Fitbit(CI_id, CI_client_secret, oauth2=True, access_token=CI_access_token, refresh_token=CI_refresh_token)
#iterate over days
activities = authd_client.activities(date = date_string)
f = open( name + '_' + date_string+ '_' + 'activities.json', 'w')
json.dump(activities, f)
f.close()
# Steps
intra_steps = authd_client.intraday_time_series('activities/steps', base_date = date_string , detail_level = '1min', start_time = None, end_time = None)
f = open( name + '_' + date_string+ '_' + 'steps.json', 'w')
json.dump(intra_steps, f)
f.close()
f_csv = open( name + '_steps.csv', 'a')
for entry in intra_steps["activities-steps-intraday"]["dataset"]:
timestamp = date_string + " " + entry["time"] + " UTC"
f_csv.write(name + ",steps,"+ timestamp + "," + str(entry["value"])+"\n")
f_csv.close()
# Calories
intra_calories = authd_client.intraday_time_series('activities/calories', base_date = date_string , detail_level = '1min', start_time = None, end_time = None)
f = open( name + '_' + date_string+ '_' + 'calories.json', 'w')
json.dump(intra_calories, f)
f.close()
f_csv = open( name + '_calories.csv', 'a')
for entry in intra_calories["activities-calories-intraday"]["dataset"]:
timestamp = date_string + " " + entry["time"] + " UTC"
f_csv.write(name + ",calories,"+ timestamp + "," + str(entry["value"])+"\n")
f_csv.close()
# Distance
intra_distance = authd_client.intraday_time_series('activities/distance', base_date = date_string , detail_level = '1min', start_time = None, end_time = None)
f = open( name + '_' + date_string+ '_' + 'distance.json', 'w')
json.dump(intra_distance, f)
f.close()
f_csv = open( name + '_distance.csv', 'a')
for entry in intra_distance["activities-distance-intraday"]["dataset"]:
timestamp = date_string + " " + entry["time"] + " UTC"
f_csv.write(name + ",distance,"+ timestamp + "," + str(entry["value"])+"\n")
f_csv.close()
# Floors
intra_floors = authd_client.intraday_time_series('activities/floors', base_date = date_string , detail_level = '1min', start_time = None, end_time = None)
f = open( name + '_' + date_string+ '_' + 'floors.json', 'w')
json.dump(intra_floors, f)
f.close()
f_csv = open( name + '_floors.csv', 'a')
for entry in intra_floors["activities-floors-intraday"]["dataset"]:
timestamp = date_string + " " + entry["time"] + " UTC"
f_csv.write(name + ",floors,"+ timestamp + "," + str(entry["value"])+"\n")
f_csv.close()
# Elevation
intra_elevation = authd_client.intraday_time_series('activities/elevation', base_date = date_string , detail_level = '1min', start_time = None, end_time = None)
f = open( name + '_' + date_string+ '_' + 'elevation.json', 'w')
json.dump(intra_elevation, f)
f.close()
f_csv = open( name + '_elevation.csv', 'a')
for entry in intra_elevation["activities-elevation-intraday"]["dataset"]:
timestamp = date_string + " " + entry["time"] + " UTC"
f_csv.write(name + ",elevation,"+ timestamp + "," + str(entry["value"])+"\n")
f_csv.close()
# Heart
print "intra heart for day " + date_string
intra_heart = authd_client.intraday_time_series('activities/heart', base_date = date_string , detail_level = '1sec', start_time = None, end_time = None)
f = open( name + '_' + date_string+ '_' + 'heart.json', 'w')
json.dump(intra_heart, f)
f.close()
f_csv = open( name + '_heart.csv', 'a')
for entry in intra_heart["activities-heart-intraday"]["dataset"]:
timestamp = date_string + " " + entry["time"] + " UTC"
f_csv.write(name + ",heart,"+ timestamp + "," + str(entry["value"])+"\n")
f_csv.close()
sleep = authd_client.get_sleep(date_selected)
f = open( name + '_' + date_string+ '_' + 'sleep.json', 'w')
json.dump(sleep, f)
f.close()
f_csv = open( name + '_sleep.csv', 'a')
for a_sleep in sleep["sleep"]:
for entry in a_sleep["minuteData"]:
timestamp = date_string + " " + entry["dateTime"] + " UTC"
f_csv.write(name + ",sleep,"+ timestamp + "," + str(entry["value"])+"\n")
f_csv.close()