first code

This commit is contained in:
Coldin04 2025-08-13 18:49:19 +08:00
commit b18805aa26
11 changed files with 612 additions and 0 deletions

15
handlers/base_handler.py Normal file
View file

@ -0,0 +1,15 @@
from abc import ABC, abstractmethod
from typing import Dict, Any, List
class BaseHandler(ABC):
"""基础处理器类"""
@abstractmethod
def handle(self, **kwargs) -> List[Dict[str, Any]]:
"""处理请求并返回RSS项目列表"""
pass
@abstractmethod
def get_handler_name(self) -> str:
"""返回处理器名称"""
pass

View file

@ -0,0 +1,50 @@
from typing import Dict, Any, List
from datetime import datetime
from .base_handler import BaseHandler
from services.crypto_service import CryptoService
class CryptoHandler(BaseHandler):
"""加密货币价格处理器"""
def __init__(self, crypto_service: CryptoService):
self.crypto_service = crypto_service
def handle(self, pair: str = "ETH-USDT", **kwargs) -> List[Dict[str, Any]]:
"""处理加密货币价格请求"""
data = self.crypto_service.get_cached_data(f"{pair.lower()}_price", pair=pair)
if not data:
return [{
"title": f"{pair} 价格获取失败",
"description": "无法获取当前价格,请稍后重试",
"link": "https://www.okx.com",
"pub_date": datetime.now().strftime("%a, %d %b %Y %H:%M:%S +0800"),
"guid": f"{pair}-error-{int(datetime.now().timestamp())}"
}]
# 格式化价格信息
price = data['price']
change_24h = data['change_24h']
change_symbol = "📈" if change_24h >= 0 else "📉"
title = f"{pair.replace('-', '/')} 当前价格: ${price:,.4f}"
description = f"""
🔸 当前价格: ${price:,.4f}
🔸 24小时涨跌: {change_symbol} {change_24h:+.2f}%
🔸 24小时最高: ${data['high_24h']:,.4f}
🔸 24小时最低: ${data['low_24h']:,.4f}
🔸 24小时成交量: {data['volume_24h']:,.2f}
🔸 更新时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
""".strip()
return [{
"title": title,
"description": description,
"link": f"https://www.okx.com/trade-spot/{pair.lower()}",
"pub_date": datetime.now().strftime("%a, %d %b %Y %H:%M:%S +0800"),
"guid": f"{pair}-{int(datetime.now().timestamp())}"
}]
def get_handler_name(self) -> str:
return "crypto"

30
handlers/registry.py Normal file
View file

@ -0,0 +1,30 @@
from typing import Dict,List
from .base_handler import BaseHandler
from .crypto_handler import CryptoHandler
from services.crypto_service import CryptoService
class HandlerRegistry:
"""处理器注册中心"""
def __init__(self):
self.handlers: Dict[str, BaseHandler] = {}
self._register_default_handlers()
def _register_default_handlers(self):
"""注册默认处理器"""
# 注册加密货币处理器
crypto_service = CryptoService()
crypto_handler = CryptoHandler(crypto_service)
self.register_handler(crypto_handler)
def register_handler(self, handler: BaseHandler):
"""注册处理器"""
self.handlers[handler.get_handler_name()] = handler
def get_handler(self, handler_name: str) -> BaseHandler:
"""获取处理器"""
return self.handlers.get(handler_name)
def list_handlers(self) -> List[str]:
"""列出所有可用的处理器"""
return list(self.handlers.keys())