81 lines
2.1 KiB
Python
81 lines
2.1 KiB
Python
"""
|
|
股票数据API路由
|
|
"""
|
|
from fastapi import APIRouter, HTTPException, Query
|
|
from typing import Optional
|
|
from app.services.tushare_service import tushare_service
|
|
from app.utils.logger import logger
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/quote/{stock_code}")
|
|
async def get_quote(stock_code: str):
|
|
"""
|
|
获取股票实时行情
|
|
|
|
Args:
|
|
stock_code: 股票代码
|
|
|
|
Returns:
|
|
行情数据
|
|
"""
|
|
try:
|
|
quote = tushare_service.get_realtime_quote(stock_code)
|
|
if not quote:
|
|
raise HTTPException(status_code=404, detail="未找到股票数据")
|
|
return quote
|
|
except Exception as e:
|
|
logger.error(f"获取行情失败: {e}")
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.get("/kline/{stock_code}")
|
|
async def get_kline(
|
|
stock_code: str,
|
|
start_date: Optional[str] = Query(None, description="开始日期YYYYMMDD"),
|
|
end_date: Optional[str] = Query(None, description="结束日期YYYYMMDD"),
|
|
period: str = Query("D", description="周期D/W/M")
|
|
):
|
|
"""
|
|
获取K线数据
|
|
|
|
Args:
|
|
stock_code: 股票代码
|
|
start_date: 开始日期
|
|
end_date: 结束日期
|
|
period: 周期
|
|
|
|
Returns:
|
|
K线数据
|
|
"""
|
|
try:
|
|
kline = tushare_service.get_kline_data(stock_code, start_date, end_date, period)
|
|
if not kline:
|
|
raise HTTPException(status_code=404, detail="未找到K线数据")
|
|
return {"kline_data": kline}
|
|
except Exception as e:
|
|
logger.error(f"获取K线失败: {e}")
|
|
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
@router.get("/basic/{stock_code}")
|
|
async def get_basic(stock_code: str):
|
|
"""
|
|
获取股票基本信息
|
|
|
|
Args:
|
|
stock_code: 股票代码
|
|
|
|
Returns:
|
|
基本信息
|
|
"""
|
|
try:
|
|
basic = tushare_service.get_stock_basic(stock_code)
|
|
if not basic:
|
|
raise HTTPException(status_code=404, detail="未找到股票信息")
|
|
return basic
|
|
except Exception as e:
|
|
logger.error(f"获取基本信息失败: {e}")
|
|
raise HTTPException(status_code=500, detail=str(e))
|