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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
#!/usr/bin/env python3
"""
Hugging Face Unified Client
==================================
تمام درخواست‌ها از طریق این کلاینت به Hugging Face Space ارسال می‌شوند.
هیچ درخواست مستقیمی به API های خارجی ارسال نمی‌شود.

✅ تمام داده‌ها از Hugging Face
✅ بدون WebSocket (فقط HTTP)
✅ Cache و Retry مکانیزم
✅ Error Handling

References: crypto_resources_unified_2025-11-11.json
"""

import httpx
import asyncio
import logging
from typing import Dict, Any, List, Optional
from datetime import datetime, timedelta
import os
import hashlib
import json

logger = logging.getLogger(__name__)


class HuggingFaceUnifiedClient:
    """
    کلاینت یکپارچه برای تمام درخواست‌های به Hugging Face Space

    این کلاینت **تنها** منبع دریافت داده است و به جای API های دیگر،
    تمام داده‌ها را از Hugging Face Space دریافت می‌کند.
    """

    def __init__(self):
        """Initialize HuggingFace client with config"""
        self.base_url = os.getenv(
            "HF_SPACE_BASE_URL",
            "https://really-amin-datasourceforcryptocurrency.hf.space"
        )
        self.api_token = os.getenv("HF_API_TOKEN", "")
        self.timeout = httpx.Timeout(30.0, connect=10.0)

        # Request headers
        self.headers = {
            "Content-Type": "application/json",
            "User-Agent": "CryptoDataHub/1.0"
        }

        # Add auth token if available
        if self.api_token:
            self.headers["Authorization"] = f"Bearer {self.api_token}"

        # Cache configuration
        self.cache = {}
        self.cache_ttl = {
            "market": 30,      # 30 seconds
            "ohlcv": 60,       # 1 minute
            "news": 300,       # 5 minutes
            "sentiment": 0,    # No cache for sentiment
            "blockchain": 60,  # 1 minute
        }

        logger.info(f"🚀 HuggingFace Unified Client initialized")
        logger.info(f"   Base URL: {self.base_url}")
        logger.info(f"   Auth: {'✅ Token configured' if self.api_token else '❌ No token'}")

    def _get_cache_key(self, endpoint: str, params: Dict = None) -> str:
        """Generate cache key from endpoint and params"""
        cache_str = f"{endpoint}:{json.dumps(params or {}, sort_keys=True)}"
        return hashlib.md5(cache_str.encode()).hexdigest()

    def _get_cached(self, cache_key: str, cache_type: str) -> Optional[Dict]:
        """Get data from cache if available and not expired"""
        if cache_key not in self.cache:
            return None

        cached_data, cached_time = self.cache[cache_key]
        ttl = self.cache_ttl.get(cache_type, 0)

        if ttl == 0:
            # No caching
            return None

        age = (datetime.now() - cached_time).total_seconds()
        if age < ttl:
            logger.info(f"📦 Cache HIT: {cache_key} (age: {age:.1f}s)")
            return cached_data
        else:
            # Expired
            logger.info(f"⏰ Cache EXPIRED: {cache_key} (age: {age:.1f}s, ttl: {ttl}s)")
            del self.cache[cache_key]
            return None

    def _set_cache(self, cache_key: str, data: Dict, cache_type: str):
        """Store data in cache"""
        ttl = self.cache_ttl.get(cache_type, 0)
        if ttl > 0:
            self.cache[cache_key] = (data, datetime.now())
            logger.info(f"💾 Cache SET: {cache_key} (ttl: {ttl}s)")

    async def _request(
        self,
        method: str,
        endpoint: str,
        params: Optional[Dict] = None,
        json_body: Optional[Dict] = None,
        cache_type: Optional[str] = None,
        retry: int = 3
    ) -> Dict[str, Any]:
        """
        Make HTTP request to HuggingFace Space

        Args:
            method: HTTP method (GET, POST, etc.)
            endpoint: API endpoint (e.g., "/api/market")
            params: Query parameters
            json_body: JSON body for POST requests
            cache_type: Type of cache ("market", "ohlcv", etc.)
            retry: Number of retry attempts

        Returns:
            Response data as dict
        """
        # Check cache first (only for GET requests)
        if method.upper() == "GET" and cache_type:
            cache_key = self._get_cache_key(endpoint, params)
            cached = self._get_cached(cache_key, cache_type)
            if cached:
                return cached

        # Build full URL
        url = f"{self.base_url}{endpoint}"

        # Make request with retry
        last_error = None
        for attempt in range(retry):
            try:
                async with httpx.AsyncClient(timeout=self.timeout) as client:
                    if method.upper() == "GET":
                        response = await client.get(url, headers=self.headers, params=params)
                    elif method.upper() == "POST":
                        response = await client.post(url, headers=self.headers, json=json_body)
                    else:
                        raise ValueError(f"Unsupported HTTP method: {method}")

                    # Check status
                    response.raise_for_status()

                    # Parse JSON
                    data = response.json()

                    # Cache if applicable
                    if method.upper() == "GET" and cache_type:
                        cache_key = self._get_cache_key(endpoint, params)
                        self._set_cache(cache_key, data, cache_type)

                    logger.info(f"✅ HF Request: {method} {endpoint} (attempt {attempt + 1}/{retry})")
                    return data

            except httpx.HTTPStatusError as e:
                last_error = e
                logger.warning(f"❌ HF Request failed (attempt {attempt + 1}/{retry}): {e.response.status_code} - {e.response.text}")
                if attempt < retry - 1:
                    await asyncio.sleep(1 * (attempt + 1))  # Exponential backoff
            except Exception as e:
                last_error = e
                logger.error(f"❌ HF Request error (attempt {attempt + 1}/{retry}): {e}")
                if attempt < retry - 1:
                    await asyncio.sleep(1 * (attempt + 1))

        # All retries failed
        raise Exception(f"HuggingFace API request failed after {retry} attempts: {last_error}")

    # =========================================================================
    # Market Data Methods
    # =========================================================================

    async def get_market_prices(
        self,
        symbols: Optional[List[str]] = None,
        limit: int = 100
    ) -> Dict[str, Any]:
        """
        دریافت قیمت‌های بازار از HuggingFace

        Endpoint: GET /api/market

        Args:
            symbols: لیست سمبل‌ها (مثلاً ['BTC', 'ETH'])
            limit: تعداد نتایج

        Returns:
            {
                "success": True,
                "data": [
                    {
                        "symbol": "BTC",
                        "price": 50000.0,
                        "market_cap": 1000000000.0,
                        "volume_24h": 50000000.0,
                        "change_24h": 2.5,
                        "last_updated": 1234567890000
                    },
                    ...
                ],
                "source": "hf_engine",
                "timestamp": 1234567890000,
                "cached": False
            }
        """
        params = {"limit": limit}
        if symbols:
            params["symbols"] = ",".join(symbols)

        return await self._request(
            "GET",
            "/api/market",
            params=params,
            cache_type="market"
        )

    async def get_market_history(
        self,
        symbol: str,
        timeframe: str = "1h",
        limit: int = 1000
    ) -> Dict[str, Any]:
        """
        دریافت داده‌های تاریخی OHLCV از HuggingFace

        Endpoint: GET /api/market/history

        Args:
            symbol: سمبل (مثلاً "BTCUSDT")
            timeframe: بازه زمانی ("1m", "5m", "15m", "1h", "4h", "1d")
            limit: تعداد کندل‌ها

        Returns:
            {
                "success": True,
                "data": [
                    {
                        "timestamp": 1234567890000,
                        "open": 50000.0,
                        "high": 51000.0,
                        "low": 49500.0,
                        "close": 50500.0,
                        "volume": 1000000.0
                    },
                    ...
                ],
                "source": "hf_engine",
                "timestamp": 1234567890000
            }
        """
        params = {
            "symbol": symbol,
            "timeframe": timeframe,
            "limit": limit
        }

        return await self._request(
            "GET",
            "/api/market/history",
            params=params,
            cache_type="ohlcv"
        )

    # =========================================================================
    # Sentiment Analysis Methods
    # =========================================================================

    async def analyze_sentiment(self, text: str) -> Dict[str, Any]:
        """
        تحلیل احساسات متن با مدل‌های AI در HuggingFace

        Endpoint: POST /api/sentiment/analyze

        Args:
            text: متن برای تحلیل

        Returns:
            {
                "success": True,
                "data": {
                    "label": "positive",
                    "score": 0.95,
                    "sentiment": "positive",
                    "confidence": 0.95,
                    "text": "Bitcoin is...",
                    "timestamp": 1234567890000
                },
                "source": "hf_engine",
                "timestamp": 1234567890000
            }
        """
        json_body = {"text": text}

        return await self._request(
            "POST",
            "/api/sentiment/analyze",
            json_body=json_body,
            cache_type=None  # No cache for sentiment
        )

    # =========================================================================
    # News Methods (از HuggingFace Space)
    # =========================================================================

    async def get_news(
        self,
        limit: int = 20,
        source: Optional[str] = None
    ) -> Dict[str, Any]:
        """
        دریافت اخبار رمز ارز از HuggingFace

        Endpoint: GET /api/news

        Args:
            limit: تعداد خبر
            source: منبع خبر (اختیاری)

        Returns:
            {
                "articles": [
                    {
                        "id": "123",
                        "title": "Bitcoin reaches new high",
                        "url": "https://...",
                        "source": "CoinDesk",
                        "published_at": "2025-01-01T00:00:00"
                    },
                    ...
                ],
                "meta": {
                    "cache_ttl_seconds": 300,
                    "source": "hf"
                }
            }
        """
        params = {"limit": limit}
        if source:
            params["source"] = source

        return await self._request(
            "GET",
            "/api/news",
            params=params,
            cache_type="news"
        )

    # =========================================================================
    # Blockchain Explorer Methods (از HuggingFace Space)
    # =========================================================================

    async def get_blockchain_gas_prices(self, chain: str = "ethereum") -> Dict[str, Any]:
        """
        دریافت قیمت گس از HuggingFace

        Endpoint: GET /api/crypto/blockchain/gas

        Args:
            chain: نام بلاکچین (ethereum, bsc, polygon, etc.)

        Returns:
            {
                "chain": "ethereum",
                "gas_prices": {
                    "fast": 50.0,
                    "standard": 30.0,
                    "slow": 20.0,
                    "unit": "gwei"
                },
                "timestamp": "2025-01-01T00:00:00",
                "meta": {...}
            }
        """
        params = {"chain": chain}

        return await self._request(
            "GET",
            "/api/crypto/blockchain/gas",
            params=params,
            cache_type="blockchain"
        )

    async def get_blockchain_stats(
        self,
        chain: str = "ethereum",
        hours: int = 24
    ) -> Dict[str, Any]:
        """
        دریافت آمار بلاکچین از HuggingFace

        Endpoint: GET /api/crypto/blockchain/stats

        Args:
            chain: نام بلاکچین
            hours: بازه زمانی (ساعت)

        Returns:
            {
                "chain": "ethereum",
                "blocks_24h": 7000,
                "transactions_24h": 1200000,
                "avg_gas_price": 25.0,
                "mempool_size": 100000,
                "meta": {...}
            }
        """
        params = {"chain": chain, "hours": hours}

        return await self._request(
            "GET",
            "/api/crypto/blockchain/stats",
            params=params,
            cache_type="blockchain"
        )

    # =========================================================================
    # Whale Tracking Methods
    # =========================================================================

    async def get_whale_transactions(
        self,
        limit: int = 50,
        chain: Optional[str] = None,
        min_amount_usd: float = 100000
    ) -> Dict[str, Any]:
        """
        دریافت تراکنش‌های نهنگ‌ها از HuggingFace

        Endpoint: GET /api/crypto/whales/transactions
        """
        params = {
            "limit": limit,
            "min_amount_usd": min_amount_usd
        }
        if chain:
            params["chain"] = chain

        return await self._request(
            "GET",
            "/api/crypto/whales/transactions",
            params=params,
            cache_type="market"
        )

    async def get_whale_stats(self, hours: int = 24) -> Dict[str, Any]:
        """
        دریافت آمار نهنگ‌ها از HuggingFace

        Endpoint: GET /api/crypto/whales/stats
        """
        params = {"hours": hours}

        return await self._request(
            "GET",
            "/api/crypto/whales/stats",
            params=params,
            cache_type="market"
        )

    # =========================================================================
    # Health & Status Methods
    # =========================================================================

    async def health_check(self) -> Dict[str, Any]:
        """
        بررسی سلامت HuggingFace Space

        Endpoint: GET /api/health

        Returns:
            {
                "success": True,
                "status": "healthy",
                "timestamp": 1234567890000,
                "version": "1.0.0",
                "database": "connected",
                "cache": {
                    "market_data_count": 100,
                    "ohlc_count": 5000
                },
                "ai_models": {
                    "loaded": 3,
                    "failed": 0,
                    "total": 3
                },
                "source": "hf_engine"
            }
        """
        return await self._request(
            "GET",
            "/api/health",
            cache_type=None
        )

    async def get_system_status(self) -> Dict[str, Any]:
        """
        دریافت وضعیت کل سیستم

        Endpoint: GET /api/status
        """
        return await self._request(
            "GET",
            "/api/status",
            cache_type=None
        )


# Global singleton instance
_hf_client_instance = None


def get_hf_client() -> HuggingFaceUnifiedClient:
    """Get singleton instance of HuggingFace Unified Client"""
    global _hf_client_instance
    if _hf_client_instance is None:
        _hf_client_instance = HuggingFaceUnifiedClient()
    return _hf_client_instance