Files
EFTCOMPANION/top_spreads.py
T
2026-06-25 21:26:53 +00:00

83 lines
2.5 KiB
Python

# top_spreads.py
import requests
import csv
ENDPOINT = "https://api.tarkov.dev/graphql"
QUERY = """
query ItemsWithPrices {
items(lang: en) {
id
name
shortName
basePrice
lastLowPrice
low24hPrice
high24hPrice
avg24hPrice
lastOfferCount
buyFor { source currency price priceRUB vendor { name } }
sellFor { source currency price priceRUB vendor { name } }
}
}
"""
def pick_rub(p):
pr = p.get("priceRUB")
if pr is not None:
return pr
if p.get("currency") == "RUB":
return p.get("price")
return None
def best_trader_buy_rub(item):
offers = [pick_rub(o) for o in (item.get("buyFor") or []) if o.get("source") != "Flea Market"]
offers = [x for x in offers if isinstance(x, (int, float))]
return min(offers) if offers else None
def best_trader_sell_rub(item):
offers = [pick_rub(o) for o in (item.get("sellFor") or []) if o.get("source") != "Flea Market"]
offers = [x for x in offers if isinstance(x, (int, float))]
return max(offers) if offers else None
resp = requests.post(ENDPOINT, json={"query": QUERY}, timeout=60)
resp.raise_for_status()
data = resp.json()["data"]["items"]
rows = []
for it in data:
flea_low = it.get("low24hPrice") or it.get("lastLowPrice")
flea_high = it.get("high24hPrice") or it.get("avg24hPrice")
if isinstance(flea_low, (int, float)) and isinstance(flea_high, (int, float)):
gap = flea_high - flea_low
if gap > 0:
rows.append({
"id": it["id"],
"name": it["name"],
"shortName": it["shortName"],
"gap": gap,
"fleaLow": flea_low,
"fleaHigh": flea_high,
"avg24h": it.get("avg24hPrice"),
"bestTraderBuy": best_trader_buy_rub(it),
"bestTraderSell": best_trader_sell_rub(it),
"lastOfferCount": it.get("lastOfferCount"),
})
rows.sort(key=lambda r: r["gap"], reverse=True)
top = rows[:100]
# Print a quick top list
for i, r in enumerate(top, 1):
print(f"{i:>2}. {r['name']:<50} gap ₽{r['gap']:,} (low {r['fleaLow']:,} → high {r['fleaHigh']:,})")
# Save CSV
with open("top100_spread.csv", "w", newline="", encoding="utf-8") as f:
w = csv.DictWriter(f, fieldnames=[
"id","name","shortName","gap","fleaLow","fleaHigh","avg24h",
"bestTraderBuy","bestTraderSell","lastOfferCount"
])
w.writeheader()
w.writerows(top)
print("\nSaved: top100_spread.csv")