Files
douyin_guaji-main/fudai_detail_modules/fudai_detail_validator.py
T

356 lines
15 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
"""
福袋详情数据验证模块
====================
验证福袋详情数据的合理性,检测识别错误。
"""
from typing import Any, Tuple
import re
import os
import json
import path_config
def validate_fudai_detail(detail: Tuple, analyser: Any = None) -> Tuple[bool, str]:
"""验证福袋详情数据的合理性。
返回:
(is_valid, reason) - is_valid=True 表示数据合理,False 表示识别出错
reason 为失败原因描述
"""
if not isinstance(detail, tuple) or len(detail) < 6:
return False, "detail_tuple_invalid"
content = str(detail[0] or "").strip()
time_text = str(detail[1] or "").strip()
try:
participants = int(detail[2] or 0)
except Exception:
participants = 0
try:
copies = int(detail[3] or 0)
except Exception:
copies = 0
try:
prize_value_yuan = float(detail[4] or 0.0)
except Exception:
prize_value_yuan = 0.0
countdown_tuple = detail[5]
countdown_start, countdown_end = None, None
if isinstance(countdown_tuple, (tuple, list)) and len(countdown_tuple) >= 2:
try:
countdown_start = float(countdown_tuple[0] or 0.0)
countdown_end = float(countdown_tuple[1] or 0.0)
except Exception:
pass
# 检查1:内容不能为空(至少要有一些文字)
# 如果运行时显式禁用了内容识别,则跳过此检查(允许内容为空)
try:
disable_content = False
if analyser:
disable_content = bool(
getattr(analyser, "fudai_disable_content_recognition", False)
)
# 如果 analyser 未设置该属性,再尝试从 config.json 读取全局配置
if not disable_content:
try:
cfg_path = path_config.get_config_path()
if cfg_path and os.path.exists(cfg_path):
with open(cfg_path, "r", encoding="utf-8") as f:
cfg = json.load(f)
disable_content = bool(
cfg.get("fudai_disable_content_recognition", False)
)
except Exception:
pass
if not disable_content:
if not content or len(content.strip()) < 2:
return False, "content_empty_or_too_short"
except Exception:
if not content or len(content.strip()) < 2:
return False, "content_empty_or_too_short"
# 检查2:价值应该大于0(福袋至少应该有价值)
if prize_value_yuan <= 0:
return False, "prize_value_zero_or_negative"
# 检查3:价值应该合理(不能太大,比如超过100万)
if prize_value_yuan > 1000000:
return False, "prize_value_too_large"
# 检查4:倒计时应该合理
if countdown_end is not None and countdown_start is not None:
remain_seconds = countdown_end - countdown_start
# 倒计时不能是负数
if remain_seconds < 0:
return False, "countdown_negative"
# 倒计时不能太长(超过24小时)
if remain_seconds > 24 * 3600:
return False, "countdown_too_long"
# 倒计时不能为0(至少应该有几秒)
if remain_seconds == 0 and not time_text:
return False, "countdown_zero_and_no_time_text"
# 倒计时合理性检查:如果 time_text 存在,验证倒计时与 time_text 是否一致
# 直接参与标志已移除:始终执行倒计时与时间文本一致性检查
force_attend = False
if time_text and not force_attend:
try:
# 解析 time_text 为秒数
m = re.search(r"(\d{1,2})[::](\d{1,2})", time_text)
if m:
mm = int(m.group(1))
ss = int(m.group(2))
# 如果秒数>=60,尝试交换分秒
if ss >= 60 and mm < 60:
mm, ss = ss, mm
# 如果秒数仍然>=60,进行进位
if ss >= 60:
mm += ss // 60
ss = ss % 60
text_seconds = mm * 60 + ss
# 允许一定误差(±10%),但差异过大则判定为识别错误
if text_seconds > 0:
diff_ratio = abs(remain_seconds - text_seconds) / max(
text_seconds, 1.0
)
if diff_ratio > 0.15: # 差异超过15%视为不一致
return (
False,
f"countdown_mismatch_time_text (remain={int(remain_seconds)}, text={text_seconds}, diff={round(diff_ratio*100, 1)}%)",
)
except Exception:
pass
# 检查5:参与人数应该合理(不能是负数)
if participants < 0:
return False, "participants_negative"
# 检查6:份数应该合理(不能是负数,至少应该是1)
if copies < 0:
return False, "copies_negative"
# 检查7:如果份数为0,但参与人数大于0,可能识别错误
if copies == 0 and participants > 0:
return False, "copies_zero_but_participants_positive"
# 检查8:时间文本应该存在(至少应该有倒计时信息)
if not time_text and (countdown_end is None or countdown_end <= 0):
return False, "no_time_text_and_no_countdown"
# 检查9:验证并行OCR识别的额外字段(如果存在且功能启用)
if analyser:
# 获取配置:检查是否禁用某些识别功能
try:
disable_content_recognition = bool(
getattr(analyser, "fudai_disable_content_recognition", False)
)
# 如果 analyser 未设置该属性,再尝试从 config.json 读取全局配置
if not disable_content_recognition:
try:
cfg_path = path_config.get_config_path()
if cfg_path and os.path.exists(cfg_path):
with open(cfg_path, "r", encoding="utf-8") as f:
cfg = json.load(f)
disable_content_recognition = bool(
cfg.get("fudai_disable_content_recognition", False)
)
except Exception:
pass
except Exception:
disable_content_recognition = False
try:
disable_task_recognition = bool(
getattr(analyser, "fudai_disable_task_recognition", False)
)
# 如果 analyser 未设置该属性,再尝试从 config.json 读取全局配置
if not disable_task_recognition:
try:
cfg_path = path_config.get_config_path()
if cfg_path and os.path.exists(cfg_path):
with open(cfg_path, "r", encoding="utf-8") as f:
cfg = json.load(f)
disable_task_recognition = bool(
cfg.get("fudai_disable_task_recognition", False)
)
except Exception:
pass
except Exception:
disable_task_recognition = False
# 验证倒计时分钟和秒的合理性(如果倒计时识别功能启用)
try:
disable_countdown_recognition = bool(
getattr(analyser, "fudai_disable_countdown_recognition", False)
)
if not disable_countdown_recognition:
countdown_mm_raw = str(
getattr(analyser, "countdown_mm_raw", "") or ""
).strip()
countdown_ss_raw = str(
getattr(analyser, "countdown_ss_raw", "") or ""
).strip()
if countdown_mm_raw and countdown_ss_raw:
try:
mm = int(countdown_mm_raw)
ss = int(countdown_ss_raw)
if mm < 0 or mm > 59 or ss < 0 or ss > 59:
return False, "countdown_mm_ss_invalid_range"
# 如果有倒计时元组,验证是否一致
if countdown_end is not None and countdown_start is not None:
remain_seconds = countdown_end - countdown_start
expected_seconds = mm * 60 + ss
if (
abs(remain_seconds - expected_seconds) > 30
): # 允许30秒误差
return (
False,
f"countdown_mm_ss_mismatch_tuple (mm_ss={expected_seconds}s, tuple={int(remain_seconds)}s)",
)
except ValueError:
return False, "countdown_mm_ss_not_numeric"
except Exception:
pass
# 验证奖品价值原始文本的合理性(如果价值识别功能启用)
try:
disable_value_recognition = bool(
getattr(analyser, "fudai_disable_value_recognition", False)
)
if not disable_value_recognition:
value_raw = str(getattr(analyser, "value_raw", "") or "").strip()
if value_raw and prize_value_yuan > 0:
# 检查原始文本是否包含合理的价值信息
import re
# 查找数字模式(如:9999, 9999元, 价值9999等)
value_patterns = [
r"(\d+(?:\.\d+)?)", # 纯数字
r"价值[::\s]*(\d+(?:\.\d+)?)", # 价值XXX
r"(\d+(?:\.\d+)?)元", # XXX元
]
found_value = False
for pattern in value_patterns:
match = re.search(pattern, value_raw)
if match:
try:
extracted_value = float(match.group(1))
if (
abs(extracted_value - prize_value_yuan) < 1.0
): # 允许1元误差
found_value = True
break
except ValueError:
continue
if (
not found_value and len(value_raw) > 3
): # 只有当原始文本较长时才验证
return False, "value_raw_mismatch_parsed_value"
except Exception:
pass
# 验证按钮文本的合理性(如果按钮识别功能启用)
try:
disable_button_recognition = bool(
getattr(analyser, "fudai_disable_button_recognition", False)
)
if not disable_button_recognition:
button_text_raw = str(
getattr(analyser, "button_text_raw", "") or ""
).strip()
if button_text_raw:
# 检查按钮文本长度合理性
if len(button_text_raw) > 50:
return False, "button_text_too_long"
# 检查是否包含明显错误的字符
if any(char in button_text_raw for char in ["\n", "\r", "\t"] * 3):
return False, "button_text_contains_invalid_chars"
except Exception:
pass
# 验证直播间标题的合理性(如果标题识别功能启用)
try:
disable_title_recognition = bool(
getattr(analyser, "fudai_disable_title_recognition", False)
)
if not disable_title_recognition:
live_room_title_raw = str(
getattr(analyser, "live_room_title_raw", "") or ""
).strip()
if live_room_title_raw:
# 检查标题长度合理性
if len(live_room_title_raw) > 100:
return False, "live_room_title_too_long"
# 检查是否包含直播相关关键词(可选验证)
title_lower = live_room_title_raw.lower()
if len(live_room_title_raw) > 5 and not any(
keyword in title_lower
for keyword in ["直播", "live", "room", "间"]
):
# 不强制要求包含关键词,但记录警告
pass
except Exception:
pass
# 验证福袋内容的合理性(如果内容识别功能启用)
if not disable_content_recognition:
try:
fudai_content_raw = str(
getattr(analyser, "fudai_content_raw", "") or ""
).strip()
if fudai_content_raw and content:
# 检查原始内容和解析内容的基本一致性
content_lower = content.lower()
raw_lower = fudai_content_raw.lower()
# 如果解析内容是原始内容的子串,或者原始内容包含解析内容,认为是合理的
if not (content_lower in raw_lower or raw_lower in content_lower):
# 允许一定差异,但如果完全不同可能有问题
common_chars = set(content_lower) & set(raw_lower)
if (
len(common_chars) / max(len(content_lower), len(raw_lower))
< 0.3
):
return False, "fudai_content_raw_mismatch_parsed"
except Exception:
pass
# 验证任务列表的合理性(如果任务识别功能启用)
if not disable_task_recognition:
try:
task_list_raw = str(
getattr(analyser, "task_list_raw", "") or ""
).strip()
if task_list_raw:
# 检查任务列表长度合理性
if len(task_list_raw) > 500:
return False, "task_list_too_long"
# 检查是否包含任务相关关键词
task_keywords = [
"观看",
"关注",
"分享",
"评论",
"点赞",
"watch",
"follow",
"share",
"comment",
"like",
]
if len(task_list_raw) > 10 and not any(
keyword in task_list_raw for keyword in task_keywords
):
# 如果文本较长但不包含任务关键词,可能识别错误
return False, "task_list_no_task_keywords"
except Exception:
pass
# 所有检查通过
return True, "ok"