Harden giant receipt fetch CLI

This commit is contained in:
ben
2026-03-14 18:32:32 -04:00
parent 585d8c1e49
commit d57b9cf52f
8 changed files with 456 additions and 470 deletions

View File

@@ -1,66 +1,17 @@
import requests
from playwright.sync_api import sync_playwright
BASE = "https://giantfood.com"
ACCOUNT_PAGE = f"{BASE}/account/history/invoice/in-store"
USER_ID = "369513017"
LOYALTY = "440155630880"
import unittest
def get_session():
with sync_playwright() as p:
browser = p.firefox.launch(headless=False)
page = browser.new_page()
page.goto(ACCOUNT_PAGE)
print("log in manually in the browser, then press ENTER here")
input()
cookies = page.context.cookies()
ua = page.evaluate("() => navigator.userAgent")
browser.close()
s = requests.Session()
s.headers.update({
"user-agent": ua,
"accept": "application/json, text/plain, */*",
"referer": ACCOUNT_PAGE,
})
for c in cookies:
domain = c.get("domain", "").lstrip(".") or "giantfood.com"
s.cookies.set(c["name"], c["value"], domain=domain)
return s
try:
from playwright.sync_api import sync_playwright # noqa: F401
import requests # noqa: F401
except ImportError as exc: # pragma: no cover - dependency-gated smoke test
sync_playwright = None
_IMPORT_ERROR = exc
else:
_IMPORT_ERROR = None
def test_history(session):
url = f"{BASE}/api/v6.0/user/{USER_ID}/order/history"
r = session.get(
url,
params={
"filter": "instore",
"loyaltyNumber": LOYALTY,
},
)
print("status:", r.status_code)
print()
data = r.json()
print("orders found:", len(data.get("records", [])))
print()
for rec in data.get("records", [])[:5]:
print(rec["orderId"], rec["orderDate"], rec["orderTotal"])
if __name__ == "__main__":
session = get_session()
test_history(session)
@unittest.skipIf(sync_playwright is None, f"optional smoke test dependency missing: {_IMPORT_ERROR}")
class GiantLoginSmokeTest(unittest.TestCase):
def test_dependencies_available(self):
self.assertIsNotNone(sync_playwright)