-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
50 lines (38 loc) · 1.52 KB
/
main.py
File metadata and controls
50 lines (38 loc) · 1.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
"""retailerapi quickstart — Python 3.10+
Look up a product, print title + cheapest cross-retailer offer.
Run: RETAILERAPI_KEY=rk_live_... python main.py [identifier]
"""
import os
import sys
import requests
KEY = os.environ.get("RETAILERAPI_KEY")
if not KEY:
print("RETAILERAPI_KEY env var required. Get one at https://app.retailerapi.com/signup")
sys.exit(1)
identifier = sys.argv[1] if len(sys.argv) > 1 else "19667262713"
def main() -> None:
r = requests.get(
f"https://api.retailerapi.com/v1/products/{identifier}",
params={"include_cross_retailer": "true"},
headers={"Authorization": f"Bearer {KEY}"},
timeout=15,
)
r.raise_for_status()
p = r.json()
print(p.get("title", "—"))
print(f" brand: {p.get('brand') or '—'}")
price = p.get("current_price")
price_str = f"${price:.2f}" if isinstance(price, (int, float)) else "—"
print(f" walmart: {price_str} ({p.get('walmart_url') or '—'})")
others = [
c for c in (p.get("cross_retailer") or [])
if c.get("status") == "ok" and isinstance(c.get("price"), (int, float))
]
if others:
cheapest = min(others, key=lambda c: c["price"])
print(f" cheapest off-Walmart: ${cheapest['price']:.2f} at {cheapest['retailer']}")
else:
print(" cross-retailer: indexing — call again in a few seconds")
print(f"\ntokens_consumed: {p.get('tokens_consumed')} · tokens_remaining: {p.get('tokens_remaining')}")
if __name__ == "__main__":
main()