from __future__ import annotations import json import sys from datetime import datetime from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1])) import scripts.refresh_hanmac_wehago_ledgers as refresh import scripts.retry_failed_wehago_accounts_direct as direct import scripts.wehago_data_download_2022_work as wehago import scripts.wehago_ledger_api_download as api_download def main() -> int: base = Path(r"\\wsl.localhost\Ubuntu\home\b17301\WEHAGO_DB\data_download\hanmac_refresh") canonical = Path(r"\\wsl.localhost\Ubuntu\home\b17301\WEHAGO_DB\data_download\hanmac\2025") run_root = base / f"retry_2025_all_direct_{datetime.now():%Y%m%d_%H%M%S}" staging = run_root / "staging" / "2025" staging.mkdir(parents=True, exist_ok=True) wehago.CHROME_DEBUGGER_ADDRESS = "127.0.0.1:9225" wehago.DOWNLOAD_DIR = run_root driver = wehago.build_driver(run_root) try: cookies = direct.cookie_map(driver) finally: driver.quit() accounts = wehago.discover_downloaded_accounts(canonical) manifest: list[dict[str, object]] = [] failures: list[dict[str, str]] = [] print(f"2025 all accounts {len(accounts)} staging {staging}", flush=True) for index, account in enumerate(accounts, start=1): try: rows = direct.fetch_ledger_rows(2025, account, cookies) api_download.validate_api_rows(account, rows) api_download.write_api_rows(staging / account.safe_filename, account, rows) manifest.append( { "account_code": account.code, "account_name": account.name, "api_request_code": f"{account.code}00", "api_response_rows": len(rows), } ) print(f"[{index}/{len(accounts)}] OK {account.code} rows={len(rows)}", flush=True) except Exception as exc: reason = f"{type(exc).__name__}: {exc}" failures.append({"account_code": account.code, "account_name": account.name, "reason": reason}) print(f"[{index}/{len(accounts)}] FAIL {account.code}: {reason}", flush=True) api_download.write_progress( staging, { "status": "completed_with_failures" if failures else "completed", "completed": len(manifest), "total": len(accounts), "accounts": manifest, "failures": failures, }, ) validation = refresh.validate_staging(staging) summary = { "year": 2025, "requested": len(accounts), "downloaded": len(manifest), "failures": len(failures), "failed_codes": [item["account_code"] for item in failures], "staging": str(staging), "validation": validation, } (run_root / "retry_summary.json").write_text( json.dumps(summary, ensure_ascii=False, indent=2), encoding="utf-8", ) print(f"SUMMARY_PATH {run_root / 'retry_summary.json'}", flush=True) print( f"VALIDATION_PASSED {validation.get('passed')} DUP_GROUPS {len(validation.get('duplicate_contents', []))}", flush=True, ) return 0 if not failures else 1 if __name__ == "__main__": raise SystemExit(main())