123 lines
3.9 KiB
Python
123 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Look up Steam prices via the IsThereAnyDeal (ITAD) API using Steam app IDs.
|
|
|
|
Usage:
|
|
export ITAD_API_KEY=your_key_here
|
|
python3 itad.py 3330
|
|
python3 itad.py 730 1245620 -c DK
|
|
python3 itad.py 730 --json
|
|
|
|
API key: https://isthereanydeal.com/apps/my/
|
|
"""
|
|
import argparse
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
import requests
|
|
|
|
BASE = "https://api.isthereanydeal.com"
|
|
STEAM_SHOP_ID = 61 # Steam's shop ID on ITAD
|
|
|
|
session = requests.Session()
|
|
session.headers.update({
|
|
"User-Agent": "itad-price-script/1.0 (+python-requests)",
|
|
"Accept": "application/json",
|
|
})
|
|
|
|
|
|
def api(method: str, path: str, key: str, params: dict | None = None, body=None):
|
|
"""Call the ITAD API; on failure print the status and response body, then exit."""
|
|
params = {"key": key, **(params or {})}
|
|
try:
|
|
r = session.request(method, f"{BASE}{path}", params=params, json=body, timeout=20)
|
|
except requests.RequestException as e:
|
|
sys.exit(f"Network error: {e}")
|
|
|
|
if not r.ok:
|
|
text = r.text.strip().replace(key, "<key>")
|
|
sys.exit(
|
|
f"HTTP {r.status_code} from {path}\n{text[:500]}\n"
|
|
"Hints: 403 = bad/inactive API key or blocked request; "
|
|
"401 = key missing; 429 = rate limited."
|
|
)
|
|
return r.json()
|
|
|
|
|
|
def lookup_game(key: str, appid: int) -> dict | None:
|
|
"""Steam app ID -> ITAD game record."""
|
|
data = api("GET", "/games/lookup/v1", key, {"appid": appid})
|
|
return data["game"] if data.get("found") else None
|
|
|
|
|
|
def get_steam_prices(key: str, itad_ids: list[str], country: str) -> dict:
|
|
"""ITAD game IDs -> {id: current Steam deal or None}."""
|
|
data = api(
|
|
"POST", "/games/prices/v3", key,
|
|
{"country": country, "shops": STEAM_SHOP_ID},
|
|
body=itad_ids,
|
|
)
|
|
return {e["id"]: (e.get("deals") or [None])[0] for e in data}
|
|
|
|
|
|
def main() -> int:
|
|
p = argparse.ArgumentParser(description="Steam price lookup via IsThereAnyDeal")
|
|
p.add_argument("appids", nargs="+", type=int, help="Steam app ID(s)")
|
|
p.add_argument("-c", "--country", default="US", help="2-letter country code (default: US)")
|
|
p.add_argument("-k", "--key", default=os.environ.get("ITAD_API_KEY"),
|
|
help="ITAD API key (or set ITAD_API_KEY)")
|
|
p.add_argument("--json", action="store_true", help="Output JSON")
|
|
args = p.parse_args()
|
|
|
|
if not args.key:
|
|
print("Missing API key. Set ITAD_API_KEY or pass --key.", file=sys.stderr)
|
|
return 1
|
|
|
|
games = {}
|
|
for appid in args.appids:
|
|
game = lookup_game(args.key, appid)
|
|
if game:
|
|
games[appid] = game
|
|
else:
|
|
print(f"[{appid}] not found on ITAD", file=sys.stderr)
|
|
if not games:
|
|
return 1
|
|
|
|
prices = get_steam_prices(args.key, [g["id"] for g in games.values()], args.country)
|
|
|
|
results = []
|
|
for appid, game in games.items():
|
|
deal = prices.get(game["id"])
|
|
if not deal:
|
|
results.append({"appid": appid, "title": game["title"], "available": False})
|
|
continue
|
|
results.append({
|
|
"appid": appid,
|
|
"title": game["title"],
|
|
"available": True,
|
|
"price": deal["price"]["amount"],
|
|
"list_price": deal["regular"]["amount"],
|
|
"discount_percent": deal["cut"],
|
|
"currency": deal["price"]["currency"],
|
|
"url": deal.get("url"),
|
|
})
|
|
|
|
if args.json:
|
|
print(json.dumps(results, indent=2))
|
|
return 0
|
|
|
|
for r in results:
|
|
print(f"{r['title']} (app {r['appid']})")
|
|
if not r["available"]:
|
|
print(" No Steam price available for this region\n")
|
|
continue
|
|
cur = r["currency"]
|
|
print(f" Price: {r['price']:.2f} {cur}")
|
|
print(f" List price: {r['list_price']:.2f} {cur}")
|
|
print(f" Discount: {r['discount_percent']}%\n")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |