remove garth
This commit is contained in:
@@ -3,7 +3,8 @@ import datetime
|
|||||||
import os
|
import os
|
||||||
import time
|
import time
|
||||||
from garminconnect import Garmin
|
from garminconnect import Garmin
|
||||||
import garth
|
|
||||||
|
os.environ["GARMINTOKENS"] = "/app/data"
|
||||||
|
|
||||||
app = Flask(__name__)
|
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(",")
|
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
|
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)
|
CACHE_TTL = int(os.getenv("CACHE_TTL", "300"))
|
||||||
TOKEN_PATH = "/app/data/garminconnect"
|
|
||||||
# --- GLOBAL CACHE ---
|
# --- GLOBAL CACHE ---
|
||||||
garmin_client = None
|
garmin_client = None
|
||||||
last_login_time = 0
|
last_login_time = 0
|
||||||
|
|
||||||
cached_data = None
|
cached_data = None
|
||||||
last_fetch_time = 0
|
last_fetch_time = 0
|
||||||
|
|
||||||
@@ -28,33 +28,21 @@ last_fetch_time = 0
|
|||||||
def get_client():
|
def get_client():
|
||||||
global garmin_client, last_login_time
|
global garmin_client, last_login_time
|
||||||
|
|
||||||
# Reuse client if already logged in and in memory
|
|
||||||
if garmin_client:
|
if garmin_client:
|
||||||
return garmin_client
|
return garmin_client
|
||||||
|
|
||||||
# Ensure the directory in your volume exists
|
print("🔄 Initializing Garmin client...")
|
||||||
os.makedirs(TOKEN_PATH, exist_ok=True)
|
|
||||||
|
|
||||||
try:
|
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 = Garmin(EMAIL, PASSWORD)
|
||||||
garmin_client.login()
|
garmin_client.login()
|
||||||
|
|
||||||
# 4. Save the newly acquired tokens to your persistent folder!
|
print("✅ Logged into Garmin (tokens loaded or newly saved)")
|
||||||
garth.dump(TOKEN_PATH)
|
except Exception as e:
|
||||||
print("✅ Fresh login successful, tokens saved.")
|
print(f"❌ Failed to log into Garmin: {e}")
|
||||||
|
raise e
|
||||||
|
|
||||||
last_login_time = time.time()
|
last_login_time = time.time()
|
||||||
|
|
||||||
return garmin_client
|
return garmin_client
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-2
@@ -1,4 +1,3 @@
|
|||||||
flask
|
flask
|
||||||
requests
|
requests
|
||||||
gunicorn
|
gunicorn
|
||||||
garth
|
|
||||||
Reference in New Issue
Block a user