diff --git a/app/mifit_to_influxdb.py b/app/mifit_to_influxdb.py
--- a/app/mifit_to_influxdb.py
+++ b/app/mifit_to_influxdb.py
#
@@ -415,6 +415,84 @@ def get_band_data(auth_info, config):
#
return result_set, serial
#
+def get_blood_oxygen_data(auth_info, config):
#
+ ''' Retrieve stress level information
#
+ ''' calculate the times that the api query should check between
#
+ today = datetime.datetime.today()
#
+ today_end = datetime.datetime.combine(today, datetime.datetime.max.time())
#
+ query_start_d = today - datetime.timedelta(days=config['QUERY_DURATION'])
#
+ # Make it midnight - the api doesn't seem to like mid-day queries
#
+ query_start = datetime.datetime.combine(query_start_d, datetime.datetime.min.time())
#
+ print("Retrieving blood oxygen data")
#
+ band_data_url=f"https://api-mifit.zepp.com/users/{auth_info['token_info']['user_id']}/events"
#
+ 'apptoken': auth_info['token_info']['app_token'],
#
+ 'from': query_start.strftime('%s000'),
#
+ 'to': today_end.strftime('%s000'),
#
+ "eventType": "blood_oxygen",
#
+ "timeZone" : "Europe/London"
#
+ response=requests.get(band_data_url,params=data,headers=headers)
#
+ r_json = response.json()
#
+ if "items" not in r_json:
#
+ for blood in r_json['items']:
#
+ if blood['subType'] == "odi":
#
+ rows.append(processODIEvent(blood))
#
+ elif blood['subType'] == "osa_event":
#
+ rows.append(processOSAEvent(blood))
#
+def processOSAEvent(record):
#
+ ''' Process a possible Obstructive Sleep Apnea event
#
+ osa_record = json.loads(record['extra'])
#
+ "timestamp": int(record['timestamp']) * 1000000, # Convert to nanos
#
+ "spo2_decrease" : float(osa_record['spo2_decrease']),
#
+ "blood_event" : "osa"
#
+def processODIEvent(record):
#
+ ''' Process an ODI event
#
+ "timestamp": int(record['timestamp']) * 1000000, # Convert to nanos
#
+ "odi_read" : float(record['odi']),
#
+ # Not sure what this is, skipping for now
#
+ # (I *think* it might just be a record ID)
#
+ # "odi_number" : int(record['odiNum']),
#
+ # There are also "cost" and "valid"
#
+ "score" : float(record['score']),
#
+ "blood_event" : "odi"
#
def get_stress_data(auth_info, config):
#
''' Retrieve stress level information
#
@@ -444,7 +522,7 @@ def get_stress_data(auth_info, config):
#
response=requests.get(band_data_url,params=data,headers=headers)
#
r_json = response.json()
#
if "items" not in r_json:
#
for stress in r_json['items']:
#
@@ -538,6 +616,13 @@ def main():
#
print("Failed to collect stress data")
#
+ blood_o2 = get_blood_oxygen_data(auth_info, config)
#
+ result_set = result_set + blood_o2
#
+ print("Failed to collect blood oxygen data")
#
write_results(result_set, serial, config)