-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.py
More file actions
69 lines (57 loc) · 2.13 KB
/
Copy pathsearch.py
File metadata and controls
69 lines (57 loc) · 2.13 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""Python example for the TCG Price Lookup API.
Usage:
pip install tcglookup
TCGLOOKUP_API_KEY=tlk_live_... python search.py
Get a free API key at https://tcgpricelookup.com/tcg-api
"""
import os
import sys
from tcglookup import (
PlanAccessError,
RateLimitError,
TcgLookupClient,
)
def main() -> int:
api_key = os.environ.get("TCGLOOKUP_API_KEY")
if not api_key:
print("Set TCGLOOKUP_API_KEY in your environment.", file=sys.stderr)
return 1
with TcgLookupClient(api_key=api_key) as client:
# Search for cards by name + game.
results = client.cards.search(q="charizard", game="pokemon", limit=5)
cards = results["data"]
print(f"Found {results['total']} matches. Showing first {len(cards)}:\n")
for card in cards:
market = (
card["prices"]["raw"]
.get("near_mint", {})
.get("tcgplayer", {})
.get("market")
)
price = f"${market:.2f}" if market is not None else "—"
print(f" {card['name']:<30} {card['set']['name']:<28} {price}")
# Fetch a single card by its UUID and inspect every condition tier.
if cards:
print("\nFull price block for the top hit:")
top = client.cards.get(cards[0]["id"])
for condition, sources in top["prices"]["raw"].items():
market = (sources.get("tcgplayer") or {}).get("market")
price = f"${market:.2f}" if market is not None else "—"
print(f" {condition:<20} {price}")
# Last call's rate-limit headers are exposed for monitoring.
print(
f"\nRate limit: {client.rate_limit.remaining}/{client.rate_limit.limit}"
)
return 0
if __name__ == "__main__":
try:
sys.exit(main())
except PlanAccessError:
print(
"This endpoint requires Trader plan — upgrade at tcgpricelookup.com/tcg-api",
file=sys.stderr,
)
sys.exit(1)
except RateLimitError:
print("Rate limit exceeded. Wait or upgrade plan.", file=sys.stderr)
sys.exit(1)