179 lines
5.2 KiB
Python
179 lines
5.2 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, Tuple
|
||
|
||
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)
|
||
|
||
|
||
def _select_reservation_target_for_window(
|
||
reservations: List[ReservationItem],
|
||
now_ts: float,
|
||
dynamic_ahead_for_item,
|
||
poll_before: float,
|
||
) -> Tuple[int, ReservationItem]:
|
||
"""Pick the highest-value reservation among items whose attend windows overlap now.
|
||
|
||
The normal queue order remains earliest-first. This selector only changes behavior
|
||
when multiple reservations are already actionable at the same time.
|
||
"""
|
||
if not reservations:
|
||
raise IndexError("empty reservations")
|
||
|
||
actionable = []
|
||
for idx, item in enumerate(reservations):
|
||
try:
|
||
ft = float(item.get("future_ts") or 0.0)
|
||
except Exception:
|
||
ft = 0.0
|
||
if ft <= 0:
|
||
continue
|
||
try:
|
||
dynamic_ahead = float(dynamic_ahead_for_item(item) or 0.0)
|
||
except Exception:
|
||
dynamic_ahead = 0.0
|
||
window_start = ft - max(0.0, dynamic_ahead)
|
||
window_end = ft + max(0.0, poll_before)
|
||
if now_ts < window_start or now_ts > window_end:
|
||
continue
|
||
try:
|
||
value = float(item.get("prize_value_yuan") or 0.0)
|
||
except Exception:
|
||
value = 0.0
|
||
try:
|
||
created = float(item.get("created_ts") or 0.0)
|
||
except Exception:
|
||
created = 0.0
|
||
actionable.append((idx, value, ft, created, item))
|
||
|
||
if not actionable:
|
||
return 0, reservations[0]
|
||
|
||
actionable.sort(key=lambda row: (-row[1], row[2], row[3], row[0]))
|
||
idx, _value, _ft, _created, item = actionable[0]
|
||
return idx, item
|
||
|
||
|