diff --git a/Dockerfile b/Dockerfile index 6de6c4d..004e69b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,8 +2,14 @@ FROM python:3.11-slim WORKDIR /app +RUN apt-get update && apt-get install -y git && rm -rf /var/lib/apt/lists/* + COPY requirements.txt . + RUN pip install --no-cache-dir -r requirements.txt + +RUN pip install --no-cache-dir git+https://github.com/cyberjunky/python-garminconnect.git@react + COPY . . EXPOSE 5000 diff --git a/app.py b/app.py index 1fb8674..949536d 100644 --- a/app.py +++ b/app.py @@ -15,7 +15,7 @@ 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" # --- GLOBAL CACHE --- garmin_client = None last_login_time = 0 @@ -27,16 +27,31 @@ last_fetch_time = 0 def get_client(): global garmin_client, last_login_time - # Reuse client if already logged in + # Reuse client if already logged in and in memory if garmin_client: return garmin_client - # Otherwise login once - garmin_client = Garmin(EMAIL, PASSWORD) - garmin_client.login() - last_login_time = time.time() + # Ensure the directory in your volume exists + os.makedirs(TOKEN_PATH, exist_ok=True) - print("✅ Logged into Garmin") + try: + # 1. Attempt to login using the tokens folder + print("🔄 Attempting to log in using stored tokens...") + garmin_client = Garmin() + garmin_client.login(TOKEN_PATH) + print("✅ Logged into Garmin using cached tokens") + except Exception as e: + print(f"⚠️ Token login failed or missing ({e}). Falling back to credentials...") + + # 2. If it fails, do a normal login with username and password + garmin_client = Garmin(EMAIL, PASSWORD) + garmin_client.login() + + # 3. Save the newly acquired tokens to your persistent folder! + garmin_client.garth.dump(TOKEN_PATH) + print("✅ Fresh login successful, tokens saved.") + + last_login_time = time.time() return garmin_client diff --git a/requirements.txt b/requirements.txt index e75c792..4287cd1 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,3 @@ flask -garminconnect requests gunicorn \ No newline at end of file