operit_loader.py: - OkHttp 新增 newClient()(真实市场插件普遍使用,而非 newBuilder) - OkHttpRequest.execute() 改为 async,与 Operit 运行时异步语义对齐 - OkHttpResponse 新增 json() 与 statusMessage()(基于 http.HTTPStatus) operit_translate.py(新增): - Operit .ts 插件 -> xiaoli-cli .py 脚手架生成器 - 自动提取 METADATA、生成 exports 胶水,原 TS 作参考注释保留 plugins/operit_scripts/ 新增 5 个已跑通的真实插件: - market_time 时间查询/格式化 - market_duckduckgo 网页内容抓取与清洗 - market_crossref Crossref 学术文献检索 - market_network_test HTTP 连通性测试 - market_reader 本地代码递归搜索 注: 工具需注册为 async 包装函数,同步 lambda 会导致 complete() 不执行的假阳性
104 lines
3.0 KiB
Python
104 lines
3.0 KiB
Python
# METADATA
|
||
# {
|
||
# "name": "market_time",
|
||
# "display_name": {
|
||
# "zh": "时间(市场插件翻译)",
|
||
# "en": "Time (translated from operit market)"
|
||
# },
|
||
# "description": {
|
||
# "zh": "从 operit 市场 time.ts 忠实翻译:获取/格式化当前时间。",
|
||
# "en": "Faithful translation of operit market time.ts: get/format current time."
|
||
# },
|
||
# "category": "Utility",
|
||
# "tools": [
|
||
# {
|
||
# "name": "get_time",
|
||
# "description": {
|
||
# "zh": "获取当前时间各字段",
|
||
# "en": "Get current time fields"
|
||
# },
|
||
# "parameters": []
|
||
# },
|
||
# {
|
||
# "name": "format_time",
|
||
# "description": {
|
||
# "zh": "格式化当前时间为 24h/12h 字符串",
|
||
# "en": "Format current time as 24h/12h strings"
|
||
# },
|
||
# "parameters": []
|
||
# }
|
||
# ]
|
||
# }
|
||
|
||
import datetime
|
||
|
||
|
||
def getErrorMessage(error):
|
||
return str(error) if error else "unknown"
|
||
|
||
|
||
def getErrorStack(error):
|
||
import traceback
|
||
return traceback.format_exc()
|
||
|
||
|
||
async def wrap(func, params, ok_msg="操作成功", err_msg="操作失败"):
|
||
try:
|
||
result = await func(params)
|
||
complete({"success": True, "message": ok_msg, "data": result})
|
||
except Exception as error:
|
||
console.error(f"Error: {getErrorMessage(error)}")
|
||
complete({"success": False, "message": getErrorMessage(error), "error_stack": getErrorStack(error)})
|
||
|
||
|
||
async def get_time(params):
|
||
"""获取当前时间各字段(忠实于 time.ts get_time)"""
|
||
now = datetime.datetime.now()
|
||
return {
|
||
"timestamp": int(now.timestamp() * 1000),
|
||
"iso": now.isoformat(),
|
||
"local": now.strftime("%Y-%m-%d %H:%M:%S"),
|
||
"date": {
|
||
"year": now.year,
|
||
"month": now.month,
|
||
"day": now.day,
|
||
"weekday": now.strftime("%A"),
|
||
},
|
||
"time": {
|
||
"hours": now.hour,
|
||
"minutes": now.minute,
|
||
"seconds": now.second,
|
||
},
|
||
}
|
||
|
||
|
||
async def format_time(params):
|
||
"""格式化当前时间为 24h/12h 字符串(忠实于 time.ts format_time)"""
|
||
now = datetime.datetime.now()
|
||
pad = lambda n: str(n).zfill(2)
|
||
hours = now.hour
|
||
minutes = now.minute
|
||
seconds = now.second
|
||
time24h = f"{pad(hours)}:{pad(minutes)}:{pad(seconds)}"
|
||
suffix = "PM" if hours >= 12 else "AM"
|
||
hour12 = 12 if hours % 12 == 0 else hours % 12
|
||
time12h = f"{pad(hour12)}:{pad(minutes)}:{pad(seconds)} {suffix}"
|
||
return {
|
||
"iso": now.isoformat(),
|
||
"date": f"{now.year}-{pad(now.month)}-{pad(now.day)}",
|
||
"time24h": time24h,
|
||
"time12h": time12h,
|
||
}
|
||
|
||
|
||
async def get_time_wrapper(params):
|
||
await wrap(get_time, params, "时间获取完成", "时间获取失败")
|
||
|
||
|
||
async def format_time_wrapper(params):
|
||
await wrap(format_time, params, "时间格式化完成", "时间格式化失败")
|
||
|
||
|
||
exports.get_time = get_time_wrapper
|
||
exports.format_time = format_time_wrapper
|