File size: 14,285 Bytes
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca2386d
 
 
 
 
 
 
 
8b7b267
ca2386d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7b267
 
ca2386d
8b7b267
ca2386d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7b267
 
 
 
ca2386d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b7b267
ca2386d
 
 
 
 
 
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ca2386d
 
 
 
 
8b7b267
 
 
 
ca2386d
 
 
 
 
 
 
 
 
 
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
ca2386d
 
 
 
 
8b7b267
ca2386d
 
 
 
 
 
 
 
 
 
8b7b267
ca2386d
 
 
 
 
 
8b7b267
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
HuggingFace Dataset Aggregator - Uses ALL Free HF Datasets
Maximizes usage of all available free HuggingFace datasets for historical OHLCV data
"""

import httpx
import logging
import io
import csv
from typing import Dict, Any, List, Optional
from datetime import datetime
from fastapi import HTTPException

logger = logging.getLogger(__name__)


class HFDatasetAggregator:
    """
    Aggregates historical OHLCV data from ALL free HuggingFace datasets:
    - linxy/CryptoCoin (26 symbols x 7 timeframes = 182 CSVs)
    - WinkingFace/CryptoLM-Bitcoin-BTC-USDT
    - WinkingFace/CryptoLM-Ethereum-ETH-USDT
    - WinkingFace/CryptoLM-Solana-SOL-USDT
    - WinkingFace/CryptoLM-Ripple-XRP-USDT
    """
    
    def __init__(self):
        self.timeout = 30.0
        
        # linxy/CryptoCoin dataset configuration
        self.linxy_base_url = "https://huggingface.co/datasets/linxy/CryptoCoin/resolve/main"
        self.linxy_symbols = [
            "BTC", "ETH", "BNB", "XRP", "ADA", "DOGE", "SOL", "TRX", "DOT", "MATIC",
            "LTC", "SHIB", "AVAX", "UNI", "LINK", "ATOM", "XLM", "ETC", "XMR", "BCH",
            "NEAR", "APT", "ARB", "OP", "FTM", "ALGO"
        ]
        self.linxy_timeframes = ["1m", "5m", "15m", "30m", "1h", "4h", "1d"]
        
        # WinkingFace datasets configuration
        self.winkingface_datasets = {
            "BTC": "https://huggingface.co/datasets/WinkingFace/CryptoLM-Bitcoin-BTC-USDT/resolve/main",
            "ETH": "https://huggingface.co/datasets/WinkingFace/CryptoLM-Ethereum-ETH-USDT/resolve/main",
            "SOL": "https://huggingface.co/datasets/WinkingFace/CryptoLM-Solana-SOL-USDT/resolve/main",
            "XRP": "https://huggingface.co/datasets/WinkingFace/CryptoLM-Ripple-XRP-USDT/resolve/main"
        }
        
        # Cache for dataset data
        self._cache = {}
        self._cache_duration = 3600  # 1 hour
    
    async def get_ohlcv(
        self,
        symbol: str,
        timeframe: str = "1h",
        limit: int = 1000
    ) -> List[Dict[str, Any]]:
        """
        Get OHLCV data from HuggingFace datasets with fallback
        """
        symbol = symbol.upper().replace("USDT", "").replace("USD", "")
        
        # Try linxy/CryptoCoin first
        if symbol in self.linxy_symbols and timeframe in self.linxy_timeframes:
            try:
                data = await self._get_linxy_ohlcv(symbol, timeframe, limit)
                if data:
                    logger.info(f"✅ linxy/CryptoCoin: Fetched {len(data)} candles for {symbol}/{timeframe}")
                    return data
            except Exception as e:
                logger.warning(f"⚠️ linxy/CryptoCoin failed for {symbol}/{timeframe}: {e}")
        
        # Try WinkingFace datasets
        if symbol in self.winkingface_datasets:
            try:
                data = await self._get_winkingface_ohlcv(symbol, timeframe, limit)
                if data:
                    logger.info(f"✅ WinkingFace: Fetched {len(data)} candles for {symbol}")
                    return data
            except Exception as e:
                logger.warning(f"⚠️ WinkingFace failed for {symbol}: {e}")
        
        raise HTTPException(
            status_code=404,
            detail=f"No HuggingFace dataset found for {symbol}/{timeframe}"
        )
    
    async def _get_linxy_ohlcv(
        self,
        symbol: str,
        timeframe: str,
        limit: int
    ) -> List[Dict[str, Any]]:
        """Get OHLCV data from linxy/CryptoCoin dataset"""
        cache_key = f"linxy_{symbol}_{timeframe}"
        
        # Check cache
        if cache_key in self._cache:
            cached_data, cached_time = self._cache[cache_key]
            if (datetime.utcnow().timestamp() - cached_time) < self._cache_duration:
                logger.info(f"✅ Returning cached data for {symbol}/{timeframe}")
                return cached_data[:limit]
        
        # Download CSV from HuggingFace
        # NOTE: linxy/CryptoCoin uses filenames like BTCUSDT_1h.csv (not BTC_1h.csv)
        candidate_files = [
            f"{symbol}USDT_{timeframe}.csv",
            f"{symbol}_{timeframe}.csv",  # legacy fallback
        ]

        response = None
        last_err = None
        async with httpx.AsyncClient(timeout=self.timeout) as client:
            for csv_filename in candidate_files:
                csv_url = f"{self.linxy_base_url}/{csv_filename}"
                try:
                    # These CSVs can be large (10MB+). Prefer a tail range request.
                    # We only need the most recent candles.
                    resp = await client.get(
                        csv_url,
                        follow_redirects=True,
                        headers={"Range": "bytes=-1500000"},
                    )
                    resp.raise_for_status()
                    response = resp
                    break
                except Exception as e:
                    last_err = e
                    continue

        if response is None:
            raise HTTPException(status_code=404, detail=f"linxy/CryptoCoin CSV not found for {symbol}/{timeframe}: {last_err}")
            
            # Parse CSV
            # Note: with Range requests we likely won't have the header row.
            csv_content = response.text
            lines = csv_content.splitlines()
            if not lines:
                raise HTTPException(status_code=404, detail=f"Empty CSV content for {symbol}/{timeframe}")

            # Drop first line if it's a partial row (common with Range tail)
            if lines and ("timestamp" not in lines[0].lower()):
                lines = lines[1:]

            # If header present, use DictReader; otherwise use fixed fieldnames.
            if lines and ("timestamp" in lines[0].lower() and "open" in lines[0].lower()):
                csv_reader = csv.DictReader(io.StringIO("\n".join(lines)))
            else:
                csv_reader = csv.DictReader(
                    io.StringIO("\n".join(lines)),
                    fieldnames=["timestamp", "open", "high", "low", "close", "volume"],
                )
            
            ohlcv_data = []
            for row in csv_reader:
                try:
                    # linxy/CryptoCoin CSV formats vary.
                    # Common format is Binance-style export with:
                    # "Open time,open,high,low,close,volume,Close time,..."
                    ts_raw = (
                        row.get("timestamp")
                        or row.get("Open time")
                        or row.get("open_time")
                        or row.get("time")
                        or row.get("date")
                    )
                    if ts_raw is None:
                        continue

                    # Parse timestamp (supports int, float, or datetime strings)
                    ts_val: int
                    try:
                        ts_val = int(float(ts_raw))
                    except Exception:
                        # Example: "2017-08-17 04:00:00"
                        try:
                            dt = datetime.fromisoformat(str(ts_raw).strip())
                        except Exception:
                            dt = datetime.strptime(str(ts_raw).strip(), "%Y-%m-%d %H:%M:%S")
                        ts_val = int(dt.timestamp() * 1000)

                    ohlcv_data.append({
                        "timestamp": ts_val,
                        "open": float(row.get("open", 0) or 0),
                        "high": float(row.get("high", 0) or 0),
                        "low": float(row.get("low", 0) or 0),
                        "close": float(row.get("close", 0) or 0),
                        "volume": float(row.get("volume", 0) or 0)
                    })
                except (ValueError, KeyError) as e:
                    logger.warning(f"⚠️ Failed to parse row: {e}")
                    continue
            
            # Sort by timestamp (newest first)
            ohlcv_data.sort(key=lambda x: x["timestamp"], reverse=True)
            
            # Cache the result
            self._cache[cache_key] = (ohlcv_data, datetime.utcnow().timestamp())
            
            return ohlcv_data[:limit]
    
    async def _get_winkingface_ohlcv(
        self,
        symbol: str,
        timeframe: str,
        limit: int
    ) -> List[Dict[str, Any]]:
        """Get OHLCV data from WinkingFace datasets"""
        cache_key = f"winkingface_{symbol}_{timeframe}"
        
        # Check cache
        if cache_key in self._cache:
            cached_data, cached_time = self._cache[cache_key]
            if (datetime.utcnow().timestamp() - cached_time) < self._cache_duration:
                logger.info(f"✅ Returning cached data for {symbol} (WinkingFace)")
                return cached_data[:limit]
        
        # WinkingFace datasets have different CSV filenames
        base_url = self.winkingface_datasets[symbol]
        
        # Try different possible filenames
        possible_files = [
            f"{symbol}USDT_{timeframe}.csv",
            f"data.csv",
            f"{symbol}USDT_1h.csv"  # Fallback to 1h if specific timeframe not found
        ]
        
        for csv_filename in possible_files:
            try:
                csv_url = f"{base_url}/{csv_filename}"
                
                async with httpx.AsyncClient(timeout=self.timeout) as client:
                    response = await client.get(
                        csv_url,
                        follow_redirects=True,
                        headers={"Range": "bytes=-1500000"},
                    )
                    response.raise_for_status()
                    
                    # Parse CSV
                    csv_content = response.text
                    lines = csv_content.splitlines()
                    if lines and ("timestamp" not in lines[0].lower()):
                        lines = lines[1:]
                    if lines and ("timestamp" in lines[0].lower() and "open" in lines[0].lower()):
                        csv_reader = csv.DictReader(io.StringIO("\n".join(lines)))
                    else:
                        csv_reader = csv.DictReader(
                            io.StringIO("\n".join(lines)),
                            fieldnames=["timestamp", "open", "high", "low", "close", "volume"],
                        )
                    
                    ohlcv_data = []
                    for row in csv_reader:
                        try:
                            # WinkingFace CSV format may vary
                            # Try to detect and parse correctly
                            timestamp_key = None
                            for key in ["timestamp", "time", "date", "unix"]:
                                if key in row:
                                    timestamp_key = key
                                    break
                            
                            if not timestamp_key:
                                # Try Binance-style export
                                if "Open time" in row:
                                    timestamp_key = "Open time"
                                else:
                                    continue
                            
                            ts_raw = row.get(timestamp_key, 0)
                            try:
                                ts_val = int(float(ts_raw))
                            except Exception:
                                try:
                                    dt = datetime.fromisoformat(str(ts_raw).strip())
                                except Exception:
                                    dt = datetime.strptime(str(ts_raw).strip(), "%Y-%m-%d %H:%M:%S")
                                ts_val = int(dt.timestamp() * 1000)

                            ohlcv_data.append({
                                "timestamp": ts_val,
                                "open": float(row.get("open", 0) or 0),
                                "high": float(row.get("high", 0) or 0),
                                "low": float(row.get("low", 0) or 0),
                                "close": float(row.get("close", 0) or 0),
                                "volume": float(row.get("volume", 0) or 0)
                            })
                        except (ValueError, KeyError) as e:
                            logger.warning(f"⚠️ Failed to parse row: {e}")
                            continue
                    
                    if ohlcv_data:
                        # Sort by timestamp (newest first)
                        ohlcv_data.sort(key=lambda x: x["timestamp"], reverse=True)
                        
                        # Cache the result
                        self._cache[cache_key] = (ohlcv_data, datetime.utcnow().timestamp())
                        
                        return ohlcv_data[:limit]
                
            except Exception as e:
                logger.warning(f"⚠️ Failed to fetch {csv_filename}: {e}")
                continue
        
        raise Exception(f"No data found for {symbol} in WinkingFace datasets")
    
    async def get_available_symbols(self) -> Dict[str, List[str]]:
        """
        Get list of available symbols from all datasets
        """
        return {
            "linxy_cryptocoin": self.linxy_symbols,
            "winkingface": list(self.winkingface_datasets.keys())
        }
    
    async def get_available_timeframes(self, symbol: str) -> List[str]:
        """
        Get available timeframes for a specific symbol
        """
        symbol = symbol.upper().replace("USDT", "").replace("USD", "")
        
        timeframes = []
        
        # Check linxy/CryptoCoin
        if symbol in self.linxy_symbols:
            timeframes.extend(self.linxy_timeframes)
        
        # WinkingFace datasets typically have 1h data
        if symbol in self.winkingface_datasets:
            timeframes.append("1h")
        
        return list(set(timeframes))  # Remove duplicates


# Global instance
hf_dataset_aggregator = HFDatasetAggregator()

__all__ = ["HFDatasetAggregator", "hf_dataset_aggregator"]