feat: unify proxy config and centralize runtime logging
This commit is contained in:
1 parent
7af21bed6a
commit
80f2a82c19
9 files changed
+174
-43
No files matched your search
+20
-20
@@ -12,10 +12,14 @@ import pytz
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from data.database import DatabaseManager
|
||||
from utils.network_proxy import apply_global_proxy_from_env, get_proxy_url
|
||||
from utils.project_logger import get_project_logger
|
||||
from utils.send_msg import lark_notifier
|
||||
|
||||
# 加载环境变量
|
||||
load_dotenv()
|
||||
apply_global_proxy_from_env()
|
||||
logger = get_project_logger("data.fetcher")
|
||||
|
||||
|
||||
class BinanceDataFetcher:
|
||||
@@ -32,17 +36,16 @@ class BinanceDataFetcher:
|
||||
Args:
|
||||
cache_dir: 缓存/数据库目录
|
||||
"""
|
||||
# 配置代理
|
||||
# 统一读取代理配置,兼容全局代理环境变量。
|
||||
proxies = None
|
||||
use_proxy = os.getenv('USE_PROXY', 'false').lower() == 'true'
|
||||
proxy_url = get_proxy_url()
|
||||
|
||||
if use_proxy:
|
||||
socks5_proxy = os.getenv('SOCKS5_PROXY', 'socks5://127.0.0.1:7890')
|
||||
if proxy_url:
|
||||
proxies = {
|
||||
'http': socks5_proxy,
|
||||
'https': socks5_proxy
|
||||
'http': proxy_url,
|
||||
'https': proxy_url
|
||||
}
|
||||
print(f"✓ 使用SOCKS5代理: {socks5_proxy}")
|
||||
logger.info("使用代理: %s", proxy_url)
|
||||
|
||||
try:
|
||||
# 初始化Binance客户端(带代理)
|
||||
@@ -53,13 +56,10 @@ class BinanceDataFetcher:
|
||||
|
||||
# 测试连接
|
||||
self.client.ping()
|
||||
print("✓ 已成功连接到Binance API")
|
||||
logger.info("已成功连接到Binance API")
|
||||
except Exception as e:
|
||||
print(f"⚠ Binance API连接失败: {e}")
|
||||
print("提示:")
|
||||
print(" 1. 检查代理设置 (.env文件)")
|
||||
print(" 2. 确保代理服务正在运行")
|
||||
print(" 3. 或者使用已缓存的数据")
|
||||
logger.warning("Binance API连接失败: %s", e)
|
||||
logger.warning("提示: 检查代理设置、代理服务状态,或使用缓存数据")
|
||||
self.client = None
|
||||
|
||||
# 初始化数据库管理器
|
||||
@@ -112,13 +112,13 @@ class BinanceDataFetcher:
|
||||
|
||||
# 简单判断覆盖范围 (容忍少量缺失)
|
||||
if min_ts <= start_dt + timedelta(days=2) and max_ts >= end_dt - timedelta(days=2):
|
||||
print(f"从数据库加载数据:{symbol} {timeframe} ({len(df)} rows)")
|
||||
logger.info("从数据库加载数据: %s %s (%s rows)", symbol, timeframe, len(df))
|
||||
return df
|
||||
|
||||
# 2. 从API获取数据
|
||||
if self.client is None:
|
||||
if not df.empty:
|
||||
print(f"⚠ API不可用,返回数据库现有数据 ({len(df)} rows)")
|
||||
logger.warning("API不可用,返回数据库现有数据 (%s rows)", len(df))
|
||||
return df
|
||||
|
||||
raise ConnectionError(
|
||||
@@ -129,7 +129,7 @@ class BinanceDataFetcher:
|
||||
pair = f"{symbol}USDT"
|
||||
interval = self.INTERVALS[timeframe]
|
||||
|
||||
print(f"获取 {symbol} {timeframe} 数据,时间范围:{start_dt.date()} 至 {end_dt.date()}...")
|
||||
logger.info("获取 %s %s 数据,时间范围: %s 至 %s", symbol, timeframe, start_dt.date(), end_dt.date())
|
||||
|
||||
klines = []
|
||||
for attempt in range(max_retries):
|
||||
@@ -145,15 +145,15 @@ class BinanceDataFetcher:
|
||||
except Exception as e:
|
||||
# 如果是最后一次尝试,且依然失败
|
||||
if attempt == max_retries - 1:
|
||||
print(f"❌ 第 {attempt + 1} 次尝试失败,已达到最大重试次数。错误: {e}")
|
||||
logger.error("第 %s 次尝试失败,已达到最大重试次数。错误: %s", attempt + 1, e)
|
||||
if not df.empty:
|
||||
print(f"⚠ API请求最终失败,返回数据库现有数据")
|
||||
logger.warning("API请求最终失败,返回数据库现有数据")
|
||||
return df
|
||||
raise e # 如果没有缓存数据,抛出异常
|
||||
|
||||
# 计算等待时间 (指数退避: 2s, 4s, 8s...)
|
||||
wait_time = 2 * (2 ** attempt)
|
||||
print(f"⚠ 请求失败 (尝试 {attempt + 1}/{max_retries}),{wait_time}秒后重试... 错误: {e}")
|
||||
logger.warning("请求失败(尝试 %s/%s),%s 秒后重试,错误: %s", attempt + 1, max_retries, wait_time, e)
|
||||
time.sleep(wait_time)
|
||||
|
||||
if not klines:
|
||||
@@ -179,7 +179,7 @@ class BinanceDataFetcher:
|
||||
# 按时间排序
|
||||
new_df = new_df.sort_values('timestamp').reset_index(drop=True)
|
||||
|
||||
print(f"已获取 {len(new_df)} 行数据")
|
||||
logger.info("已获取 %s 行数据", len(new_df))
|
||||
|
||||
# 保存到数据库
|
||||
self.db_manager.save_ohlcv(new_df, symbol, timeframe)
|
||||
|
||||
Reference in new issue
Block a user