130 lines
3.5 KiB
Python
Executable file
130 lines
3.5 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
"""Open the monitor browser profile for manual login."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import logging
|
|
import sys
|
|
|
|
from monitor.browser import BrowserManager
|
|
from monitor.config import load_config, load_env_file
|
|
from storage.snapshots import SnapshotStore
|
|
|
|
|
|
def setup_logging(debug: bool = False) -> None:
|
|
level = logging.DEBUG if debug else logging.INFO
|
|
logging.basicConfig(
|
|
level=level,
|
|
format="%(asctime)s %(levelname)-5s %(message)s",
|
|
datefmt="%H:%M:%S",
|
|
)
|
|
logging.getLogger("playwright").setLevel(logging.WARNING)
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
parser = argparse.ArgumentParser(
|
|
description=(
|
|
"Open the monitor's persistent browser profile so you can log in "
|
|
"to websites manually. Sessions are saved for the main monitor."
|
|
),
|
|
)
|
|
parser.add_argument(
|
|
"--config",
|
|
default="config.yaml",
|
|
help="Path to configuration file (default: config.yaml)",
|
|
)
|
|
parser.add_argument(
|
|
"--url",
|
|
action="append",
|
|
dest="urls",
|
|
metavar="URL",
|
|
help="URL to open (can be repeated for multiple tabs)",
|
|
)
|
|
parser.add_argument(
|
|
"--websites",
|
|
action="store_true",
|
|
help="Open all enabled websites from config.yaml",
|
|
)
|
|
parser.add_argument(
|
|
"--website-id",
|
|
action="append",
|
|
dest="website_ids",
|
|
metavar="ID",
|
|
help="Open a specific configured website by id (can be repeated)",
|
|
)
|
|
parser.add_argument(
|
|
"--debug",
|
|
action="store_true",
|
|
help="Enable debug logging",
|
|
)
|
|
return parser.parse_args()
|
|
|
|
|
|
def resolve_urls(args: argparse.Namespace, config) -> list[str]:
|
|
"""Build the list of URLs to open from CLI flags."""
|
|
urls: list[str] = []
|
|
|
|
if args.urls:
|
|
urls.extend(args.urls)
|
|
|
|
if args.websites:
|
|
urls.extend(site.url for site in config.websites if site.enabled)
|
|
|
|
if args.website_ids:
|
|
site_map = {site.id: site for site in config.websites}
|
|
for site_id in args.website_ids:
|
|
site = site_map.get(site_id)
|
|
if site is None:
|
|
raise ValueError(f"Unknown website id: {site_id}")
|
|
if not site.enabled:
|
|
logging.getLogger(__name__).warning(
|
|
"Website '%s' is disabled in config but opening anyway",
|
|
site_id,
|
|
)
|
|
urls.append(site.url)
|
|
|
|
# Preserve order, remove duplicates
|
|
seen: set[str] = set()
|
|
unique_urls: list[str] = []
|
|
for url in urls:
|
|
if url not in seen:
|
|
seen.add(url)
|
|
unique_urls.append(url)
|
|
|
|
return unique_urls
|
|
|
|
|
|
def main() -> int:
|
|
args = parse_args()
|
|
setup_logging(debug=args.debug)
|
|
load_env_file()
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
try:
|
|
config = load_config(args.config)
|
|
except FileNotFoundError as exc:
|
|
logger.error("%s", exc)
|
|
return 1
|
|
|
|
try:
|
|
urls = resolve_urls(args, config)
|
|
except ValueError as exc:
|
|
logger.error("%s", exc)
|
|
return 1
|
|
|
|
snapshot_store = SnapshotStore(
|
|
base_directory=config.monitor.snapshot_directory,
|
|
retention_count=config.monitor.snapshot_retention_count,
|
|
)
|
|
browser_manager = BrowserManager(config=config, snapshot_store=snapshot_store)
|
|
|
|
logger.info("Starting login launcher")
|
|
browser_manager.open_for_login(urls=urls)
|
|
logger.info("Login launcher finished — profile saved")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|