This commit is contained in:
2026-03-09 17:17:48 +08:00
parent e611911738
commit fffee149ec
5 changed files with 68 additions and 8 deletions
+51 -5
View File
@@ -1,8 +1,10 @@
from api.types.colg import HistoryGold, RealTimeGold import asyncio
from api.types.colg import HistoryGold, OfficialGold, RealTimeGold
from fastapi.params import Param from fastapi.params import Param
from api.core.Gzip import register_gzip_request from api.core.Gzip import register_gzip_request
import json import json
from fastapi import APIRouter, Path, Query ,Depends from fastapi import APIRouter, Path, Query, Depends
from api.core.Response import response from api.core.Response import response
from api.core.Redis import get_redis_info from api.core.Redis import get_redis_info
from api.dp import RedisDep from api.dp import RedisDep
@@ -11,6 +13,7 @@ import requests
from config.main import config from config.main import config
from datetime import datetime, timezone, timedelta from datetime import datetime, timezone, timedelta
from api.core.Response import Return as Response from api.core.Response import Return as Response
import aiohttp
router = APIRouter() router = APIRouter()
@@ -45,7 +48,6 @@ async def get_real_time_gold(redis: RedisDep) -> Response[list[RealTimeGold]]:
""" """
key = 'openapi:colg:real_time_gold' key = 'openapi:colg:real_time_gold'
try: try:
def get_gold_data(): def get_gold_data():
res = [] res = []
for region_name, region_id in regionMap.items(): for region_name, region_id in regionMap.items():
@@ -94,7 +96,6 @@ async def get_history_gold(region: Annotated[int, Query(..., description='跨区
- region参数必须在regionMap的值范围内 - region参数必须在regionMap的值范围内
- timeRange参数是可选的,使用默认值7表示最近7天的数据 - timeRange参数是可选的,使用默认值7表示最近7天的数据
""" """
print(region, timeRange)
if region not in regionMap.values(): if region not in regionMap.values():
return response( return response(
code=404, code=404,
@@ -129,10 +130,55 @@ async def get_history_gold(region: Annotated[int, Query(..., description='跨区
'tooltip': {'trigger': 'axis', 'axisPointer': {'type': 'cross', 'label': {'backgroundColor': '#6a7985'}}}, 'tooltip': {'trigger': 'axis', 'axisPointer': {'type': 'cross', 'label': {'backgroundColor': '#6a7985'}}},
'legend': {'data': ['平均金价', '12时金价'], 'bottom': 0}, 'legend': {'data': ['平均金价', '12时金价'], 'bottom': 0},
'xAxis': {'type': 'category', 'name': '日期', 'boundaryGap': False, 'data': [item['date'] for item in data]}, 'xAxis': {'type': 'category', 'name': '日期', 'boundaryGap': False, 'data': [item['date'] for item in data]},
'yAxis': {'type': 'value', 'name': '价格 (万金币/1人民币)', 'min': (min([item['avg'] for item in data] + [item['zero'] for item in data]) // 5 ) * 5, 'max': (max([item['avg'] for item in data] + [item['zero'] for item in data]) // 5 ) * 5 + 5, 'axisLabel': {'formatter': '{value} 万金币'}}, 'yAxis': {'type': 'value', 'name': '价格 (万金币/1人民币)', 'min': (min([item['avg'] for item in data] + [item['zero'] for item in data]) // 5) * 5, 'max': (max([item['avg'] for item in data] + [item['zero'] for item in data]) // 5) * 5 + 5, 'axisLabel': {'formatter': '{value} 万金币'}},
'series': [ 'series': [
{'name': '平均金价', 'type': 'line', 'smooth': True, 'data': [item['avg'] for item in data], 'markPoint': {'data': [{'type': 'max', 'name': '最大值'}, {'type': 'min', 'name': '最小值'}]}}, {'name': '平均金价', 'type': 'line', 'smooth': True, 'data': [item['avg'] for item in data], 'markPoint': {'data': [{'type': 'max', 'name': '最大值'}, {'type': 'min', 'name': '最小值'}]}},
{'name': '12时金价', 'type': 'line', 'smooth': True, 'data': [item['zero'] for item in data], 'markLine': {'data': [{'type': 'average', 'name': '平均值'}]}}, {'name': '12时金价', 'type': 'line', 'smooth': True, 'data': [item['zero'] for item in data], 'markLine': {'data': [{'type': 'average', 'name': '平均值'}]}},
], ],
} }
return response(data={'region': region_name, 'data': data, 'echartsData': echartsData}) return response(data={'region': region_name, 'data': data, 'echartsData': echartsData})
@router.get('/gold/official/', operation_id='officialGold')
async def get_official_gold(redis: RedisDep) -> Response[list[OfficialGold]]:
"""
获取官方金币寄售实时数据
"""
official_url = 'https://dzhu.qq.com/zangyi/activity/app'
headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}
params = {
'r': 'godRank/godDealRank',
'userId': config.HELPER_ID,
'token': config.HELPER_TOKEN,
}
key = 'openapi:colg:real_time_gold_officical'
list = ['1', '2', '3A', '3B', '4', '5', '6', '7']
try:
async def get_data():
datas = []
async def fetch_data():
async with aiohttp.ClientSession() as session:
tasks = []
for i in list:
params['serverNo'] = i
query = '&'.join(f'{k}={v}' for k, v in params.items())
tasks.append(session.post(official_url, data=query, headers=headers, timeout=10))
responses = await asyncio.gather(*tasks)
for index, resp in enumerate(responses):
res = json.loads(await resp.read())
datas.append({
'updataTime': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
'region': f'跨{list[index]}',
'godDealRank': [int(i.get('price')) for i in res['data']['godDealRank']],
'godTopDealRank': [int(i.get('price')) for i in res['data']['godTopDealRank']],
})
return datas
return await fetch_data()
except Exception as e:
print(e)
return response(data=[])
# 设置半小时过期一次
data = await get_redis_info(redis, key, get_data, 30 * 60)
return response(data=data)
+11 -1
View File
@@ -26,4 +26,14 @@ class HistoryGold(BaseModel):
"""跨区""" """跨区"""
data: list[DayGold] = Field(..., description='历史数据') data: list[DayGold] = Field(..., description='历史数据')
echartsData : dict = Field(..., description='echarts数据') echartsData : dict = Field(..., description='echarts数据')
"""echarts数据""" """echarts数据"""
class OfficialGold(BaseModel):
updataTime: str = Field(..., description='更新时间')
"""更新时间"""
region: str = Field(..., description='跨区')
"""跨区"""
godDealRank: list[float] = Field(..., description='金币寄售最低成交价排名前5的价格')
"""金币寄售最低成交价排名前5的价格"""
godTopDealRank: list[float] = Field(..., description='金币寄售最高成交价排名前5的价格')
"""金币寄售最高成交价排名前5的价格"""
+3 -1
View File
@@ -3,4 +3,6 @@ MCPPORT=47174
DEBUG_MODE=True DEBUG_MODE=True
WORKERS=1 WORKERS=1
REDIS_URL=redis://localhost:6379/1 REDIS_URL=redis://localhost:6379/1
COLG_TOKEN="Bearer " COLG_TOKEN="Bearer "
HELPER_TOKEN=""
HELPER_ID=""
+2
View File
@@ -8,6 +8,8 @@ class Config(BaseSettings):
MCPPORT: int = 27174 MCPPORT: int = 27174
REDIS_URL: str = 'redis://localhost:6379/1' REDIS_URL: str = 'redis://localhost:6379/1'
COLG_TOKEN: str = '' COLG_TOKEN: str = ''
HELPER_TOKEN: str = ''
HELPER_ID: str = ''
class Config: class Config:
env_file = ('config/.env', 'config/.env.dev', 'config/.env.prod') env_file = ('config/.env', 'config/.env.dev', 'config/.env.prod')