Commit c33d0a9b authored by Cédrick COPOL's avatar Cédrick COPOL

API-feat: ajoute la récuperation des mesures

parent cbf40fdc
...@@ -171,6 +171,26 @@ class Device(object): ...@@ -171,6 +171,26 @@ class Device(object):
self.timezone = device["timezone"] self.timezone = device["timezone"]
break break
# Measure - Getmeas
# http://developer.withings.com/oauth2/#tag/measure%2Fpaths%2Fhttps%3A~1~1wbsapi.withings.net~1measure%3Faction%3Dgetmeas%2Fget
measTypes = (
('1', 'Weight', 'kg'),
('4', 'Height', 'm'),
('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 and scale devices,
('12', 'Temperature'),
('54', 'SP02','%'),
('71', 'Body Temperature'),
('73', 'Skin Temperature'),
('76', 'Muscle Mass'),
('77', 'Hydration'),
('88', 'Bone Mass'),
('91', 'Pulse Wave Velocity'))
measNames = {mType[1]:mType[0] for mType in measTypes}
class API(object): class API(object):
base_api = "https://wbsapi.withings.net" base_api = "https://wbsapi.withings.net"
...@@ -181,6 +201,7 @@ class API(object): ...@@ -181,6 +201,7 @@ class API(object):
def __init__(self, token): def __init__(self, token):
self.token = token self.token = token
self.get_devices() self.get_devices()
self.measures_available = [mType[1] for mType in measTypes]
@property @property
def access_token(self): def access_token(self):
...@@ -196,3 +217,40 @@ class API(object): ...@@ -196,3 +217,40 @@ class API(object):
raise Exception(f"Impossible to get devices. status: {r.status_code}. error: {r.json()['errors'][0]['message']}") raise Exception(f"Impossible to get devices. status: {r.status_code}. error: {r.json()['errors'][0]['message']}")
self.devices = [Device(device) for device in r.json()["body"]["devices"]] self.devices = [Device(device) for device in r.json()["body"]["devices"]]
def get_measure(self, name, nbDays=14, goal=False):
"""Return measures of several days from now
Parameters
----------
name: str
nbDays: int
Returns
-------
response: dict
{timestamp:{type:valeur, type:valeur}}
"""
if not name in measNames:
raise ValueError(f"measure must be at least one of this : {list(measNames.keys())}")
enddate = datetime.today()
startdate = (enddate-timedelta(days=nbDays))
params = {"enddate" : int(datetime.timestamp(enddate)),
"startdate": int(datetime.timestamp(startdate)),
"category": 1 if not goal else 2,
"meastype":measNames[name],
"action":"getmeas"
}
headers= {'Authorization': 'Bearer ' + self.access_token}
url='https://wbsapi.withings.net/measure'
r = requests.get(url, headers=headers, params=params)
if r.status_code != 200:
raise Exception("Bad request",r.json())
dates, values = [], []
for grp in r.json()["body"]["measuregrps"]:
dates.append(int(grp["date"]))
for measure in grp["measures"]:
values.append(measure["value"]*10**(measure["unit"]))
return dates[::-1], values[::-1]
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment