""" NAS Sharing Session Management Handles session checking and credential management for Synology DSM sharing links. Chrome profile handles cookie persistence automatically - no JSON files needed. """ import os import time from typing import Optional, Tuple from selenium import webdriver from selenium.webdriver.common.by import By def get_username_password() -> Tuple[str, str]: """ Get credentials from environment variables. Returns: Tuple of (username, password) Raises: ValueError: If credentials not found in environment """ username = os.getenv("NAS_USERNAME") password = os.getenv("NAS_PASSWORD") if not username or not password: raise ValueError("NAS_USERNAME and NAS_PASSWORD must be set in .env.local") return username, password class SharingSessionManager: """ Manages DSM session check for sharing links. Chrome profile handles cookies automatically - no JSON files needed. """ def __init__(self, driver: webdriver.Chrome): """ Initialize session manager. Args: driver: Selenium Chrome WebDriver instance """ self.driver = driver def ensure_logged_in_page(self) -> None: """ Navigate to DSM URL to trigger Chrome profile cookie loading. Chrome profile handles cookies automatically. Raises: RuntimeError: If driver not initialized ValueError: If NAS_DSM_URL not set in environment """ if not self.driver: raise RuntimeError("[Session] ❌ Driver not initialized") dsm_url = os.getenv("NAS_DSM_URL") if not dsm_url: raise ValueError("NAS_DSM_URL must be set in .env.local") print("[Session] 🌐 Navigating to DSM URL...") self.driver.get(dsm_url) time.sleep(2) # Wait for cookies to load from profile print("[Session] ✅ Chrome profile cookies loaded automatically") def is_logged_in(self) -> bool: """ Check if already logged in to DSM. Chrome profile cookies handle authentication automatically. Returns: True if logged in, False otherwise """ if not self.driver: return False try: # Look for DSM-specific elements that indicate logged-in state elem = self.driver.find_element(By.XPATH, "//div[contains(text(), 'Synology Drive')]") return elem is not None except: return False