Files
douyi_guaji_main/douyin_utils.py
T
Your Name 539e580147 feat: Add coverage report generation and enhance testing framework
- Introduced a new `.coverage` file for tracking test coverage metrics.
- Added `pytest.ini` configuration for streamlined testing setup, including coverage reporting and logging options.
- Created `requirements-test.txt` to manage testing dependencies, ensuring a consistent testing environment.
- Implemented a `run_tests.py` script to facilitate running tests with various options, including unit and integration tests.
- Established a structured testing directory with unit and integration tests, enhancing code reliability and maintainability.
- Updated documentation to reflect new testing procedures and configurations.
2025-12-22 13:26:26 +08:00

232 lines
6.8 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
from typing import Iterable
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}后开奖"
# 识别错序: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
t = re.sub(r"\s+", "", str(text))
# 先去掉倒计时类数字,避免把 00:31 解析成 31 元
t = re.sub(r"\b\d{1,2}[::]\d{2}\b", "", t)
# 优先匹配带币种符号/单位的金额:参考价值¥199 / ¥199.9 / ¥199 / 199元
currency_matches = re.findall(r"[¥¥](\d+(?:\.\d+)?)", t)
yuan_matches = re.findall(r"(\d+(?:\.\d+)?)元", t)
tagged: Iterable[str] = list(currency_matches) + list(yuan_matches)
if tagged:
vals: list[float] = []
for n in tagged:
try:
vals.append(float(n))
except ValueError:
pass
return max(vals) if vals else 0.0
# 去掉常见干扰:份数/参与人数
t = re.sub(r"共\d+份", "", t)
t = re.sub(r"\d+人已参与", "", t)
# 退化:抓小数
decimals = re.findall(r"\d+\.\d+", t)
if decimals:
vals = []
for n in decimals:
try:
vals.append(float(n))
except ValueError:
pass
return max(vals) if vals else 0.0
# 再退化:抓整数
ints = re.findall(r"\d+", t)
if ints:
vals = []
for n in ints:
try:
v = int(n)
except ValueError:
continue
if 1 <= v <= 99999:
vals.append(float(v))
return max(vals) if vals else 0.0
return 0.0
def parse_countdown_seconds(text: str) -> int:
"""
将福袋倒计时文本解析为总秒数。
这是对 ``normalize_countdown_text`` 的简单封装,方便在其它模块或测试中直接得到秒数。
示例::
>>> parse_countdown_seconds("5分30秒")
330
>>> parse_countdown_seconds("30秒")
30
>>> parse_countdown_seconds("5:00")
300
"""
norm = normalize_countdown_text(text)
if not norm:
return 0
# 期望格式 "mm:ss" 或 "mm:ss后开奖"
m = re.search(r"(\d{1,3}):(\d{1,2})", norm)
if not m:
return 0
try:
mm = int(m.group(1))
ss = int(m.group(2))
except ValueError:
return 0
return mm * 60 + ss
class Tee:
"""将 print 输出同时写入多个文件对象的简单工具类。"""
def __init__(self, *files):
self.files = files
def write(self, obj):
for f in self.files:
f.write(obj)
f.flush()
def flush(self):
for f in self.files:
f.flush()