File size: 27,339 Bytes
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
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/**
 * Comprehensive API Client - Multi-Source with Fallback Chains
 * Integrates 150+ crypto data sources with automatic failover
 * Minimum 10 endpoints per query type as per requirements
 */

// ═══════════════════════════════════════════════════════════════
// API KEYS
// ═══════════════════════════════════════════════════════════════
// Never ship secrets in client-side code. Configure keys on the server side.
const API_KEYS = {
  ETHERSCAN: '',
  ETHERSCAN_BACKUP: '',
  BSCSCAN: '',
  TRONSCAN: '',
  CMC_PRIMARY: '',
  CMC_BACKUP: '',
  NEWSAPI: '',
  CRYPTOCOMPARE: '',
  HUGGINGFACE: ''
};

// ═══════════════════════════════════════════════════════════════
// CORS PROXIES
// ═══════════════════════════════════════════════════════════════
// Disabled on Hugging Face Spaces.
const CORS_PROXIES = [];

// ═══════════════════════════════════════════════════════════════
// MARKET DATA SOURCES (15+ endpoints)
// ═══════════════════════════════════════════════════════════════
const MARKET_SOURCES = [
  // Direct APIs (no proxy needed)
  {
    id: 'coingecko',
    name: 'CoinGecko',
    baseUrl: 'https://api.coingecko.com/api/v3',
    needsProxy: false,
    priority: 1,
    getPrice: (symbol) => `/simple/price?ids=${symbol}&vs_currencies=usd,eur&include_24hr_change=true&include_market_cap=true`
  },
  {
    id: 'coinpaprika',
    name: 'CoinPaprika',
    baseUrl: 'https://api.coinpaprika.com/v1',
    needsProxy: false,
    priority: 2,
    getPrice: (symbol) => `/tickers/${symbol}-${symbol}` // e.g., btc-bitcoin
  },
  {
    id: 'coincap',
    name: 'CoinCap',
    baseUrl: 'https://api.coincap.io/v2',
    needsProxy: false,
    priority: 3,
    getPrice: (symbol) => `/assets/${symbol}`
  },
  {
    id: 'binance',
    name: 'Binance Public',
    baseUrl: 'https://api.binance.com/api/v3',
    needsProxy: false,
    priority: 4,
    getPrice: (symbol) => `/ticker/price?symbol=${symbol.toUpperCase()}USDT`
  },
  {
    id: 'coinlore',
    name: 'CoinLore',
    baseUrl: 'https://api.coinlore.net/api',
    needsProxy: false,
    priority: 5,
    getPrice: (symbol) => `/ticker/?id=${symbol}` // requires coin ID
  },
  {
    id: 'defillama',
    name: 'DefiLlama',
    baseUrl: 'https://coins.llama.fi',
    needsProxy: false,
    priority: 6,
    getPrice: (symbol) => `/prices/current/coingecko:${symbol}`
  },
  {
    id: 'coinstats',
    name: 'CoinStats',
    baseUrl: 'https://api.coinstats.app/public/v1',
    needsProxy: false,
    priority: 7,
    getPrice: (symbol) => `/coins/${symbol}`
  },
  {
    id: 'messari',
    name: 'Messari',
    baseUrl: 'https://data.messari.io/api/v1',
    needsProxy: false,
    priority: 8,
    getPrice: (symbol) => `/assets/${symbol}/metrics`
  },
  {
    id: 'nomics',
    name: 'Nomics',
    baseUrl: 'https://api.nomics.com/v1',
    needsProxy: false,
    priority: 9,
    getPrice: (symbol) => `/currencies/ticker?ids=${symbol.toUpperCase()}&convert=USD`
  },
  {
    id: 'coindesk',
    name: 'CoinDesk',
    baseUrl: 'https://api.coindesk.com/v1',
    needsProxy: false,
    priority: 10,
    getPrice: () => `/bpi/currentprice.json` // Bitcoin only
  },
  // APIs requiring proxy or keys
  {
    id: 'cmc_primary',
    name: 'CoinMarketCap',
    baseUrl: 'https://pro-api.coinmarketcap.com/v1',
    needsProxy: true,
    priority: 11,
    headers: () => ({ 'X-CMC_PRO_API_KEY': API_KEYS.CMC_PRIMARY }),
    getPrice: (symbol) => `/cryptocurrency/quotes/latest?symbol=${symbol.toUpperCase()}`
  },
  {
    id: 'cmc_backup',
    name: 'CoinMarketCap Backup',
    baseUrl: 'https://pro-api.coinmarketcap.com/v1',
    needsProxy: true,
    priority: 12,
    headers: () => ({ 'X-CMC_PRO_API_KEY': API_KEYS.CMC_BACKUP }),
    getPrice: (symbol) => `/cryptocurrency/quotes/latest?symbol=${symbol.toUpperCase()}`
  },
  {
    id: 'cryptocompare',
    name: 'CryptoCompare',
    baseUrl: 'https://min-api.cryptocompare.com/data',
    needsProxy: false,
    priority: 13,
    getPrice: (symbol) => `/price?fsym=${symbol.toUpperCase()}&tsyms=USD,EUR&api_key=${API_KEYS.CRYPTOCOMPARE}`
  },
  {
    id: 'kraken',
    name: 'Kraken Public',
    baseUrl: 'https://api.kraken.com/0/public',
    needsProxy: false,
    priority: 14,
    getPrice: (symbol) => `/Ticker?pair=${symbol.toUpperCase()}USD`
  },
  {
    id: 'bitfinex',
    name: 'Bitfinex Public',
    baseUrl: 'https://api-pub.bitfinex.com/v2',
    needsProxy: false,
    priority: 15,
    getPrice: (symbol) => `/ticker/t${symbol.toUpperCase()}USD`
  }
];

// ═══════════════════════════════════════════════════════════════
// NEWS SOURCES (12+ endpoints)
// ═══════════════════════════════════════════════════════════════
const NEWS_SOURCES = [
  {
    id: 'cryptopanic',
    name: 'CryptoPanic',
    baseUrl: 'https://cryptopanic.com/api/v1',
    needsProxy: false,
    priority: 1,
    getNews: () => `/posts/?public=true`
  },
  {
    id: 'coinstats_news',
    name: 'CoinStats News',
    baseUrl: 'https://api.coinstats.app/public/v1',
    needsProxy: false,
    priority: 2,
    getNews: () => `/news`
  },
  {
    id: 'cointelegraph_rss',
    name: 'Cointelegraph RSS',
    baseUrl: 'https://cointelegraph.com',
    needsProxy: false,
    priority: 3,
    getNews: () => `/rss`,
    parseRSS: true
  },
  {
    id: 'coindesk_rss',
    name: 'CoinDesk RSS',
    baseUrl: 'https://www.coindesk.com',
    needsProxy: false,
    priority: 4,
    getNews: () => `/arc/outboundfeeds/rss/?outputType=xml`,
    parseRSS: true
  },
  {
    id: 'decrypt_rss',
    name: 'Decrypt RSS',
    baseUrl: 'https://decrypt.co',
    needsProxy: false,
    priority: 5,
    getNews: () => `/feed`,
    parseRSS: true
  },
  {
    id: 'bitcoin_magazine_rss',
    name: 'Bitcoin Magazine RSS',
    baseUrl: 'https://bitcoinmagazine.com',
    needsProxy: false,
    priority: 6,
    getNews: () => `/.rss/full/`,
    parseRSS: true
  },
  {
    id: 'reddit_crypto',
    name: 'Reddit r/CryptoCurrency',
    baseUrl: 'https://www.reddit.com/r/CryptoCurrency',
    needsProxy: false,
    priority: 7,
    getNews: () => `/hot.json?limit=25`
  },
  {
    id: 'reddit_bitcoin',
    name: 'Reddit r/Bitcoin',
    baseUrl: 'https://www.reddit.com/r/Bitcoin',
    needsProxy: false,
    priority: 8,
    getNews: () => `/new.json?limit=25`
  },
  {
    id: 'blockworks',
    name: 'Blockworks RSS',
    baseUrl: 'https://blockworks.co',
    needsProxy: false,
    priority: 9,
    getNews: () => `/feed`,
    parseRSS: true
  },
  {
    id: 'theblock_rss',
    name: 'The Block RSS',
    baseUrl: 'https://www.theblock.co',
    needsProxy: false,
    priority: 10,
    getNews: () => `/rss.xml`,
    parseRSS: true
  },
  {
    id: 'coinjournal',
    name: 'CoinJournal RSS',
    baseUrl: 'https://coinjournal.net',
    needsProxy: false,
    priority: 11,
    getNews: () => `/feed/`,
    parseRSS: true
  },
  {
    id: 'cryptoslate_rss',
    name: 'CryptoSlate RSS',
    baseUrl: 'https://cryptoslate.com',
    needsProxy: false,
    priority: 12,
    getNews: () => `/feed/`,
    parseRSS: true
  }
];

// ═══════════════════════════════════════════════════════════════
// SENTIMENT SOURCES (10+ endpoints for Fear & Greed)
// ═══════════════════════════════════════════════════════════════
const SENTIMENT_SOURCES = [
  {
    id: 'alternative_me',
    name: 'Alternative.me F&G',
    baseUrl: 'https://api.alternative.me',
    needsProxy: false,
    priority: 1,
    getSentiment: () => `/fng/?limit=1`
  },
  {
    id: 'cfgi_v1',
    name: 'CFGI API v1',
    baseUrl: 'https://api.cfgi.io/v1',
    needsProxy: false,
    priority: 2,
    getSentiment: () => `/fear-greed`
  },
  {
    id: 'cfgi_legacy',
    name: 'CFGI Legacy',
    baseUrl: 'https://cfgi.io',
    needsProxy: false,
    priority: 3,
    getSentiment: () => `/api`
  },
  {
    id: 'coinglass_fgi',
    name: 'CoinGlass F&G',
    baseUrl: 'https://open-api.coinglass.com/public/v2',
    needsProxy: false,
    priority: 4,
    getSentiment: () => `/indicator/fear_greed`
  },
  {
    id: 'lunarcrush',
    name: 'LunarCrush Social',
    baseUrl: 'https://api.lunarcrush.com/v2',
    needsProxy: false,
    priority: 5,
    getSentiment: () => `?data=global`
  },
  {
    id: 'santiment',
    name: 'Santiment Social Volume',
    baseUrl: 'https://api.santiment.net',
    needsProxy: false,
    priority: 6,
    getSentiment: () => `/graphql`,
    method: 'POST'
  },
  {
    id: 'thetie',
    name: 'TheTie.io Sentiment',
    baseUrl: 'https://api.thetie.io',
    needsProxy: false,
    priority: 7,
    getSentiment: () => `/v1/sentiment?symbol=BTC`
  },
  {
    id: 'augmento',
    name: 'Augmento AI Sentiment',
    baseUrl: 'https://api.augmento.ai/v1',
    needsProxy: false,
    priority: 8,
    getSentiment: () => `/signals/overview`
  },
  {
    id: 'cryptoquant_sentiment',
    name: 'CryptoQuant Sentiment',
    baseUrl: 'https://api.cryptoquant.com/v1',
    needsProxy: false,
    priority: 9,
    getSentiment: () => `/btc/indicator/fear-greed`
  },
  {
    id: 'glassnode_social',
    name: 'Glassnode Social Metrics',
    baseUrl: 'https://api.glassnode.com/v1',
    needsProxy: false,
    priority: 10,
    getSentiment: () => `/metrics/social/sentiment_positive`
  }
];

// ═══════════════════════════════════════════════════════════════
// HELPER FUNCTIONS
// ═══════════════════════════════════════════════════════════════

async function fetchWithTimeout(url, options = {}, timeout = 10000) {
  const controller = new AbortController();
  const id = setTimeout(() => controller.abort(), timeout);
  
  try {
    const response = await fetch(url, {
      ...options,
      signal: controller.signal
    });
    clearTimeout(id);
    return response;
  } catch (error) {
    clearTimeout(id);
    throw error;
  }
}

async function fetchDirect(url, options = {}) {
  try {
    const response = await fetchWithTimeout(url, options);
    if (!response.ok) {
      throw new Error(`HTTP ${response.status}: ${response.statusText}`);
    }
    const contentType = response.headers.get('content-type');
    if (contentType && contentType.includes('application/json')) {
      return await response.json();
    }
    return await response.text();
  } catch (error) {
    throw new Error(`Direct fetch failed: ${error.message}`);
  }
}

async function fetchWithProxy(url, options = {}, proxyIndex = 0) {
  if (proxyIndex >= CORS_PROXIES.length) {
    throw new Error('All CORS proxies exhausted');
  }
  
  const proxy = CORS_PROXIES[proxyIndex];
  const proxyUrl = proxy + encodeURIComponent(url);
  
  try {
    const response = await fetchWithTimeout(proxyUrl, {
      ...options,
      headers: {
        ...options.headers,
        'Origin': window.location.origin,
        'x-requested-with': 'XMLHttpRequest'
      }
    });
    
    if (!response.ok) {
      throw new Error(`Proxy returned ${response.status}`);
    }
    
    const data = await response.json();
    // Handle allOrigins response format
    return data.contents ? JSON.parse(data.contents) : data;
  } catch (error) {
    console.warn(`Proxy ${proxyIndex + 1} failed:`, error.message);
    // Try next proxy
    return fetchWithProxy(url, options, proxyIndex + 1);
  }
}

function parseRSS(xmlText, sourceName) {
  const parser = new DOMParser();
  const doc = parser.parseFromString(xmlText, 'text/xml');
  const items = doc.querySelectorAll('item');
  
  const news = [];
  items.forEach((item, index) => {
    if (index >= 20) return; // Limit to 20 items
    
    const title = item.querySelector('title')?.textContent || '';
    const link = item.querySelector('link')?.textContent || '';
    const pubDate = item.querySelector('pubDate')?.textContent || '';
    const description = item.querySelector('description')?.textContent || '';
    
    if (title && link) {
      news.push({
        title,
        link,
        publishedAt: pubDate,
        description: description.substring(0, 200),
        source: sourceName
      });
    }
  });
  
  return news;
}

// ═══════════════════════════════════════════════════════════════
// MAIN API CLIENT CLASS
// ═══════════════════════════════════════════════════════════════

class ComprehensiveAPIClient {
  constructor() {
    this.cache = new Map();
    this.cacheTimeout = 60000; // 1 minute
    this.requestLog = [];
  }

  // Cache management
  getCached(key) {
    const cached = this.cache.get(key);
    if (cached && Date.now() - cached.timestamp < this.cacheTimeout) {
      console.log(`πŸ“¦ Cache hit: ${key}`);
      return cached.data;
    }
    return null;
  }

  setCache(key, data) {
    this.cache.set(key, {
      data,
      timestamp: Date.now()
    });
  }

  // Log requests for debugging
  logRequest(source, success, error = null) {
    this.requestLog.push({
      source,
      success,
      error,
      timestamp: new Date().toISOString()
    });
    
    // Keep only last 100 logs
    if (this.requestLog.length > 100) {
      this.requestLog.shift();
    }
  }

  // ═══════════════════════════════════════════════════════════
  // MARKET DATA - Try all 15+ sources
  // ═══════════════════════════════════════════════════════════
  async getMarketPrice(symbol) {
    const cacheKey = `market_${symbol}`;
    const cached = this.getCached(cacheKey);
    if (cached) return cached;

    const normalizedSymbol = symbol.toLowerCase();
    const sources = [...MARKET_SOURCES].sort((a, b) => a.priority - b.priority);

    for (const source of sources) {
      try {
        console.log(`πŸ”„ Trying ${source.name} for ${symbol}...`);
        
        const endpoint = source.getPrice(normalizedSymbol);
        const url = `${source.baseUrl}${endpoint}`;
        const options = source.headers ? { headers: source.headers() } : {};

        let data;
        if (source.needsProxy) {
          data = await fetchWithProxy(url, options);
        } else {
          data = await fetchDirect(url, options);
        }

        // Normalize response based on source
        const normalized = this.normalizeMarketData(data, source.id, symbol);
        if (normalized) {
          this.setCache(cacheKey, normalized);
          this.logRequest(source.name, true);
          console.log(`βœ… Success: ${source.name}`);
          return normalized;
        }
      } catch (error) {
        console.warn(`❌ ${source.name} failed:`, error.message);
        this.logRequest(source.name, false, error.message);
        continue;
      }
    }

    throw new Error(`All ${sources.length} market data sources failed for ${symbol}`);
  }

  normalizeMarketData(data, sourceId, symbol) {
    try {
      switch (sourceId) {
        case 'coingecko':
          const coinId = symbol.toLowerCase();
          return {
            symbol: symbol.toUpperCase(),
            price: data[coinId]?.usd || null,
            change24h: data[coinId]?.usd_24h_change || null,
            marketCap: data[coinId]?.usd_market_cap || null,
            source: 'CoinGecko',
            timestamp: Date.now()
          };
        
        case 'binance':
          return {
            symbol: symbol.toUpperCase(),
            price: parseFloat(data.price),
            source: 'Binance',
            timestamp: Date.now()
          };
        
        case 'coincap':
          return {
            symbol: symbol.toUpperCase(),
            price: parseFloat(data.data?.priceUsd || 0),
            change24h: parseFloat(data.data?.changePercent24Hr || 0),
            marketCap: parseFloat(data.data?.marketCapUsd || 0),
            source: 'CoinCap',
            timestamp: Date.now()
          };
        
        case 'cmc_primary':
        case 'cmc_backup':
          const cmcData = data.data?.[symbol.toUpperCase()];
          return {
            symbol: symbol.toUpperCase(),
            price: cmcData?.quote?.USD?.price || null,
            change24h: cmcData?.quote?.USD?.percent_change_24h || null,
            marketCap: cmcData?.quote?.USD?.market_cap || null,
            source: 'CoinMarketCap',
            timestamp: Date.now()
          };
        
        default:
          // Generic fallback
          return {
            symbol: symbol.toUpperCase(),
            price: data.price || data.last || data.lastPrice || null,
            source: sourceId,
            timestamp: Date.now(),
            raw: data
          };
      }
    } catch (error) {
      console.warn(`Failed to normalize ${sourceId} data:`, error);
      return null;
    }
  }

  // ═══════════════════════════════════════════════════════════
  // NEWS - Try all 12+ sources
  // ═══════════════════════════════════════════════════════════
  async getNews(limit = 20) {
    const cacheKey = 'news_latest';
    const cached = this.getCached(cacheKey);
    if (cached) return cached;

    const allNews = [];
    const sources = [...NEWS_SOURCES].sort((a, b) => a.priority - b.priority);

    for (const source of sources) {
      try {
        console.log(`πŸ”„ Fetching news from ${source.name}...`);
        
        const endpoint = source.getNews();
        const url = `${source.baseUrl}${endpoint}`;

        let data;
        if (source.needsProxy) {
          data = await fetchWithProxy(url);
        } else {
          data = await fetchDirect(url);
        }

        let news = [];
        if (source.parseRSS) {
          news = parseRSS(data, source.name);
        } else {
          news = this.normalizeNewsData(data, source.id, source.name);
        }

        if (news && news.length > 0) {
          allNews.push(...news);
          this.logRequest(source.name, true);
          console.log(`βœ… Got ${news.length} articles from ${source.name}`);
        }

        // Stop if we have enough news
        if (allNews.length >= limit * 2) break;
      } catch (error) {
        console.warn(`❌ ${source.name} failed:`, error.message);
        this.logRequest(source.name, false, error.message);
        continue;
      }
    }

    // Deduplicate and sort by date
    const uniqueNews = this.deduplicateNews(allNews);
    const sortedNews = uniqueNews.slice(0, limit);
    
    this.setCache(cacheKey, sortedNews);
    return sortedNews;
  }

  normalizeNewsData(data, sourceId, sourceName) {
    try {
      switch (sourceId) {
        case 'cryptopanic':
          return data.results?.map(item => ({
            title: item.title,
            link: item.url,
            publishedAt: item.published_at,
            source: item.source?.title || sourceName,
            votes: item.votes?.positive || 0
          })) || [];
        
        case 'coinstats_news':
          return data.news?.map(item => ({
            title: item.title,
            link: item.link,
            publishedAt: item.feedDate,
            source: item.source || sourceName,
            imgURL: item.imgURL
          })) || [];
        
        case 'reddit_crypto':
        case 'reddit_bitcoin':
          return data.data?.children?.map(item => ({
            title: item.data.title,
            link: `https://reddit.com${item.data.permalink}`,
            publishedAt: new Date(item.data.created_utc * 1000).toISOString(),
            source: sourceName,
            score: item.data.score
          })) || [];
        
        default:
          return [];
      }
    } catch (error) {
      console.warn(`Failed to normalize ${sourceId} news:`, error);
      return [];
    }
  }

  deduplicateNews(newsArray) {
    const seen = new Set();
    return newsArray.filter(item => {
      const key = item.title.toLowerCase().trim();
      if (seen.has(key)) return false;
      seen.add(key);
      return true;
    });
  }

  // ═══════════════════════════════════════════════════════════
  // SENTIMENT (Fear & Greed) - Try all 10+ sources
  // ═══════════════════════════════════════════════════════════
  async getSentiment() {
    const cacheKey = 'sentiment_fng';
    const cached = this.getCached(cacheKey);
    if (cached) return cached;

    const sources = [...SENTIMENT_SOURCES].sort((a, b) => a.priority - b.priority);

    for (const source of sources) {
      try {
        console.log(`πŸ”„ Trying ${source.name} for sentiment...`);
        
        const endpoint = source.getSentiment();
        const url = `${source.baseUrl}${endpoint}`;
        const options = source.method === 'POST' ? { method: 'POST' } : {};

        let data;
        if (source.needsProxy) {
          data = await fetchWithProxy(url, options);
        } else {
          data = await fetchDirect(url, options);
        }

        const normalized = this.normalizeSentimentData(data, source.id);
        if (normalized && normalized.value !== null) {
          this.setCache(cacheKey, normalized);
          this.logRequest(source.name, true);
          console.log(`βœ… Sentiment from ${source.name}: ${normalized.value}`);
          return normalized;
        }
      } catch (error) {
        console.warn(`❌ ${source.name} failed:`, error.message);
        this.logRequest(source.name, false, error.message);
        continue;
      }
    }

    throw new Error(`All ${sources.length} sentiment sources failed`);
  }

  normalizeSentimentData(data, sourceId) {
    try {
      switch (sourceId) {
        case 'alternative_me':
          const fngData = data.data?.[0];
          return {
            value: parseInt(fngData?.value || 0),
            classification: fngData?.value_classification || 'Unknown',
            source: 'Alternative.me',
            timestamp: Date.now()
          };
        
        case 'cfgi_v1':
        case 'cfgi_legacy':
          return {
            value: parseInt(data.value || data.fgi || 0),
            classification: data.classification || this.getClassification(data.value),
            source: 'CFGI',
            timestamp: Date.now()
          };
        
        case 'coinglass_fgi':
          return {
            value: parseInt(data.data?.value || 0),
            classification: data.data?.value_classification || 'Unknown',
            source: 'CoinGlass',
            timestamp: Date.now()
          };
        
        default:
          // Generic fallback
          const value = parseInt(data.value || data.score || 50);
          return {
            value,
            classification: this.getClassification(value),
            source: sourceId,
            timestamp: Date.now(),
            raw: data
          };
      }
    } catch (error) {
      console.warn(`Failed to normalize ${sourceId} sentiment:`, error);
      return null;
    }
  }

  getClassification(value) {
    if (value <= 25) return 'Extreme Fear';
    if (value <= 45) return 'Fear';
    if (value <= 55) return 'Neutral';
    if (value <= 75) return 'Greed';
    return 'Extreme Greed';
  }

  // ═══════════════════════════════════════════════════════════
  // OHLCV DATA (Import from dedicated client)
  // ═══════════════════════════════════════════════════════════
  async getOHLCV(symbol, timeframe = '1d', limit = 100) {
    try {
      // Dynamically import OHLCV client
      const { default: ohlcvClient } = await import('/static/shared/js/ohlcv-client.js');
      return await ohlcvClient.getOHLCV(symbol, timeframe, limit);
    } catch (error) {
      console.error('Failed to load OHLCV client:', error);
      throw error;
    }
  }

  // ═══════════════════════════════════════════════════════════
  // UTILITY: Get request statistics
  // ═══════════════════════════════════════════════════════════
  getStats() {
    const total = this.requestLog.length;
    const successful = this.requestLog.filter(r => r.success).length;
    const failed = total - successful;
    const successRate = total > 0 ? ((successful / total) * 100).toFixed(1) : 0;

    return {
      total,
      successful,
      failed,
      successRate: `${successRate}%`,
      cacheSize: this.cache.size,
      recentRequests: this.requestLog.slice(-10)
    };
  }

  // Clear cache
  clearCache() {
    this.cache.clear();
    console.log('βœ… Cache cleared');
  }
}

// ═══════════════════════════════════════════════════════════════
// EXPORT
// ═══════════════════════════════════════════════════════════════
export const apiClient = new ComprehensiveAPIClient();
export default apiClient;