forked from jaroslawhartman/withings-garmin-v2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
executable file
·225 lines (169 loc) · 5.84 KB
/
test.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/usr/bin/env python
import sys
import requests
import json
import argparse
from datetime import date
from datetime import datetime
import time
class Withings():
AUTHORIZE_URL = 'https://account.withings.com/oauth2_user/authorize2'
TOKEN_URL = 'https://account.withings.com/oauth2/token'
GETMEAS_URL = 'https://wbsapi.withings.net/measure?action=getmeas'
APP_CONFIG = 'config/withings_app.json'
USER_CONFIG = 'config/withings_user.json'
# 1 Weight (kg)
# 4 Height (meter)
# 5 Fat Free Mass (kg)
# 6 Fat Ratio (%)
# 8 Fat Mass Weight (kg)
# 9 Diastolic Blood Pressure (mmHg)
# 10 Systolic Blood Pressure (mmHg)
# 11 Heart Pulse (bpm) - only for BPM devices
# 12 Temperature
# 54 SP02 (%)
# 71 Body Temperature
# 73 Skin Temperature
# 76 Muscle Mass
# 77 Hydration
# 88 Bone Mass
# 91 Pulse Wave Velocity
MEASTYPE_WEIGHT = 1
class WithingsConfig(Withings):
config = {}
config_file = ""
def __init__(self, config_file):
self.config_file = config_file
self.read()
def read(self):
try:
with open(self.config_file) as f:
self.config = json.load(f)
except (ValueError, FileNotFoundError):
print("Can't read config file " + self.config_file)
self.config = {}
def write(self):
with open(self.config_file, "w") as f:
json.dump(self.config, f, indent=4, sort_keys=True)
class WitingsOAuth2(Withings):
app_config = user_config = None
def __init__(self):
app_cfg = WithingsConfig(Withings.APP_CONFIG)
self.app_config = app_cfg.config
user_cfg = WithingsConfig(Withings.USER_CONFIG)
self.user_config = user_cfg.config
if not self.user_config.get('access_token'):
if not self.user_config.get('authentification_code'):
self.user_config['authentification_code'] = self.getAuthenticationCode()
self.getAccessToken()
self.refreshAccessToken()
app_cfg.write()
user_cfg.write()
def getAuthenticationCode(self):
params = {
"response_type" : "code",
"client_id" : self.app_config['client_id'],
"state" : "OK",
"scope" : "user.metrics",
"redirect_uri" : self.app_config['callback_url'],
}
print("***************************************")
print("* W A R N I N G *")
print("***************************************")
print()
print("User interaction needed to get Authentification Code!")
print()
print("Open the following URL in your web browser and copy back the token. You will have *30 seconds* before the token expires. HURRY UP!")
print("(This is one-time activity)")
print()
url = Withings.AUTHORIZE_URL + '?'
for key, value in params.items():
url = url + key + '=' + value + "&"
print(url)
print()
authentification_code = input("Token : ")
return authentification_code
def getAccessToken(self):
print("Get Access Token")
params = {
"grant_type" : "authorization_code",
"client_id" : self.app_config['client_id'],
"client_secret" : self.app_config['consumer_secret'],
"code" : self.user_config['authentification_code'],
"redirect_uri" : self.app_config['callback_url'],
}
req = requests.post(Withings.TOKEN_URL, params )
accessToken = req.json()
print(accessToken)
if(accessToken.get('errors')):
print("Received error(s):")
for message in accessToken.get('errors'):
error = message.get('message')
print(" " + error)
if "invalid code" in error:
print("Removing invalid authentification_code")
self.user_config['authentification_code'] = ''
print()
print("If it's regarding an invalid code, try to start the script again to obtain a new link.")
self.user_config['access_token'] = accessToken.get('access_token')
self.user_config['refresh_token'] = accessToken.get('refresh_token')
self.user_config['userid'] = accessToken.get('userid')
def refreshAccessToken(self):
print("Refresh Access Token")
params = {
"grant_type" : "refresh_token",
"client_id" : self.app_config['client_id'],
"client_secret" : self.app_config['consumer_secret'],
"refresh_token" : self.user_config['refresh_token'],
}
req = requests.post(Withings.TOKEN_URL, params )
accessToken = req.json()
print(accessToken)
if(accessToken.get('errors')):
print("Received error(s):")
for message in accessToken.get('errors'):
error = message.get('message')
print(" " + error)
if "invalid code" in error:
print("Removing invalid authentification_code")
self.user_config['authentification_code'] = ''
print()
print("If it's regarding an invalid code, try to start the script again to obtain a new link.")
self.user_config['access_token'] = accessToken.get('access_token')
self.user_config['refresh_token'] = accessToken.get('refresh_token')
self.user_config['userid'] = accessToken.get('userid')
class WithingsAccount(Withings):
def __init__(self):
self.withings = WitingsOAuth2()
def getMeasurements(self, startdate, enddate):
print("Get Measurements")
params = {
"access_token" : self.withings.user_config['access_token'],
"meastype" : Withings.MEASTYPE_WEIGHT,
"category" : 1,
"startdate" : startdate,
"enddate" : enddate,
}
req = requests.post(Withings.GETMEAS_URL, params )
measurements = req.json()
print(measurements)
def main():
usage = 'usage: sync.py [options]'
p = argparse.ArgumentParser(usage=usage)
p.add_argument('-f', '--fromdate', type=date, default=date.today(), metavar='<date>')
# p.add_argument('-f', '--fromdate', type='date', default=date.today(), metavar='<date>')
p.add_argument('-t', '--todate', type=date, default=date.today(), metavar='<date>')
args = p.parse_args()
print(args)
fromdate = args.fromdate
todate = args.todate
print(fromdate)
print(todate)
startdate = int(time.mktime(fromdate.timetuple()))
enddate = int(time.mktime(todate.timetuple())) + 86399
print(startdate)
print(enddate)
withingsAccount = WithingsAccount()
withingsAccount.getMeasurements(startdate, enddate)
if __name__ == '__main__':
main()