From 8dbe90f6de291198920c1af652e7b00922b79ac7 Mon Sep 17 00:00:00 2001 From: Charlotte Van Petegem Date: Fri, 29 Apr 2022 21:59:42 +0200 Subject: [PATCH] Also scrape stress data --- .../services/garmin-scraper/garmin2influx.py | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/modules/services/garmin-scraper/garmin2influx.py b/modules/services/garmin-scraper/garmin2influx.py index d5133910..e8f6c3c7 100644 --- a/modules/services/garmin-scraper/garmin2influx.py +++ b/modules/services/garmin-scraper/garmin2influx.py @@ -26,10 +26,23 @@ def hr2point(time, val): ) +def stress2point(time, val): + return Point("health") \ + .field("stress", max(val, 0)) \ + .time( + datetime.fromtimestamp(time / 1000, timezone.utc), + WritePrecision.S + ) + + def hr_for_date(api, date_to_fetch): return api.get_heart_rates(date_to_fetch.isoformat())['heartRateValues'] +def stress_for_date(api, date_to_fetch): + return api.get_stress_data(date_to_fetch.isoformat())['stressValuesArray'] + + date_to_fetch = date.today().isoformat() if len(sys.argv) > 1: date_to_fetch = sys.argv[1] @@ -39,21 +52,30 @@ date_to_fetch = date.fromisoformat(date_to_fetch) try: api = Garmin(email, password) api.login() - points = list(map( + hr_points = list(map( lambda p: hr2point(*p), hr_for_date(api, date_to_fetch - timedelta(days=1)) - )) - points += list(map( + )) + stress_points = list(map( + lambda p: stress2point(*p), + stress_for_date(api, date_to_fetch - timedelta(days=1)) + )) + hr_points += list(map( lambda p: hr2point(*p), hr_for_date(api, date_to_fetch) - )) + )) + stress_points += list(map( + lambda p: stress2point(*p), + stress_for_date(api, date_to_fetch) + )) with InfluxDBClient( url="https://stats.chvp.be:8086", token=token, org=org ) as client: write_api = client.write_api(write_options=SYNCHRONOUS) - write_api.write(bucket, org, points) + write_api.write(bucket, org, hr_points) + write_api.write(bucket, org, stress_points) except ( GarminConnectConnectionError, GarminConnectAuthenticationError,