diff --git a/withings_sync/garmin.py b/withings_sync/garmin.py index 26f98d8..1629427 100644 --- a/withings_sync/garmin.py +++ b/withings_sync/garmin.py @@ -5,6 +5,7 @@ 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")} @@ -12,6 +13,7 @@ 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): @@ -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): diff --git a/withings_sync/sync.py b/withings_sync/sync.py index e874d00..e5fc656 100644 --- a/withings_sync/sync.py +++ b/withings_sync/sync.py @@ -21,7 +21,7 @@ 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 @@ -29,7 +29,7 @@ def load_variable(env_var, secrets_file): 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. @@ -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") @@ -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()