# -*- coding: utf-8 -*- """Classify Shorts/TikTok job errors into actionable buckets for UI.""" from __future__ import annotations from typing import Any def classify_job_error(error: str = "", *, platform: str = "") -> dict[str, Any]: text = str(error or "").strip() low = text.lower() plat = str(platform or "").strip().lower() def hit(*needles: str) -> bool: return any(n in low or n in text for n in needles) if not text: return { "code": "ok", "label": "正常", "action": "", "action_label": "", "severity": False, } if hit("cookie", "sessionid", "登录", "login expired", "未配置 cookie", "会话"): return { "code": "cookie", "label": "Cookie / 登录失效", "action": "update_cookie", "action_label": "更新 Cookie", "severity": False, } if hit("2158", "限流", "too fast", "take a rest", "spam", "rate_limit", "rate limit", "官方限流"): return { "code": "rate_limit", "label": "平台限流", "action": "wait_retry", "action_label": "等待后自动试传", "severity": True, } if hit("quota", "403", "daily limit", "upload limit", "youtube.*quota", "exceeded"): return { "code": "quota", "label": "平台配额用尽", "action": "wait_quota", "action_label": "等配额重置或换 Client", "severity": True, } if hit("成片文件不存在", "clip", "no such file", "过小", "too small", "empty file", "录制失败"): return { "code": "clip", "label": "成片缺失/损坏", "action": "rerecord", "action_label": "重新录制", "severity": True, } if hit("黑屏", "静音", "质量不合格", "quality"): return { "code": "quality", "label": "成片质量不合格", "action": "rerecord", "action_label": "换时段重录", "severity": True, } if hit("stream", "直播已结束", "not live", "404", "拉流", "cdn"): return { "code": "stream", "label": "直播流不可用", "action": "skip_or_retry", "action_label": "跳过或换房", "severity": False, } if hit("socks", "proxy", "代理"): return { "code": "proxy", "label": "代理/网络异常", "action": "check_socks", "action_label": "检查 SOCKS", "retryable": True, } if hit("oauth", "token", "unauthorized", "401"): return { "code": "oauth", "label": "OAuth 授权失效", "action": "reauth", "action_label": "重新授权", "retryable": False, } if plat == "tiktok" and hit( "signature", "playwright", "headless_shell", "executable doesn't exist", "failed to parse signature", "failed to generate signatures", ): return { "code": "tiktok_signature", "label": "TikTok 签名失败", "action": "retry", "action_label": "重试", "retryable": True, } # Do NOT match bare "Datacenter Assigned" login banner — that line appears on every upload. if plat == "tiktok" and hit("机房不匹配", "tt-target-idc", "会话机房", "cookie 无效或机房"): return { "code": "dc", "label": "TikTok 机房不匹配", "action": "update_cookie", "action_label": "重贴 Cookie + 机房", "retryable": False, } return { "code": "unknown", "label": "其他错误", "action": "retry", "action_label": "重试", "retryable": True, "detail": text[:200], } def enrich_job_error(job: dict[str, Any] | None, *, platform: str = "") -> dict[str, Any] | None: if not job: return None out = dict(job) err = str(out.get("error") or "") info = classify_job_error(err, platform=platform) out["error_class"] = info return out