#!/usr/bin/env python3
import urllib.request, json, os
from datetime import datetime, timezone

BASE = os.path.expanduser("~/Claude/Projects/에이전트구축")
OUT = os.path.join(BASE, "bybit_stocks.json")

url = "https://api.bybit.com/v5/market/instruments-info?category=linear&limit=1000"
with urllib.request.urlopen(url, timeout=30) as r:
    data = json.loads(r.read())

stocks = []
for x in data["result"]["list"]:
    if x.get("symbolType") == "stock":
        ticker = x["baseCoin"].replace("STOCK", "")
        launch = datetime.fromtimestamp(int(x["launchTime"])/1000, timezone.utc).strftime("%Y-%m-%d")
        stocks.append({
            "ticker": ticker,
            "symbol": x["symbol"],
            "launch": launch,
            "maxLev": x["leverageFilter"]["maxLeverage"]
        })

stocks.sort(key=lambda s: s["ticker"])
with open(OUT, "w") as f:
    json.dump({"updated": datetime.now(timezone.utc).isoformat(), "count": len(stocks), "stocks": stocks}, f, ensure_ascii=False, indent=1)

print(f"{len(stocks)}개 종목 저장 완료: {OUT}")
