from dataclasses import dataclass from browser_session import ( find_json_storage_value, find_storage_value, list_storage_keys, load_browser_context, ) COSTCO_STORAGE_ORIGINS = ["costco.com"] COSTCO_HEADER_FIELDS = [ ("costco-x-authorization", "costco-x-authorization"), ("costco-x-wcs-clientId", "costco-x-wcs-clientId"), ("client-identifier", "client-identifier"), ] COSTCO_JSON_HEADER_KEYS = ["headers", "costco.headers"] @dataclass class RetailerSession: cookies: object headers: dict[str, str] def load_giant_session(browser="firefox", profile_dir=None): context = load_browser_context( browser=browser, domain_name="giantfood.com", storage_origins=["giantfood.com"], profile_dir=profile_dir, ) return RetailerSession(cookies=context.cookies, headers={}) def load_costco_session(browser="firefox", profile_dir=None): context = load_browser_context( browser=browser, domain_name=".costco.com", storage_origins=COSTCO_STORAGE_ORIGINS, profile_dir=profile_dir, ) headers = extract_costco_headers(context.storage_entries) missing = [ header_name for header_name, value in headers.items() if not value ] if missing: available_keys = ", ".join( list_storage_keys(context.storage_entries, COSTCO_STORAGE_ORIGINS) ) raise ValueError( "missing Costco browser session headers: " f"{', '.join(missing)}. " f"Available Costco storage keys: {available_keys or '(none)'}" ) return RetailerSession(cookies=context.cookies, headers=headers) def extract_costco_headers(storage_entries): headers = {} for header_name, storage_key in COSTCO_HEADER_FIELDS: value = find_storage_value( storage_entries, COSTCO_STORAGE_ORIGINS, storage_key, ) if not value: value = find_costco_header_in_json(storage_entries, header_name) headers[header_name] = value return headers def find_costco_header_in_json(storage_entries, header_name): for json_key in COSTCO_JSON_HEADER_KEYS: value = find_json_storage_value( storage_entries, COSTCO_STORAGE_ORIGINS, json_key, header_name, ) if value: return value return ""