把 douyin/reservation/douyin_reservation_logic.py 按职责拆成 douyin/reservation/reservation_logic/ 子包: - _common (57) 共享错误处理工具占位 + ReservationItem 类型 - helpers (130) 内部工具:标题归一化 / 相似度 / 排序 / 时间戳 - add (447) add_reservation_core_logic 主入口(添加新预约) - cleanup_validate (136) validate_back_to_reservation / cleanup_expired_reservations - queue_full (305) handle_queue_full_wait_at_earliest 队列满时等待策略 - attend_window (1437) process_reservation_attend_window 进入预约窗口(最大头) - record (328) record_high_value_reservation 高价值预约登记入口 原文件改为 53 行 re-export shim,17 个公共符号全部保留。 - process_reservation_attend_window 7 处外部引用 - handle_queue_full_wait_at_earliest 5 处外部引用 - _now_ts 5 处 / cleanup_expired_reservations 4 处 / _sort_reservations_inplace 4 处 - 其他 5 处以下 全部不需修改。 回归: - 8 个新文件 + shim compileall 通过 - pytest 353 passed / 1 skipped - 旧 import 路径与新子包路径返回同一函数对象 - _now_ts / _normalize_room_title / _calculate_string_similarity 等纯函数 调用结果与拆分前一致 Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
131 lines
3.7 KiB
Python
131 lines
3.7 KiB
Python
"""reservation_logic.helpers - 内部工具函数。
|
||
|
||
- _now_ts 当前时间戳(time.time 别名)
|
||
- _normalize_room_title_for_matching 规范化直播间标题用于匹配
|
||
- _calculate_string_similarity 两个字符串的相似度(用于回退校验)
|
||
- _extract_content_core 抽取福袋内容主干
|
||
- _sort_reservations_inplace 队列排序:future_ts → prize → created_ts
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import re
|
||
import time
|
||
from typing import List
|
||
|
||
from ._common import ReservationItem
|
||
|
||
|
||
def _now_ts() -> float:
|
||
return time.time()
|
||
|
||
|
||
|
||
|
||
def _normalize_room_title_for_matching(title: str) -> str:
|
||
"""规范化直播间标题用于匹配比较。
|
||
|
||
去除数字、特殊字符等,只保留核心名称部分。
|
||
例如:"王媛媛" 和 "王媛媛2" 会被规范化为相同的字符串。
|
||
"""
|
||
if not title:
|
||
return ""
|
||
# 去除空格和换行
|
||
s = str(title).replace("\r", " ").replace("\n", " ").strip()
|
||
s = re.sub(r"\s+", "", s)
|
||
# 去除常见无关词
|
||
for w in (
|
||
"点赞",
|
||
"赞",
|
||
"关注",
|
||
"粉丝",
|
||
"直播中",
|
||
"正在直播",
|
||
"进入直播间",
|
||
"推荐",
|
||
"在线",
|
||
"人在线",
|
||
):
|
||
s = s.replace(w, "")
|
||
# 去除数字和特殊字符,只保留中文、英文、数字(但去除末尾的数字)
|
||
# 先去除末尾的数字(如 "王媛媛2" -> "王媛媛")
|
||
s = re.sub(r"\d+$", "", s)
|
||
# 去除所有特殊字符,只保留中文、英文
|
||
s = re.sub(r"[^A-Za-z\u4e00-\u9fff]", "", s)
|
||
return s
|
||
|
||
|
||
|
||
|
||
def _calculate_string_similarity(s1: str, s2: str) -> float:
|
||
"""计算两个字符串的相似度(简单的字符重叠比例)。
|
||
|
||
返回 0.0 到 1.0 之间的值,1.0 表示完全相同。
|
||
"""
|
||
if not s1 or not s2:
|
||
return 0.0
|
||
if s1 == s2:
|
||
return 1.0
|
||
|
||
# 计算字符集合的交集和并集
|
||
set1 = set(s1)
|
||
set2 = set(s2)
|
||
intersection = len(set1 & set2)
|
||
union = len(set1 | set2)
|
||
|
||
if union == 0:
|
||
return 0.0
|
||
|
||
# Jaccard相似度
|
||
jaccard = intersection / union
|
||
|
||
# 如果长度相近,额外加分
|
||
len_ratio = (
|
||
min(len(s1), len(s2)) / max(len(s1), len(s2))
|
||
if max(len(s1), len(s2)) > 0
|
||
else 0.0
|
||
)
|
||
|
||
# 综合相似度(Jaccard占70%,长度比例占30%)
|
||
similarity = jaccard * 0.7 + len_ratio * 0.3
|
||
|
||
return similarity
|
||
|
||
|
||
|
||
|
||
def _extract_content_core(content: str) -> str:
|
||
"""提取福袋内容的核心部分(去除倒计时等变化部分)。
|
||
|
||
例如:"10后开奖03(琦儿专属1HONOR/荣耀平板X9Pro11.5英寸/官方"
|
||
提取为:"琦儿专属1HONOR/荣耀平板X9Pro11.5英寸/官方"
|
||
"""
|
||
if not content:
|
||
return ""
|
||
# 去除开头的倒计时部分(如 "10后开奖03"、"06后开奖09" 等)
|
||
# 匹配模式:数字+后开奖+数字
|
||
s = re.sub(r"^\d+后开奖\d+", "", content)
|
||
# 去除开头的括号和数字
|
||
s = re.sub(r"^[\((]\d+", "", s)
|
||
# 去除开头的数字
|
||
s = re.sub(r"^\d+", "", s)
|
||
# 去除前后空格
|
||
s = s.strip()
|
||
return s
|
||
|
||
|
||
|
||
|
||
def _sort_reservations_inplace(reservations: List[ReservationItem]) -> None:
|
||
"""按 future_ts 升序,其次 prize_value_yuan 降序,其次 created_ts 升序排序。"""
|
||
|
||
def _key(it: ReservationItem):
|
||
ft = float(it.get("future_ts") or 0.0)
|
||
val = float(it.get("prize_value_yuan") or 0.0)
|
||
created = float(it.get("created_ts") or 0.0)
|
||
# future_ts 越小越优先;价值越大越优先(因此取 -val);创建时间越早越优先
|
||
return (ft, -val, created)
|
||
|
||
reservations.sort(key=_key)
|
||
|
||
|