feat(api): 为美股/港股数据添加 stock info 信息

- 在 universal_fetcher._fetch_yfinance 中获取公司信息
- 包含 sector、industry、market_cap 字段
- 将信息存储在 DataFrame.attrs 中
- Flask API 自动提取并返回 info 字段
This commit is contained in:
2026-05-07 23:45:00 +08:00
parent 270f4fe7f4
commit 7cf20268cf
2 changed files with 26 additions and 1 deletions

View File

@@ -232,7 +232,8 @@ def dataframe_to_json(df: pd.DataFrame) -> Dict:
# 转换为字典列表
records = df_reset.to_dict(orient='records')
return {
# 构建返回结果
result = {
"data": records,
"count": len(records),
"columns": list(df_reset.columns),
@@ -241,6 +242,12 @@ def dataframe_to_json(df: pd.DataFrame) -> Dict:
"end": df.index.max().strftime('%Y-%m-%d') if hasattr(df.index.max(), 'strftime') else str(df.index.max()),
} if len(df) > 0 else None
}
# 添加股票信息(如果存在)
if hasattr(df, 'attrs') and df.attrs.get('info'):
result['info'] = df.attrs['info']
return result
def validate_date(date_str: str) -> bool:

View File

@@ -362,6 +362,20 @@ class UniversalDataFetcher:
try:
ticker = yf.Ticker(yf_code)
# 获取公司信息(仅对股票)
info = {}
if asset_type in ['us_stock', 'hk_stock']:
try:
stock_info = ticker.info
info = {
'sector': stock_info.get('sector'),
'industry': stock_info.get('industry'),
'market_cap': stock_info.get('marketCap'),
}
except Exception:
pass
# end_date 需要加一天yfinance 的 end 是排他的)
end_date_obj = pd.Timestamp(end_date) + timedelta(days=1)
data = ticker.history(
@@ -388,6 +402,10 @@ class UniversalDataFetcher:
data = data[available]
data['code'] = code
# 添加公司信息到 DataFrame 的 attrs属性
if info:
data.attrs['info'] = info
return data
except Exception as e: