44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""邮件通知 (复用 send_email.py 技能脚本)"""
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
SEND_EMAIL = os.path.join(
|
|
os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
|
|
"skills", "send_email.py",
|
|
)
|
|
|
|
|
|
def notify_email(task, run):
|
|
"""发送完成通知邮件, 返回 (bool, msg)"""
|
|
if not os.path.exists(SEND_EMAIL):
|
|
return False, "send_email.py 不存在"
|
|
cfg = task.get("config", {})
|
|
to = cfg.get("notify_email") or "wlq@tphai.com"
|
|
results = run.get("results", [])
|
|
ok = sum(1 for r in results if r.get("status") == "OK")
|
|
fail = len(results) - ok
|
|
lines = [
|
|
f"项目: {task['name']}",
|
|
f"模式: {task['mode']}",
|
|
f"状态: {run.get('status')}",
|
|
f"成功: {ok} 失败: {fail} 图片: {run.get('stats', {}).get('images', 0)}",
|
|
f"输出目录: {run.get('out_dir', '')}",
|
|
"",
|
|
"明细:",
|
|
]
|
|
for r in results[:50]:
|
|
lines.append(f" [{r.get('status')}] {r.get('title', '')[:40]} {r.get('url', '')}")
|
|
if len(results) > 50:
|
|
lines.append(f" ... 共 {len(results)} 条")
|
|
body = "\n".join(lines)
|
|
try:
|
|
r = subprocess.run(
|
|
[sys.executable, SEND_EMAIL, f"[爬虫完成] {task['name']}", body, "--to", to],
|
|
timeout=60, capture_output=True,
|
|
)
|
|
return r.returncode == 0, (r.stderr or b"").decode(errors="ignore")[:200]
|
|
except Exception as e:
|
|
return False, str(e)
|