diff --git a/app.py b/app.py index b63cffe..b95321a 100644 --- a/app.py +++ b/app.py @@ -3,7 +3,8 @@ import datetime import os import time from garminconnect import Garmin -import garth + +os.environ["GARMINTOKENS"] = "/app/data" app = Flask(__name__) @@ -15,12 +16,11 @@ GOAL_KM = float(os.getenv("GOAL_KM", "80")) ALLOWED_TYPES = os.getenv("ALLOWED_TYPES", "running,treadmill_running").split(",") TEST_MODE = os.getenv("TEST_MODE", "false").lower() == "true" # Check if TEST_MODE is enabled -CACHE_TTL = int(os.getenv("CACHE_TTL", "300")) # seconds (default 5 mins) -TOKEN_PATH = "/app/data/garminconnect" +CACHE_TTL = int(os.getenv("CACHE_TTL", "300")) + # --- GLOBAL CACHE --- garmin_client = None last_login_time = 0 - cached_data = None last_fetch_time = 0 @@ -28,33 +28,21 @@ last_fetch_time = 0 def get_client(): global garmin_client, last_login_time - # Reuse client if already logged in and in memory if garmin_client: return garmin_client - # Ensure the directory in your volume exists - os.makedirs(TOKEN_PATH, exist_ok=True) - + print("🔄 Initializing Garmin client...") + try: - print("🔄 Attempting to log in using stored tokens...") - # 1. Ask garth to resume the session from the folder - garth.resume(TOKEN_PATH) - # 2. If it succeeds, initialize Garmin without credentials! - garmin_client = Garmin() - print("✅ Logged into Garmin using cached tokens") - except Exception as e: - print(f"⚠️ Token login failed or missing ({e}). Falling back to credentials...") - - # 3. If it fails, do a normal login with username and password garmin_client = Garmin(EMAIL, PASSWORD) garmin_client.login() - # 4. Save the newly acquired tokens to your persistent folder! - garth.dump(TOKEN_PATH) - print("✅ Fresh login successful, tokens saved.") + print("✅ Logged into Garmin (tokens loaded or newly saved)") + except Exception as e: + print(f"❌ Failed to log into Garmin: {e}") + raise e last_login_time = time.time() - return garmin_client diff --git a/requirements.txt b/requirements.txt index 67dd519..4287cd1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ flask requests -gunicorn -garth \ No newline at end of file +gunicorn \ No newline at end of file