Files
douyin_guaji-main/douyin/utils/time_utils.py
T
CodexandClaude Opus 4.5 b137aead48 refactor: 合并 fudai_detail_modules/ 到 douyin/fudai/core/
将 fudai_detail_modules/ 6 个模块全部迁移到 douyin/fudai/core/:
- fudai_detail_coupon_detection.py
- fudai_detail_operations.py
- fudai_detail_room_title.py
- fudai_detail_utils.py
- fudai_detail_validator.py
- fudai_detail_reader.py -> fudai_detail_orchestrator.py(避免与 core/ 已有同名文件冲突)

更新所有 import:
- douyin/fudai/douyin_fudai_core.py
- douyin/fudai/douyin_fudai_detail.py

删除 fudai_detail_modules/ 空目录及其 README/__init__。

验证:python -m compileall 全项目通过。

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-05-21 08:46:11 +08:00

61 lines
1.6 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.
"""倒计时解析与时间相关的工具函数。
集中化以前散落在多个文件的重复实现:
- douyin/fudai/douyin_fudai_core.py: _parse_mm_ss
- douyin/fudai/core/fudai_detail_reader.py: _parse_mm_ss
注意:normalize_countdown_text 仍在 douyin/utils/douyin_utils.py 中,
那里逻辑更复杂(包含 OCR 字符纠正等),不在此处重复。
"""
from __future__ import annotations
import logging
import re
from typing import Optional, Tuple
def parse_mm_ss(text: str) -> Optional[Tuple[int, int]]:
"""从文本中解析 mm:ss 格式的时间,返回 (分钟, 秒) 元组。
支持中文冒号 `:` 和英文冒号 `:`。
Args:
text: 待解析的文本,例如 "11:37" 或 "5:30后开奖"
Returns:
成功返回 (mm, ss) 元组;失败返回 None
"""
if not text:
return None
m = re.search(r"(\d{1,2})[::](\d{2})", str(text))
if not m:
return None
try:
return int(m.group(1)), int(m.group(2))
except (ValueError, TypeError) as e:
logging.debug("解析倒计时格式失败: text=%r, error=%s", str(text)[:50], e)
return None
def extract_2digits(text: str) -> str:
"""从文本中提取最多 2 位数字。
用于 OCR 倒计时分钟段/秒段的预处理。
Args:
text: OCR 识别的文本
Returns:
2 位数字字符串;不足 2 位时左侧补 0;无数字返回空串
"""
digits = "".join(ch for ch in str(text or "") if ch.isdigit())
if len(digits) >= 2:
return digits[:2]
if len(digits) == 1:
return "0" + digits
return ""
__all__ = ["parse_mm_ss", "extract_2digits"]