Simplify Costco browser header extraction

This commit is contained in:
ben
2026-03-16 16:23:38 -04:00
parent 5a331c9af4
commit 1b4c7dde25
5 changed files with 199 additions and 141 deletions

View File

@@ -225,27 +225,8 @@ def build_headers(auth_headers):
headers.update(auth_headers)
return headers
def build_session():
retailer_session = load_costco_session()
click.echo(
"session bootstrap: "
f"cookies={bool(retailer_session.cookies)}, "
f"authorization={'costco-x-authorization' in retailer_session.headers}, "
f"client_id={'costco-x-wcs-clientId' in retailer_session.headers}, "
f"client_identifier={'client-identifier' in retailer_session.headers}"
)
auth = retailer_session.headers.get("costco-x-authorization", "")
if auth:
click.echo(
f"auth prefix ok={auth.startswith('Bearer ')} len={len(auth)} token_prefix={auth[:24]}"
)
click.echo(
"header values: "
f"client_id={retailer_session.headers.get('costco-x-wcs-clientId', '')} "
f"client_identifier={retailer_session.headers.get('client-identifier', '')}"
)
def build_session(profile_dir=None):
retailer_session = load_costco_session(profile_dir=profile_dir)
session = requests.Session()
session.cookies.update(retailer_session.cookies)
session.headers.update(build_headers(retailer_session.headers))
@@ -597,15 +578,38 @@ def write_csv(path, rows, fieldnames):
type=int,
help="How many months of receipts to enumerate back from today.",
)
def main(outdir, document_type, document_sub_type, window_days, months_back):
@click.option(
"--firefox-profile-dir",
default=None,
help="Firefox profile directory to use for cookies and session storage.",
)
def main(
outdir,
document_type,
document_sub_type,
window_days,
months_back,
firefox_profile_dir,
):
outdir = Path(outdir)
raw_dir = outdir / "raw"
raw_dir = outdir / "raw"
try:
session = build_session()
session = build_session(profile_dir=firefox_profile_dir)
except Exception as exc:
raise click.ClickException(
f"failed to load Costco browser session: {exc}"
) from exc
if firefox_profile_dir:
raise click.ClickException(
f"failed to load Costco browser session: {exc}"
) from exc
prompted_profile = click.prompt(
"Firefox profile dir",
type=click.Path(exists=True, file_okay=False, path_type=Path),
)
try:
session = build_session(profile_dir=prompted_profile)
except Exception as prompt_exc:
raise click.ClickException(
f"failed to load Costco browser session: {prompt_exc}"
) from prompt_exc
start_date, end_date = resolve_date_range(months_back)
summary_payload, request_metadata = fetch_summary_windows(