add desc
This commit is contained in:
@@ -17,6 +17,8 @@ driver = nonebot.get_driver()
|
||||
def startup():
|
||||
if pconfig.proxy:
|
||||
rlib.set_proxy(pconfig.proxy)
|
||||
if pconfig.pjsk_proxy:
|
||||
rlib.set_pjsk_proxy(pconfig.pjsk_proxy)
|
||||
if pconfig.github_token:
|
||||
rlib.set_ghp(pconfig.github_token)
|
||||
try:
|
||||
|
||||
@@ -12,6 +12,7 @@ except:
|
||||
class Config(BaseModel):
|
||||
assets_studio_path: str
|
||||
github_token: str | None = None
|
||||
pjsk_proxy: str | None = None
|
||||
@property
|
||||
def proxy(self) -> str | None:
|
||||
return _proxy
|
||||
|
||||
@@ -2,6 +2,7 @@ import ctypes
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
from dataclasses import field, dataclass
|
||||
from typing import Optional, Dict, Any, List
|
||||
|
||||
|
||||
@@ -14,16 +15,33 @@ def get_lib_filename(base_name):
|
||||
return f"lib{base_name}.so"
|
||||
|
||||
|
||||
class FetchData:
|
||||
event_id: int
|
||||
card_paths: List[str]
|
||||
@dataclass
|
||||
class FetchResultCard:
|
||||
text: str = ""
|
||||
path: List[str] = field(default_factory=list)
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> 'FetchData':
|
||||
instance = cls()
|
||||
instance.event_id = data.get("event_id", 0)
|
||||
instance.card_paths = data.get("card_paths", [])
|
||||
return instance
|
||||
def from_dict(cls, data: Dict[str, Any]) -> 'FetchResultCard':
|
||||
return cls(
|
||||
text=data.get("text", ""),
|
||||
path=data.get("path", []),
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class FetchResult:
|
||||
event_id: int = 0
|
||||
cards: List[FetchResultCard] = field(default_factory=list)
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, data: Dict[str, Any]) -> 'FetchResult':
|
||||
return cls(
|
||||
event_id=data.get("event_id", 0),
|
||||
cards=[
|
||||
FetchResultCard.from_dict(card)
|
||||
for card in data.get("cards", [])
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
class SekaiSyncLib:
|
||||
@@ -36,6 +54,10 @@ class SekaiSyncLib:
|
||||
self.lib.proxy.argtypes = [ctypes.c_char_p]
|
||||
self.lib.proxy.restype = None
|
||||
|
||||
# pjsk_proxy(x: *const c_char)
|
||||
self.lib.pjsk_proxy.argtypes = [ctypes.c_char_p]
|
||||
self.lib.pjsk_proxy.restype = None
|
||||
|
||||
# ghp(x: *const c_char)
|
||||
self.lib.ghp.argtypes = [ctypes.c_char_p]
|
||||
self.lib.ghp.restype = None
|
||||
@@ -72,6 +94,10 @@ class SekaiSyncLib:
|
||||
"""设置代理,传 None 或空字符串可清除"""
|
||||
self.lib.proxy(self._str_to_bytes(proxy_url))
|
||||
|
||||
def set_pjsk_proxy(self, proxy_url: Optional[str]):
|
||||
"""设置 pjsk_proxy,传 None 或空字符串可清除"""
|
||||
self.lib.pjsk_proxy(self._str_to_bytes(proxy_url))
|
||||
|
||||
def set_ghp(self, token: Optional[str]):
|
||||
"""设置 GitHub Token,传 None 或空字符串可清除"""
|
||||
self.lib.ghp(self._str_to_bytes(token))
|
||||
@@ -96,7 +122,7 @@ class SekaiSyncLib:
|
||||
"""
|
||||
return self.lib.stop()
|
||||
|
||||
def fetch_data(self) -> Optional[FetchData]:
|
||||
def fetch_data(self) -> Optional[FetchResult]:
|
||||
# 1. 获取裸指针
|
||||
ptr = self.lib.fetch_data()
|
||||
|
||||
@@ -112,7 +138,7 @@ class SekaiSyncLib:
|
||||
|
||||
# 3. 解码并反序列化 JSON
|
||||
json_str = json_bytes.decode('utf-8')
|
||||
return FetchData.from_dict(json.loads(json_str))
|
||||
return FetchResult.from_dict(json.loads(json_str))
|
||||
|
||||
finally:
|
||||
# 4. 无论是否发生 JSON 解析异常,绝对保证释放 Rust 内存!
|
||||
|
||||
@@ -36,11 +36,13 @@ async def sync():
|
||||
return
|
||||
message = Message()
|
||||
inner = [MessageSegment.text(f"活动 {data.event_id} 的卡牌更新了!")]
|
||||
for item in data.card_paths:
|
||||
p = Path(item)
|
||||
n = MessageSegment.image(file=item)
|
||||
n.data["summary"] = f"[{p.name}]"
|
||||
inner.append(n)
|
||||
for card in data.cards:
|
||||
inner.append(MessageSegment.text(card.text))
|
||||
for item in card.path:
|
||||
p = Path(item)
|
||||
n = MessageSegment.image(file=item)
|
||||
n.data["summary"] = f"[{p.name}]"
|
||||
inner.append(n)
|
||||
for node in inner:
|
||||
message.append(MessageSegment.node_custom(
|
||||
user_id=int(get_bot().self_id),
|
||||
@@ -54,11 +56,12 @@ async def sync():
|
||||
except Exception as e:
|
||||
logger.error(f"发送消息到群组 {group} 失败: {e}")
|
||||
|
||||
for path in data.card_paths:
|
||||
try:
|
||||
Path(path).unlink()
|
||||
except Exception as e:
|
||||
logger.error(f"删除文件 {path} 失败: {e}")
|
||||
for card in data.cards:
|
||||
for path in card.path:
|
||||
try:
|
||||
Path(path).unlink()
|
||||
except Exception as e:
|
||||
logger.error(f"删除文件 {path} 失败: {e}")
|
||||
|
||||
|
||||
subscribe = on_command("订阅烤leak", permission=SUPERUSER)
|
||||
|
||||
Reference in New Issue
Block a user