Skip to content

Commit

Permalink
Merge branch 'master' into feat/bypass-manual-version-bumping
Browse files Browse the repository at this point in the history
  • Loading branch information
longstone authored Aug 20, 2024
2 parents 0ab0970 + d01a6d5 commit c158d40
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
lxml
requests
garth
setuptools
46 changes: 21 additions & 25 deletions withings_sync/garmin.py
Original file line number Diff line number Diff line change
@@ -1,56 +1,52 @@
"""This module handles the Garmin connectivity."""

import io
import logging
import garth
import os
import io

log = logging.getLogger("garmin")
import garth

log = logging.getLogger("garmin")

HOME = os.getenv('HOME', '.')
GARMIN_SESSION = os.getenv('GARMIN_SESSION', HOME + '/.garmin_session')
HOME = os.getenv("HOME", ".")
GARMIN_SESSION = os.getenv('GARMIN_SESSION', os.path.join(HOME, ".garmin_session"))


class LoginSucceeded(Exception):
"""Used to raise on LoginSucceeded"""
"""Raised when login succeeds."""


class LoginFailed(Exception):
"""Used to raise on LoginFailed"""
"""Raised when login fails."""


class APIException(Exception):
"""Used to raise on APIException"""
"""Raised for API exceptions."""


class GarminConnect:
"""Main GarminConnect class"""
"""Main GarminConnect class."""

def __init__(self) -> None:
self.client = garth.Client()

def login(self, email=None, password=None):
logged_in = False
if os.path.exists(GARMIN_SESSION):
self.client.load(GARMIN_SESSION)
try:
self.client.username
logged_in = True
except Exception:
pass

if not logged_in:
try:
self.client.login(email, password)
self.client.dump(GARMIN_SESSION)
except Exception as ex:
raise APIException("Authentication failure: {}. Did you enter correct credentials?".format(ex))
if hasattr(self.client, "username"):
return

try:
self.client.login(email, password)
self.client.dump(GARMIN_SESSION)
except Exception as ex:
raise APIException(
f"Authentication failure: {ex}. Did you enter correct credentials?"
)

def upload_file(self, ffile):
"""upload fit file to Garmin connect"""
# Convert the fitfile to a in-memory file for upload
"""Upload fit file to Garmin Connect."""
fit_file = io.BytesIO(ffile.getvalue())
fit_file.name = 'withings.fit'
fit_file.name = "withings.fit"
self.client.upload(fit_file)
return True
7 changes: 6 additions & 1 deletion withings_sync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,9 @@ def prepare_syncdata(height, groups):
collected_metrics = "weight data"
if "BLOOD_PRESSURE" in ARGS.features:
collected_metrics += " or blood pressure"
elif "diastolic_blood_pressure" in group_data:
collected_metrics += ", but blood pressure (to enable sync set --features BLOOD_PRESSURE)"


logging.info(
"%s This Withings metric contains no %s. Not syncing...", dt, collected_metrics
Expand Down Expand Up @@ -478,8 +481,10 @@ def sync():
# Save this sync so we don't re-download the same data again (if no range has been specified)
if not ARGS.fromdate:
withings.set_lastsync()
else:
elif ARGS.garmin_username is None:
logging.info("No Garmin username - skipping sync")
else:
logging.info("No Garmin data selected - skipping sync")
else:
logging.info("Skipping upload")
return 0
Expand Down

0 comments on commit c158d40

Please sign in to comment.