Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import httpx
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
API_KEY = os.environ.get("TCGLOOKUP_API_KEY", "")
|
| 6 |
+
BASE_URL = "https://api.tcgpricelookup.com/v1"
|
| 7 |
+
|
| 8 |
+
GAMES = {
|
| 9 |
+
"All games": "",
|
| 10 |
+
"Pokémon": "pokemon",
|
| 11 |
+
"Magic: The Gathering": "mtg",
|
| 12 |
+
"Yu-Gi-Oh!": "yugioh",
|
| 13 |
+
"Disney Lorcana": "lorcana",
|
| 14 |
+
"One Piece": "onepiece",
|
| 15 |
+
"Star Wars: Unlimited": "swu",
|
| 16 |
+
"Flesh and Blood": "fab",
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
+
def search_cards(query: str, game: str, limit: int):
|
| 20 |
+
if not query.strip():
|
| 21 |
+
return "Enter a card name to search."
|
| 22 |
+
|
| 23 |
+
params = {"q": query, "limit": min(limit, 20)}
|
| 24 |
+
game_slug = GAMES.get(game, "")
|
| 25 |
+
if game_slug:
|
| 26 |
+
params["game"] = game_slug
|
| 27 |
+
|
| 28 |
+
headers = {"Accept": "application/json"}
|
| 29 |
+
if API_KEY:
|
| 30 |
+
headers["X-API-Key"] = API_KEY
|
| 31 |
+
|
| 32 |
+
try:
|
| 33 |
+
r = httpx.get(f"{BASE_URL}/cards/search", params=params, headers=headers, timeout=30)
|
| 34 |
+
r.raise_for_status()
|
| 35 |
+
data = r.json()
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"API error: {e}"
|
| 38 |
+
|
| 39 |
+
cards = data.get("data", [])
|
| 40 |
+
total = data.get("total", 0)
|
| 41 |
+
|
| 42 |
+
if not cards:
|
| 43 |
+
return f"No results for \"{query}\". Try a different name or remove the game filter."
|
| 44 |
+
|
| 45 |
+
lines = [f"**Found {total} matches.** Showing top {len(cards)}:\n"]
|
| 46 |
+
for i, card in enumerate(cards, 1):
|
| 47 |
+
name = card.get("name", "?")
|
| 48 |
+
set_name = card.get("set", {}).get("name", "?")
|
| 49 |
+
game_name = card.get("game", {}).get("name", "?")
|
| 50 |
+
number = card.get("number", "")
|
| 51 |
+
rarity = card.get("rarity", "")
|
| 52 |
+
|
| 53 |
+
# Price
|
| 54 |
+
raw = card.get("prices", {}).get("raw", {})
|
| 55 |
+
nm = raw.get("near_mint", {}).get("tcgplayer", {})
|
| 56 |
+
market = nm.get("market")
|
| 57 |
+
price_str = f"${market:.2f}" if market else "—"
|
| 58 |
+
|
| 59 |
+
num_str = f" #{number}" if number else ""
|
| 60 |
+
rar_str = f" · {rarity}" if rarity else ""
|
| 61 |
+
|
| 62 |
+
lines.append(f"**{i}.** {name}{num_str} — {set_name} ({game_name}){rar_str}")
|
| 63 |
+
lines.append(f" NM TCGPlayer market: **{price_str}**")
|
| 64 |
+
lines.append("")
|
| 65 |
+
|
| 66 |
+
lines.append("---")
|
| 67 |
+
lines.append("*Data from [TCG Price Lookup API](https://tcgpricelookup.com/tcg-api). Get a free API key at [tcgpricelookup.com/tcg-api](https://tcgpricelookup.com/tcg-api).*")
|
| 68 |
+
return "\n".join(lines)
|
| 69 |
+
|
| 70 |
+
demo = gr.Interface(
|
| 71 |
+
fn=search_cards,
|
| 72 |
+
inputs=[
|
| 73 |
+
gr.Textbox(label="Card name", placeholder="e.g. Charizard, Black Lotus, Blue-Eyes White Dragon"),
|
| 74 |
+
gr.Dropdown(choices=list(GAMES.keys()), value="All games", label="Game filter"),
|
| 75 |
+
gr.Slider(minimum=1, maximum=20, value=5, step=1, label="Max results"),
|
| 76 |
+
],
|
| 77 |
+
outputs=gr.Markdown(label="Results"),
|
| 78 |
+
title="TCG Card Price Search",
|
| 79 |
+
description="""
|
| 80 |
+
Search live trading card prices across **8 major TCGs**: Pokémon, Magic: The Gathering, Yu-Gi-Oh!, Disney Lorcana, One Piece, Star Wars: Unlimited, and Flesh and Blood.
|
| 81 |
+
|
| 82 |
+
Powered by the [TCG Price Lookup API](https://tcgpricelookup.com/tcg-api). Get a free API key at [tcgpricelookup.com/tcg-api](https://tcgpricelookup.com/tcg-api).
|
| 83 |
+
|
| 84 |
+
Official SDKs: [JavaScript](https://github.com/TCG-Price-Lookup/tcglookup-js) · [Python](https://github.com/TCG-Price-Lookup/tcglookup-py) · [Go](https://github.com/TCG-Price-Lookup/tcglookup-go) · [Rust](https://github.com/TCG-Price-Lookup/tcglookup-rs) · [PHP](https://github.com/TCG-Price-Lookup/tcglookup-php)
|
| 85 |
+
""",
|
| 86 |
+
examples=[
|
| 87 |
+
["Charizard", "Pokémon", 5],
|
| 88 |
+
["Black Lotus", "Magic: The Gathering", 3],
|
| 89 |
+
["Blue-Eyes White Dragon", "Yu-Gi-Oh!", 5],
|
| 90 |
+
["Elsa Spirit of Winter", "Disney Lorcana", 3],
|
| 91 |
+
["Monkey D. Luffy", "One Piece", 5],
|
| 92 |
+
],
|
| 93 |
+
theme="default",
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
demo.launch()
|