Spaces:
Running
Running
增加插件更新提醒
Browse files- router_items.py +23 -0
- 云端_定时版本检测引擎.py +115 -0
router_items.py
CHANGED
|
@@ -379,6 +379,29 @@ async def check_github_updates():
|
|
| 379 |
"updated_count": updated_count
|
| 380 |
}
|
| 381 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 382 |
@router.get("/api/item/{item_id}/version")
|
| 383 |
async def get_item_version(item_id: str):
|
| 384 |
"""获取单个资源的最新版本号"""
|
|
|
|
| 379 |
"updated_count": updated_count
|
| 380 |
}
|
| 381 |
|
| 382 |
+
@router.get("/api/items/{item_id}")
|
| 383 |
+
async def get_item_by_id(item_id: str):
|
| 384 |
+
"""根据ID获取单个资源的详细信息"""
|
| 385 |
+
items_db = db.load_data("items.json", default_data=[])
|
| 386 |
+
comments_db = db.load_data("comments.json", default_data={})
|
| 387 |
+
versions_db = db.load_data("versions.json", default_data={})
|
| 388 |
+
|
| 389 |
+
for item in items_db:
|
| 390 |
+
if item["id"] == item_id:
|
| 391 |
+
# 添加关联数据
|
| 392 |
+
item["commentsData"] = comments_db.get(item_id, [])
|
| 393 |
+
item["comments"] = len(item["commentsData"])
|
| 394 |
+
item["latest_version"] = versions_db.get(item_id, "")
|
| 395 |
+
|
| 396 |
+
# 🔴 【安全防线】:抹除敏感信息
|
| 397 |
+
item.pop("github_token", None)
|
| 398 |
+
item.pop("netdisk_password", None)
|
| 399 |
+
item.pop("viewed_by", None)
|
| 400 |
+
|
| 401 |
+
return {"status": "success", "data": item}
|
| 402 |
+
|
| 403 |
+
raise HTTPException(status_code=404, detail="资源不存在")
|
| 404 |
+
|
| 405 |
@router.get("/api/item/{item_id}/version")
|
| 406 |
async def get_item_version(item_id: str):
|
| 407 |
"""获取单个资源的最新版本号"""
|
云端_定时版本检测引擎.py
CHANGED
|
@@ -5,6 +5,9 @@ from datetime import date
|
|
| 5 |
import httpx
|
| 6 |
import logging
|
| 7 |
import 数据库连接 as db
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
logger = logging.getLogger("ComfyUI-Ranking.VersionCheck")
|
| 10 |
|
|
@@ -45,6 +48,108 @@ async def fetch_latest_github_hash(repo_url, token):
|
|
| 45 |
logger.error(f"检测版本失败 {repo_url}: {e}")
|
| 46 |
return None
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
async def daily_version_check_task():
|
| 49 |
"""每日 02:00 定时执行的守护进程"""
|
| 50 |
while True:
|
|
@@ -63,6 +168,8 @@ async def daily_version_check_task():
|
|
| 63 |
versions_db = db.load_data("versions.json", default_data={})
|
| 64 |
|
| 65 |
updated_count = 0
|
|
|
|
|
|
|
| 66 |
for item in items_db:
|
| 67 |
link = item.get("link", "")
|
| 68 |
if "github.com" in link:
|
|
@@ -71,10 +178,18 @@ async def daily_version_check_task():
|
|
| 71 |
if latest_hash and versions_db.get(item["id"]) != latest_hash:
|
| 72 |
versions_db[item["id"]] = latest_hash
|
| 73 |
updated_count += 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
if updated_count > 0:
|
| 76 |
db.save_data("versions.json", versions_db)
|
| 77 |
print(f"✅ 版本检测任务完成,共发现并更新了 {updated_count} 个资源的新版本。")
|
|
|
|
|
|
|
| 78 |
else:
|
| 79 |
print("✅ 版本检测任务完成,暂无任何新版本发现。")
|
| 80 |
|
|
|
|
| 5 |
import httpx
|
| 6 |
import logging
|
| 7 |
import 数据库连接 as db
|
| 8 |
+
from notifications import add_notification
|
| 9 |
+
from database_sql import SessionLocal
|
| 10 |
+
from models_sql import Ownership
|
| 11 |
|
| 12 |
logger = logging.getLogger("ComfyUI-Ranking.VersionCheck")
|
| 13 |
|
|
|
|
| 48 |
logger.error(f"检测版本失败 {repo_url}: {e}")
|
| 49 |
return None
|
| 50 |
|
| 51 |
+
async def trigger_update_notifications(updated_items: list):
|
| 52 |
+
"""
|
| 53 |
+
向已购买/已安装插件的用户发送更新通知
|
| 54 |
+
|
| 55 |
+
参数:
|
| 56 |
+
updated_items: 有更新的插件列表,每项包含 id, title, version_hash
|
| 57 |
+
"""
|
| 58 |
+
if not updated_items:
|
| 59 |
+
return
|
| 60 |
+
|
| 61 |
+
session = None
|
| 62 |
+
try:
|
| 63 |
+
# 加载已通知记录,防止重复通知
|
| 64 |
+
update_notifications_db = db.load_data("update_notifications.json", default_data={})
|
| 65 |
+
|
| 66 |
+
# 构建插件ID到最新版本号的映射
|
| 67 |
+
updated_versions = {item["id"]: item["version_hash"] for item in updated_items}
|
| 68 |
+
|
| 69 |
+
# 筛选出需要通知的插件(版本号变化才通知)
|
| 70 |
+
items_to_notify = []
|
| 71 |
+
for item in updated_items:
|
| 72 |
+
item_id = item["id"]
|
| 73 |
+
current_version = item["version_hash"]
|
| 74 |
+
last_notified_version = update_notifications_db.get(item_id)
|
| 75 |
+
|
| 76 |
+
if last_notified_version != current_version:
|
| 77 |
+
items_to_notify.append(item)
|
| 78 |
+
|
| 79 |
+
if not items_to_notify:
|
| 80 |
+
print("📢 所有插件更新已通知过,无需重复发送")
|
| 81 |
+
return
|
| 82 |
+
|
| 83 |
+
# 从 SQL 数据库查询购买记录
|
| 84 |
+
session = SessionLocal()
|
| 85 |
+
|
| 86 |
+
# 为每个需要通知的插件,查找已购买用户并发送通知
|
| 87 |
+
total_notified = 0
|
| 88 |
+
batch_size = 50 # 分批发送,每批50个用户
|
| 89 |
+
|
| 90 |
+
for item in items_to_notify:
|
| 91 |
+
item_id = item["id"]
|
| 92 |
+
title = item.get("title", "未知插件")
|
| 93 |
+
version_hash = item["version_hash"]
|
| 94 |
+
|
| 95 |
+
# 从 SQL ownerships 表查询已购买该插件的用户(排除已退款记录)
|
| 96 |
+
ownerships = session.query(Ownership).filter(
|
| 97 |
+
Ownership.item_id == item_id,
|
| 98 |
+
Ownership.is_refunded == False
|
| 99 |
+
).all()
|
| 100 |
+
|
| 101 |
+
# 使用 set 去重用户账号
|
| 102 |
+
target_users = list(set(record.account for record in ownerships if record.account))
|
| 103 |
+
|
| 104 |
+
if not target_users:
|
| 105 |
+
# 无人购买,但仍记录已通知版本
|
| 106 |
+
update_notifications_db[item_id] = version_hash
|
| 107 |
+
continue
|
| 108 |
+
|
| 109 |
+
# 准备通知数据
|
| 110 |
+
notif_data = {
|
| 111 |
+
"type": "plugin_update",
|
| 112 |
+
"from_user": "system",
|
| 113 |
+
"target_item_id": item_id,
|
| 114 |
+
"target_item_title": title,
|
| 115 |
+
"content": f"您已安装的插件 [{title}] 有新版本可用"
|
| 116 |
+
}
|
| 117 |
+
|
| 118 |
+
# 分批发送通知
|
| 119 |
+
notified_count = 0
|
| 120 |
+
for i in range(0, len(target_users), batch_size):
|
| 121 |
+
batch = target_users[i:i + batch_size]
|
| 122 |
+
|
| 123 |
+
for account in batch:
|
| 124 |
+
try:
|
| 125 |
+
add_notification(account, notif_data)
|
| 126 |
+
notified_count += 1
|
| 127 |
+
except Exception as e:
|
| 128 |
+
logger.error(f"发送更新通知失败 (user={account}, item={item_id}): {e}")
|
| 129 |
+
# 单个失败不影响其他用户
|
| 130 |
+
|
| 131 |
+
# 每批之间短暂休眠,避免通知风暴
|
| 132 |
+
if i + batch_size < len(target_users):
|
| 133 |
+
await asyncio.sleep(0.5)
|
| 134 |
+
|
| 135 |
+
# 记录该插件已通知的版本
|
| 136 |
+
update_notifications_db[item_id] = version_hash
|
| 137 |
+
total_notified += notified_count
|
| 138 |
+
print(f"📢 插件 [{title}] 已向 {notified_count} 位用户发送更新通知")
|
| 139 |
+
|
| 140 |
+
# 保存已通知记录
|
| 141 |
+
db.save_data("update_notifications.json", update_notifications_db)
|
| 142 |
+
print(f"✅ 更新通知发送完成,共通知 {total_notified} 位用户,涉及 {len(items_to_notify)} 个插件")
|
| 143 |
+
|
| 144 |
+
except Exception as e:
|
| 145 |
+
logger.error(f"触发更新通知时发生错误: {e}")
|
| 146 |
+
# 不抛出异常,让主流程继续
|
| 147 |
+
finally:
|
| 148 |
+
# 确保 session 正确关闭
|
| 149 |
+
if session:
|
| 150 |
+
session.close()
|
| 151 |
+
|
| 152 |
+
|
| 153 |
async def daily_version_check_task():
|
| 154 |
"""每日 02:00 定时执行的守护进程"""
|
| 155 |
while True:
|
|
|
|
| 168 |
versions_db = db.load_data("versions.json", default_data={})
|
| 169 |
|
| 170 |
updated_count = 0
|
| 171 |
+
updated_items = [] # 记录有更新的插件信息
|
| 172 |
+
|
| 173 |
for item in items_db:
|
| 174 |
link = item.get("link", "")
|
| 175 |
if "github.com" in link:
|
|
|
|
| 178 |
if latest_hash and versions_db.get(item["id"]) != latest_hash:
|
| 179 |
versions_db[item["id"]] = latest_hash
|
| 180 |
updated_count += 1
|
| 181 |
+
# 记录更新的插件信息
|
| 182 |
+
updated_items.append({
|
| 183 |
+
"id": item["id"],
|
| 184 |
+
"title": item.get("title", "未知插件"),
|
| 185 |
+
"version_hash": latest_hash
|
| 186 |
+
})
|
| 187 |
|
| 188 |
if updated_count > 0:
|
| 189 |
db.save_data("versions.json", versions_db)
|
| 190 |
print(f"✅ 版本检测任务完成,共发现并更新了 {updated_count} 个资源的新版本。")
|
| 191 |
+
# 触发更新通知
|
| 192 |
+
await trigger_update_notifications(updated_items)
|
| 193 |
else:
|
| 194 |
print("✅ 版本检测任务完成,暂无任何新版本发现。")
|
| 195 |
|