+108
@@ -646,6 +646,58 @@ def _update_chat_config(chat_id: str, download_filter: str, update_memory: bool
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
|
||||
def _update_chat_configs_multi(items: list, update_memory: bool = True) -> dict:
|
||||
"""
|
||||
多频道版本:把 items 数组写入 config["chat"],完整覆盖原数组。
|
||||
每个 item 形如 {"chat_id": "xxx", "download_filter": "xxx"}。
|
||||
同步更新内存里的 _app.chat_download_config。
|
||||
"""
|
||||
global _app
|
||||
if not items:
|
||||
return {"success": False, "error": "队列为空"}
|
||||
|
||||
try:
|
||||
yaml = YAML()
|
||||
yaml.preserve_quotes = True
|
||||
with open(CONFIG_FILE_PATH, "r", encoding="utf-8") as f:
|
||||
config = yaml.load(f)
|
||||
|
||||
# 按队列顺序重建 chat 数组
|
||||
new_chat_list = []
|
||||
for item in items:
|
||||
chat_id = str(item.get("chat_id", "")).strip()
|
||||
download_filter = str(item.get("download_filter", "") or "").strip()
|
||||
if not chat_id:
|
||||
continue
|
||||
entry = {"chat_id": chat_id, "last_read_message_id": 0}
|
||||
if download_filter:
|
||||
entry["download_filter"] = download_filter
|
||||
new_chat_list.append(entry)
|
||||
|
||||
if not new_chat_list:
|
||||
return {"success": False, "error": "队列中没有有效的频道"}
|
||||
|
||||
config["chat"] = new_chat_list
|
||||
|
||||
with open(CONFIG_FILE_PATH, "w", encoding="utf-8") as f:
|
||||
yaml.dump(config, f)
|
||||
|
||||
# 同步内存配置
|
||||
if update_memory and _app:
|
||||
_app.config["chat"] = new_chat_list
|
||||
_app.chat_download_config.clear()
|
||||
for entry in new_chat_list:
|
||||
c = ChatDownloadConfig()
|
||||
c.last_read_message_id = 0
|
||||
c.download_filter = entry.get("download_filter", "")
|
||||
_app.chat_download_config[entry["chat_id"]] = c
|
||||
|
||||
return {"success": True, "count": len(new_chat_list)}
|
||||
except Exception as e:
|
||||
logger.exception(f"Error updating multi chat config: {e}")
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
|
||||
async def _validate_chat(chat_id: str) -> dict:
|
||||
"""
|
||||
Validate chat/channel and get its info.
|
||||
@@ -804,6 +856,62 @@ def api_save_and_restart():
|
||||
return jsonify({"success": False, "error": str(e)})
|
||||
|
||||
|
||||
@_flask_app.route("/api/save_and_restart_multi", methods=["POST"])
|
||||
@login_required
|
||||
def api_save_and_restart_multi():
|
||||
"""多频道版本的保存并重启。接收 items 数组,每项含 chat_id + download_filter。"""
|
||||
global _app
|
||||
try:
|
||||
data = request.get_json() or {}
|
||||
items = data.get("items", [])
|
||||
if not isinstance(items, list) or not items:
|
||||
return jsonify({"success": False, "error": "队列为空"})
|
||||
|
||||
# 规范化 items:每项必须有 chat_id;download_filter 可由 start_date/end_date 构建
|
||||
normalized = []
|
||||
for it in items:
|
||||
cid = str(it.get("chat_id", "") or "").strip()
|
||||
if not cid:
|
||||
continue
|
||||
flt = str(it.get("download_filter", "") or "").strip()
|
||||
if not flt:
|
||||
sd = str(it.get("start_date", "") or "").strip()
|
||||
ed = str(it.get("end_date", "") or "").strip()
|
||||
if sd:
|
||||
flt = _build_download_filter(sd, ed)
|
||||
normalized.append({
|
||||
"chat_id": cid,
|
||||
"download_filter": flt,
|
||||
"chat_title": it.get("chat_title", "") or "",
|
||||
"chat_type": it.get("chat_type", "") or "",
|
||||
})
|
||||
|
||||
if not normalized:
|
||||
return jsonify({"success": False, "error": "没有有效的频道"})
|
||||
|
||||
result = _update_chat_configs_multi(normalized, update_memory=True)
|
||||
if not result.get("success"):
|
||||
return jsonify(result)
|
||||
|
||||
# 历史记录更新
|
||||
for it in normalized:
|
||||
_add_to_channel_history(it["chat_id"], it["chat_title"], it["chat_type"])
|
||||
|
||||
# 触发重启
|
||||
if _app:
|
||||
_app.restart_program = True
|
||||
logger.info(f"Restart flag set, {len(normalized)} chats queued")
|
||||
|
||||
return jsonify({
|
||||
"success": True,
|
||||
"count": len(normalized),
|
||||
"message": f"已保存 {len(normalized)} 个频道任务,正在重启..."
|
||||
})
|
||||
except Exception as e:
|
||||
logger.exception(f"Error in api_save_and_restart_multi: {e}")
|
||||
return jsonify({"success": False, "error": str(e)})
|
||||
|
||||
|
||||
@_flask_app.route("/api/start_download", methods=["POST"])
|
||||
@login_required
|
||||
def api_start_download():
|
||||
|
||||
Reference in New Issue
Block a user