(argv: list[str])
| 194 | |
| 195 | |
| 196 | def main(argv: list[str]) -> int: |
| 197 | args = parse_args(argv) |
| 198 | profile = SITE_PROFILES[args.site] |
| 199 | output_path = Path(args.output).resolve() if args.output else default_storage_path(args.site) |
| 200 | output_path.parent.mkdir(parents=True, exist_ok=True) |
| 201 | secret_name = f"{args.site.upper()}_STORAGE_STATE" |
| 202 | |
| 203 | if args.from_firefox_profile is not None: |
| 204 | try: |
| 205 | cookies_db = _locate_firefox_profile(args.from_firefox_profile) |
| 206 | except RuntimeError as err: |
| 207 | print(f"Error: {err}", file=sys.stderr) |
| 208 | return 1 |
| 209 | print(f"Reading {args.site} cookies from Firefox profile: {cookies_db}") |
| 210 | state = _firefox_storage_state(cookies_db, profile["cookie_host_glob"]) |
| 211 | if not profile["is_logged_in"](state["cookies"]): |
| 212 | print(f"Error: this Firefox profile does not appear to be logged in to {args.site}.", |
| 213 | file=sys.stderr) |
| 214 | return 1 |
| 215 | output_path.write_text(json.dumps(state), encoding="utf-8") |
| 216 | print(f"Wrote storage state: {output_path}") |
| 217 | print(" (local syndication runs discover this file automatically — no env var needed)") |
| 218 | print(f" cookies captured: {len(state['cookies'])}") |
| 219 | if not args.no_base64: |
| 220 | encoded = base64.b64encode(output_path.read_bytes()).decode("ascii") |
| 221 | print() |
| 222 | print(f"For CI only — paste the following as the {secret_name} repository secret:") |
| 223 | print("-" * 72) |
| 224 | print(encoded) |
| 225 | print("-" * 72) |
| 226 | return 0 |
| 227 | |
| 228 | try: |
| 229 | from playwright.sync_api import sync_playwright |
| 230 | except ImportError: |
| 231 | print("Playwright is not installed. In a venv, run: pip install playwright && playwright install chromium", |
| 232 | file=sys.stderr) |
| 233 | return 1 |
| 234 | |
| 235 | with sync_playwright() as pw: |
| 236 | launch_kwargs: dict = {"headless": False} |
| 237 | # The args namespace renamed channel to browser to allow Firefox. |
| 238 | browser_choice = args.browser |
| 239 | if browser_choice == "firefox": |
| 240 | try: |
| 241 | browser = pw.firefox.launch(headless=False) |
| 242 | except Exception as err: # noqa: BLE001 |
| 243 | print(f"Could not launch Playwright Firefox ({err}). " |
| 244 | "Run `playwright install firefox` and retry.", file=sys.stderr) |
| 245 | return 1 |
| 246 | else: |
| 247 | if browser_choice and browser_choice != "chromium": |
| 248 | launch_kwargs["channel"] = browser_choice |
| 249 | try: |
| 250 | browser = pw.chromium.launch(**launch_kwargs) |
| 251 | except Exception as err: # noqa: BLE001 — channel may not be installed |
| 252 | print(f"Could not launch with browser='{browser_choice}' ({err}); falling back to bundled Chromium.", |
| 253 | file=sys.stderr) |
no test coverage detected