Files
douyi_guaji_main/douyin/utils/douyin_utils.py
T
Your Name 0b5f150691 Refactor and clean up code across multiple scripts
- Removed trailing whitespace and ensured files end with a single newline in cleanup_unused_files.py.
- Added fix_trailing_whitespace.py to batch remove trailing whitespace from Python files.
- Updated install_dependencies.py to improve dependency installation prompts.
- Cleaned up pick_coords.py by removing unnecessary whitespace and improving code readability.
- Enhanced region_selector.py by disabling specific pylint checks for better compatibility with third-party libraries.
- Improved WPS流程图生成工具.py by cleaning up whitespace and enhancing readability.
- Updated __init__.py in typings to remove unnecessary lines.
- Modified app.js to adjust default configuration values and clean up unused code.
2026-01-02 23:57:03 +08:00

183 lines
5.5 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.
import os
import re
import unicodedata
def normalize_countdown_text(text: str) -> str:
"""对福袋倒计时类 OCR 文本做标准化,抽离自 douyin_fudai.py,方便复用与单元测试。"""
if not text:
return ""
t = re.sub(r"\s+", "", str(text))
t = t.replace(":", ":")
# 常见 OCR 混淆纠正(只做轻量替换,避免影响其它文字)
t = t.translate(
str.maketrans(
{
"O": "0",
"o": "0",
"Q": "0",
"D": "0",
"I": "1",
"l": "1",
"|": "1",
"!": "1",
"S": "5",
"s": "5",
"B": "8",
"G": "6",
"Z": "2",
}
)
)
# 中文:x分y秒 / y秒
m = re.search(r"(\d{1,2})分(\d{1,2})秒", t)
if m:
mm = int(m.group(1))
ss = int(m.group(2))
if ss >= 60:
mm += ss // 60
ss = ss % 60
return f"{mm}:{ss:02d}"
# 仅分钟:x分钟
m = re.search(r"(\d{1,2})分钟", t)
if m:
mm = int(m.group(1))
return f"{mm}:00"
m = re.search(r"(\d{1,3})秒", t)
if m:
ss = int(m.group(1))
mm = ss // 60
ss = ss % 60
return f"{mm}:{ss:02d}"
# 标准样式:11:37后开奖 / 11:37后开奖
m = re.search(r"(\d{1,2})[::](\d{2}).*后开奖", t)
if m:
mm = int(m.group(1))
ss = int(m.group(2))
# 若分秒识别反了(例如 53:08 实际应为 08:53),优先选择 <= 15 分钟的解释
# 或者秒数不合法(>=60),也尝试交换
total1 = mm * 60 + ss
total2 = ss * 60 + mm
if (total1 > 900 and total2 <= 900) or (ss >= 60 and mm < 60):
mm, ss = ss, mm
return f"{mm}:{ss:02d}后开奖"
# 新的兜底解析路径:处理诸如 14后开奖、1413后开奖、1304后开奖等无分割符的情况
m = re.search(r"(\d{1,2})(\d{2})后开奖", t)
if m:
mm = int(m.group(1))
ss = int(m.group(2))
return f"{mm}:{ss:02d}后开奖"
# 识别错序:54后开奖04
m = re.search(r"(\d{1,2})后开奖(\d{2})", t)
if m:
mm = int(m.group(1))
ss = int(m.group(2))
if (mm * 60 + ss) > 900 and (ss * 60 + mm) <= 900:
mm, ss = ss, mm
return f"{mm}:{ss:02d}后开奖"
# 纯 mm:ss 格式(不带"后开奖"):11:17 / 05:11
m = re.search(r"^(\d{1,2})[::](\d{1,2})$", t)
if m:
mm = int(m.group(1))
ss = int(m.group(2))
# 检查是否需要交换分秒:
# 1. 秒数不合法(>=60)且交换后合法
# 2. 总时间>15分钟且交换后<=15分钟
total1 = mm * 60 + ss
total2 = ss * 60 + mm
if (ss >= 60 and mm < 60) or (total1 > 900 and total2 <= 900):
mm, ss = ss, mm
# 如果秒数仍然>=60,进行进位处理
if ss >= 60:
mm += ss // 60
ss = ss % 60
return f"{mm}:{ss:02d}"
# 纯数字 1137 / 5404
digits = "".join(ch for ch in t if ch.isdigit())
if len(digits) == 4:
a = int(digits[:2])
b = int(digits[2:])
# 生成两种解释:mm:ss 以及交换后的 mm:ss
mm1, ss1 = a, b
mm2, ss2 = b, a
total1 = mm1 * 60 + ss1
total2 = mm2 * 60 + ss2
# 默认相信“从左到右”的顺序:前两位=分,后两位=秒。
# 只有在明显异常(>15分钟)或秒数不合法时才交换。
if ss1 >= 60 and ss2 < 60:
mm, ss = mm2, ss2
elif ss2 >= 60 and ss1 < 60:
mm, ss = mm1, ss1
elif total1 > 900 and total2 <= 900:
mm, ss = mm2, ss2
elif total2 > 900 and total1 <= 900:
mm, ss = mm1, ss1
else:
mm, ss = mm1, ss1
return f"{mm}:{ss:02d}"
# 3 位数字:934 => 09:34
if len(digits) == 3:
mm = int(digits[0])
ss = int(digits[1:])
if ss >= 60:
# 尝试 15:09(OCR 丢 0)
mm2 = int(digits[:2])
ss2 = int(digits[2])
if ss2 < 60:
return f"{mm2:02d}:{ss2:02d}"
mm += ss // 60
ss = ss % 60
return f"{mm:02d}:{ss:02d}"
# 2 位数字:31 => 00:31(更符合临近开奖)
if len(digits) == 2:
return f"00:{int(digits):02d}"
return t
def extract_prize_value_yuan(text: str) -> float:
"""从 OCR 文本中抽取奖品价值金额(元),抽离自 douyin_fudai.py。"""
if not text:
return 0.0
s = unicodedata.normalize("NFKC", str(text))
values: list[float] = []
for idx, ch in enumerate(s):
if ch != "¥":
continue
j = idx + 1
while j < len(s) and s[j].isspace():
j += 1
start = j
has_digit = False
dot_seen = False
while j < len(s):
cj = s[j]
if cj.isdigit():
has_digit = True
j += 1
continue
if cj == ".":
if dot_seen:
break
dot_seen = True
j += 1
continue
if cj == ",":
j += 1
continue
break
if not has_digit:
continue
num_text = s[start:j].replace(",", "")
try:
values.append(float(num_text))
except ValueError:
continue
return max(values) if values else 0.0