Skip to content

Commit

Permalink
feat: add support for garmin.cn #146
Browse files Browse the repository at this point in the history
  • Loading branch information
longstone committed Nov 29, 2024
1 parent 67c0ed2 commit 329923b
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 4 additions & 0 deletions withings_sync/garmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import os

import garth

# Temporary fix until Garth project merges https://github.com/matin/garth/issues/73
garth.http.USER_AGENT = {"User-Agent": ("GCM-iOS-5.7.2.1")}

log = logging.getLogger("garmin")

HOME = os.getenv("HOME", ".")
GARMIN_SESSION = os.getenv('GARMIN_SESSION', os.path.join(HOME, ".garmin_session"))
GARMIN_DOMAIN_CN = (os.getenv('GARMIN_DOMAIN_CN', "False").lower == 'true')


class LoginSucceeded(Exception):
Expand All @@ -31,6 +33,8 @@ class GarminConnect:

def __init__(self) -> None:
self.client = garth.Client()
if GARMIN_DOMAIN_CN:
self.client.configure(domain='garmin.cn')

def login(self, email=None, password=None):
if os.path.exists(GARMIN_SESSION):
Expand Down
14 changes: 12 additions & 2 deletions withings_sync/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
from withings_sync.fit import FitEncoderWeight, FitEncoderBloodPressure


def load_variable(env_var, secrets_file):
def load_variable(env_var, secrets_file, default_value=""):
"""Load a variable from an environment variable or from a secrets file"""
# Try to read the value from the secrets file. Silently fail if the file
# cannot be read and use an empty value
try:
with open(secrets_file, encoding='utf-8') as secret:
value = secret.read().strip("\n")
except OSError:
value = ""
value = default_value

# Load variable from environment if it exists, otherwise use the
# value read from the secrets file.
Expand All @@ -38,6 +38,7 @@ def load_variable(env_var, secrets_file):

GARMIN_USERNAME = load_variable('GARMIN_USERNAME', "/run/secrets/garmin_username")
GARMIN_PASSWORD = load_variable('GARMIN_PASSWORD', "/run/secrets/garmin_password")
GARMIN_DOMAIN_CN = load_variable('GARMIN_DOMAIN_CN', "/run/secrets/garmin_domain_cn", "False")
TRAINERROAD_USERNAME = load_variable('TRAINERROAD_USERNAME', "/run/secrets/trainerroad_username")
TRAINERROAD_PASSWORD = load_variable('TRAINERROAD_PASSWORD', "/run/secrets/trainerroad_password")

Expand Down Expand Up @@ -148,6 +149,15 @@ def date_parser(date_string):
help="Run verbosely."
)

parser.add_argument(
"--garmin-china",
"-gcn",
default=GARMIN_DOMAIN_CN,
type=bool,
action="store_true",
help="if set to true, use garmin.cn domain"
)

return parser.parse_args()


Expand Down

0 comments on commit 329923b

Please sign in to comment.