File size: 11,153 Bytes
b190b45 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
#!/usr/bin/env python3
"""
KuCoin API Client
کلاینت KuCoin با پشتیبانی Smart Access
"""
import httpx
import logging
from typing import Optional, Dict, List
from datetime import datetime
logger = logging.getLogger(__name__)
class KuCoinClient:
"""
KuCoin Exchange API Client
KuCoin یکی از صرافیهای محبوب که ممکنه در بعضی مناطق فیلتر باشه
از Smart Access برای دسترسی قابل اطمینان استفاده میکنه
"""
def __init__(self):
self.base_url = "https://api.kucoin.com"
self.futures_url = "https://api-futures.kucoin.com"
async def _make_request(
self,
url: str,
params: Optional[Dict] = None,
use_rotating_access: bool = True
) -> Optional[Dict]:
"""
ارسال درخواست به KuCoin با Rotating DNS/Proxy
Args:
url: آدرس API
params: پارامترهای درخواست
use_rotating_access: استفاده از Rotating Access (DNS/Proxy چرخشی)
"""
try:
if use_rotating_access:
# استفاده از Rotating Access برای امنیت و دسترسی همیشگی
from backend.services.rotating_access_manager import rotating_access_manager
logger.info(f"🔐 KuCoin request with ROTATING Access: {url}")
response = await rotating_access_manager.secure_fetch(
url,
params=params,
use_rotating_dns=True,
use_rotating_proxy=True
)
else:
# درخواست مستقیم (فقط برای تست)
logger.info(f"🔗 KuCoin direct request: {url}")
async with httpx.AsyncClient(timeout=10.0) as client:
response = await client.get(url, params=params)
if response and response.status_code == 200:
data = response.json()
# بررسی پاسخ KuCoin
if data.get("code") == "200000": # Success code
logger.info(f"✅ KuCoin request successful")
return data.get("data")
else:
logger.error(f"❌ KuCoin API error: {data.get('msg')}")
return None
else:
logger.error(f"❌ KuCoin request failed: {response.status_code if response else 'No response'}")
return None
except Exception as e:
logger.error(f"❌ KuCoin request exception: {e}")
return None
async def get_ticker(self, symbol: str = "BTC-USDT", use_rotating_access: bool = True) -> Optional[Dict]:
"""
دریافت قیمت فعلی یک ارز
Args:
symbol: نماد ارز (مثلاً BTC-USDT)
Returns:
{
"symbol": "BTC-USDT",
"price": "50000.5",
"changeRate": "0.0123",
"high": "51000",
"low": "49000",
...
}
"""
url = f"{self.base_url}/api/v1/market/stats"
params = {"symbol": symbol}
logger.info(f"📊 Getting KuCoin ticker for {symbol}")
data = await self._make_request(url, params, use_rotating_access=use_rotating_access)
if data:
return {
"symbol": data.get("symbol"),
"price": float(data.get("last", 0)),
"high_24h": float(data.get("high", 0)),
"low_24h": float(data.get("low", 0)),
"volume_24h": float(data.get("vol", 0)),
"change_24h": float(data.get("changeRate", 0)) * 100,
"timestamp": datetime.now().isoformat()
}
return None
async def get_all_tickers(self) -> Optional[List[Dict]]:
"""
دریافت قیمت همه ارزها
Returns:
[
{"symbol": "BTC-USDT", "price": 50000, ...},
{"symbol": "ETH-USDT", "price": 3000, ...},
...
]
"""
url = f"{self.base_url}/api/v1/market/allTickers"
logger.info(f"📊 Getting all KuCoin tickers")
data = await self._make_request(url, use_smart_access=True)
if data and "ticker" in data:
tickers = []
for ticker in data["ticker"][:50]: # محدود به 50 تا
tickers.append({
"symbol": ticker.get("symbol"),
"price": float(ticker.get("last", 0)),
"volume_24h": float(ticker.get("vol", 0)),
"change_24h": float(ticker.get("changeRate", 0)) * 100
})
return tickers
return None
async def get_orderbook(self, symbol: str = "BTC-USDT", depth: int = 20) -> Optional[Dict]:
"""
دریافت Order Book (لیست سفارشات)
Args:
symbol: نماد ارز
depth: عمق order book (20 یا 100)
Returns:
{
"bids": [[price, size], ...],
"asks": [[price, size], ...],
"timestamp": ...
}
"""
url = f"{self.base_url}/api/v1/market/orderbook/level2_{depth}"
params = {"symbol": symbol}
logger.info(f"📖 Getting KuCoin orderbook for {symbol}")
data = await self._make_request(url, params, use_smart_access=True)
if data:
return {
"symbol": symbol,
"bids": [[float(p), float(s)] for p, s in data.get("bids", [])[:10]],
"asks": [[float(p), float(s)] for p, s in data.get("asks", [])[:10]],
"timestamp": data.get("time")
}
return None
async def get_24h_stats(self, symbol: str = "BTC-USDT", use_rotating_access: bool = True) -> Optional[Dict]:
"""
دریافت آمار 24 ساعته
Returns:
{
"symbol": "BTC-USDT",
"high": 51000,
"low": 49000,
"vol": 12345,
"last": 50000,
"changeRate": 0.0123
}
"""
url = f"{self.base_url}/api/v1/market/stats"
params = {"symbol": symbol}
data = await self._make_request(url, params, use_rotating_access=use_rotating_access)
if data:
return {
"symbol": data.get("symbol"),
"high_24h": float(data.get("high", 0)),
"low_24h": float(data.get("low", 0)),
"volume_24h": float(data.get("vol", 0)),
"price": float(data.get("last", 0)),
"change_rate": float(data.get("changeRate", 0)),
"change_price": float(data.get("changePrice", 0))
}
return None
async def get_klines(
self,
symbol: str = "BTC-USDT",
interval: str = "1hour",
start_time: Optional[int] = None,
end_time: Optional[int] = None
) -> Optional[List[Dict]]:
"""
دریافت کندلها (OHLCV)
Args:
symbol: نماد ارز
interval: بازه زمانی (1min, 5min, 15min, 30min, 1hour, 4hour, 1day, 1week)
start_time: زمان شروع (timestamp)
end_time: زمان پایان (timestamp)
Returns:
[
{
"time": timestamp,
"open": 50000,
"high": 51000,
"low": 49000,
"close": 50500,
"volume": 12345
},
...
]
"""
url = f"{self.base_url}/api/v1/market/candles"
params = {
"symbol": symbol,
"type": interval
}
if start_time:
params["startAt"] = start_time
if end_time:
params["endAt"] = end_time
logger.info(f"📈 Getting KuCoin klines for {symbol} ({interval})")
data = await self._make_request(url, params, use_smart_access=True)
if data:
klines = []
for candle in data:
# KuCoin format: [timestamp, open, close, high, low, volume, turnover]
klines.append({
"timestamp": int(candle[0]),
"open": float(candle[1]),
"close": float(candle[2]),
"high": float(candle[3]),
"low": float(candle[4]),
"volume": float(candle[5])
})
return klines
return None
async def get_currencies(self) -> Optional[List[Dict]]:
"""
دریافت لیست همه ارزها
Returns:
[
{
"currency": "BTC",
"name": "Bitcoin",
"fullName": "Bitcoin",
"precision": 8
},
...
]
"""
url = f"{self.base_url}/api/v1/currencies"
logger.info(f"💰 Getting KuCoin currencies list")
data = await self._make_request(url, use_smart_access=True)
if data:
return [{
"currency": curr.get("currency"),
"name": curr.get("name"),
"full_name": curr.get("fullName"),
"precision": curr.get("precision")
} for curr in data[:100]] # محدود به 100 تا
return None
async def health_check(self, use_rotating_access: bool = True) -> bool:
"""
بررسی سلامت API
Returns:
True اگر API در دسترس باشه
"""
url = f"{self.base_url}/api/v1/status"
try:
data = await self._make_request(url, use_rotating_access=use_rotating_access)
if data:
status = data.get("status")
logger.info(f"💚 KuCoin health check: {status}")
return status == "open"
return False
except:
return False
# Global instance
kucoin_client = KuCoinClient()
__all__ = ["KuCoinClient", "kucoin_client"]
|