File size: 8,891 Bytes
3a660a3 |
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 |
import logging
import aiohttp
import os
import asyncio
from typing import Dict, List, Optional, Any
from datetime import datetime
from backend.orchestration.provider_manager import provider_manager, ProviderConfig
logger = logging.getLogger(__name__)
# ==============================================================================
# FETCH IMPLEMENTATIONS
# ==============================================================================
async def fetch_coingecko_market(config: ProviderConfig, **kwargs) -> Any:
ids = kwargs.get("ids", "bitcoin,ethereum")
vs_currency = kwargs.get("vs_currency", "usd")
url = f"{config.base_url}/coins/markets"
params = {
"vs_currency": vs_currency,
"ids": ids,
"order": "market_cap_desc",
"per_page": 100,
"page": 1,
"sparkline": "false",
"price_change_percentage": "24h"
}
# Pro API key support
if config.api_key:
params["x_cg_pro_api_key"] = config.api_key
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params, timeout=config.timeout) as response:
if response.status == 429:
raise Exception("Rate limit exceeded (429)")
response.raise_for_status()
return await response.json()
async def fetch_coingecko_price(config: ProviderConfig, **kwargs) -> Any:
coin_id = kwargs.get("coin_id", "bitcoin")
vs_currencies = kwargs.get("vs_currencies", "usd")
url = f"{config.base_url}/simple/price"
params = {"ids": coin_id, "vs_currencies": vs_currencies}
if config.api_key:
params["x_cg_pro_api_key"] = config.api_key
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params, timeout=config.timeout) as response:
response.raise_for_status()
return await response.json()
async def fetch_binance_ticker(config: ProviderConfig, **kwargs) -> Any:
symbol = kwargs.get("symbol", "BTCUSDT").upper()
url = f"{config.base_url}/ticker/price"
params = {"symbol": symbol}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params, timeout=config.timeout) as response:
if response.status == 451:
raise Exception("Geo-blocked (451)")
response.raise_for_status()
data = await response.json()
# Normalize to look somewhat like CoinGecko for generic usage if needed
return {"price": float(data.get("price", 0)), "symbol": data.get("symbol")}
async def fetch_binance_klines(config: ProviderConfig, **kwargs) -> Any:
symbol = kwargs.get("symbol", "BTCUSDT").upper()
interval = kwargs.get("interval", "1h")
limit = kwargs.get("limit", 100)
url = f"{config.base_url}/klines"
params = {
"symbol": symbol,
"interval": interval,
"limit": limit
}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params, timeout=config.timeout) as response:
if response.status == 451:
raise Exception("Geo-blocked (451)")
response.raise_for_status()
return await response.json()
async def fetch_cryptopanic_news(config: ProviderConfig, **kwargs) -> Any:
filter_type = kwargs.get("filter", "hot")
url = f"{config.base_url}/posts/"
params = {
"auth_token": config.api_key,
"filter": filter_type,
"public": "true"
}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params, timeout=config.timeout) as response:
response.raise_for_status()
return await response.json()
async def fetch_newsapi(config: ProviderConfig, **kwargs) -> Any:
query = kwargs.get("query", "crypto")
url = f"{config.base_url}/everything"
params = {
"q": query,
"apiKey": config.api_key,
"sortBy": "publishedAt",
"language": "en"
}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params, timeout=config.timeout) as response:
response.raise_for_status()
return await response.json()
async def fetch_alternative_me_fng(config: ProviderConfig, **kwargs) -> Any:
limit = kwargs.get("limit", 1)
url = f"{config.base_url}/fng/"
params = {"limit": limit}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params, timeout=config.timeout) as response:
response.raise_for_status()
return await response.json()
async def fetch_etherscan_gas(config: ProviderConfig, **kwargs) -> Any:
url = config.base_url
params = {
"module": "gastracker",
"action": "gasoracle",
"apikey": config.api_key
}
async with aiohttp.ClientSession() as session:
async with session.get(url, params=params, timeout=config.timeout) as response:
response.raise_for_status()
return await response.json()
# ==============================================================================
# REGISTRATION
# ==============================================================================
def initialize_providers():
# Market Data Providers
provider_manager.register_provider(
"market",
ProviderConfig(
name="coingecko_free",
category="market",
base_url="https://api.coingecko.com/api/v3",
rate_limit_per_min=30, # Conservative for free tier
weight=100
),
fetch_coingecko_market
)
provider_manager.register_provider(
"market_pro",
ProviderConfig(
name="coingecko_pro",
category="market",
base_url="https://pro-api.coingecko.com/api/v3", # Assuming Pro URL
api_key=os.getenv("COINGECKO_PRO_API_KEY", "04cf4b5b-9868-465c-8ba0-9f2e78c92eb1"),
rate_limit_per_min=500,
weight=200
),
fetch_coingecko_market
)
provider_manager.register_provider(
"market",
ProviderConfig(
name="binance",
category="market",
base_url="https://api.binance.com/api/v3",
rate_limit_per_min=1200,
weight=90
),
fetch_binance_ticker # Note: This fetch function behaves differently (ticker vs market list), router needs to handle
)
# OHLC Providers
provider_manager.register_provider(
"ohlc",
ProviderConfig(
name="binance_ohlc",
category="ohlc",
base_url="https://api.binance.com/api/v3",
rate_limit_per_min=1200,
weight=100
),
fetch_binance_klines
)
# News Providers
provider_manager.register_provider(
"news",
ProviderConfig(
name="cryptopanic",
category="news",
base_url="https://cryptopanic.com/api/v1",
api_key=os.getenv("CRYPTOPANIC_API_KEY", "7832690f05026639556837583758"), # Placeholder if env not set
rate_limit_per_min=60,
weight=100
),
fetch_cryptopanic_news
)
provider_manager.register_provider(
"news",
ProviderConfig(
name="newsapi",
category="news",
base_url="https://newsapi.org/v2",
api_key=os.getenv("NEWS_API_KEY", "968a5e25552b4cb5ba3280361d8444ab"),
rate_limit_per_min=100,
weight=90
),
fetch_newsapi
)
# Sentiment
provider_manager.register_provider(
"sentiment",
ProviderConfig(
name="alternative_me",
category="sentiment",
base_url="https://api.alternative.me",
rate_limit_per_min=60,
weight=100
),
fetch_alternative_me_fng
)
# OnChain / RPC
provider_manager.register_provider(
"onchain",
ProviderConfig(
name="etherscan",
category="onchain",
base_url="https://api.etherscan.io/api",
api_key=os.getenv("ETHERSCAN_API_KEY", "SZHYFZK2RR8H9TIMJBVW54V4H81K2Z2KR2"),
rate_limit_per_min=5, # Free tier limit
weight=100
),
fetch_etherscan_gas
)
provider_manager.register_provider(
"onchain",
ProviderConfig(
name="etherscan_backup",
category="onchain",
base_url="https://api.etherscan.io/api",
api_key=os.getenv("ETHERSCAN_API_KEY_2", "T6IR8VJHX2NE6ZJW2S3FDVN1TYG4PYYI45"),
rate_limit_per_min=5,
weight=90
),
fetch_etherscan_gas
)
# Auto-initialize
initialize_providers()
|