Download tools/polymarket.py from 1990two/XTOOLS: direct link, hf CLI and curl.
- Browser
- Download file 1.61 kB
-
https://huggingface.co/spaces/1990two/XTOOLS/resolve/main/tools/polymarket.py
- Command line
-
hf download hf://spaces/1990two/XTOOLS/tools/polymarket.py
-
curl -L -o polymarket.py https://huggingface.co/spaces/1990two/XTOOLS/resolve/main/tools/polymarket.py
1.61 kB
| """ | |
| Polymarket -- public Gamma API. No auth. | |
| GET https://gamma-api.polymarket.com/markets | |
| """ | |
| import httpx | |
| BASE = "https://gamma-api.polymarket.com/markets" | |
| def run(params: dict) -> dict: | |
| query_text = params.get("query") | |
| active = params.get("active", True) | |
| sort_by = params.get("sort_by", "volume_24hr") | |
| limit = int(params.get("limit", 50)) | |
| query = {"active": str(active).lower(), "limit": limit, "order": sort_by} | |
| if query_text: | |
| query["_q"] = query_text # fallback keyword param; Gamma also supports full text search endpoints | |
| resp = httpx.get(BASE, params=query, timeout=15.0) | |
| resp.raise_for_status() | |
| markets = resp.json() | |
| results = [] | |
| for m in markets: | |
| outcomes = m.get("outcomePrices") | |
| yes_price = no_price = None | |
| if outcomes: | |
| try: | |
| import json as _json | |
| prices = _json.loads(outcomes) if isinstance(outcomes, str) else outcomes | |
| if len(prices) >= 2: | |
| yes_price, no_price = float(prices[0]), float(prices[1]) | |
| except Exception: | |
| pass | |
| results.append( | |
| { | |
| "question": m.get("question"), | |
| "yes_price": yes_price, | |
| "no_price": no_price, | |
| "volume_24hr": m.get("volume24hr"), | |
| "liquidity": m.get("liquidity"), | |
| "end_date": m.get("endDate"), | |
| "url": f"https://polymarket.com/event/{m.get('slug')}" if m.get("slug") else None, | |
| } | |
| ) | |
| return {"count": len(results), "results": results} | |