From 3488624b8fb830a60952e2803722ad1e5bb8a8a3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 17 Jan 2026 18:46:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E9=A2=84=E7=BA=A6?= =?UTF-8?q?=E9=80=BB=E8=BE=91=EF=BC=8C=E5=A2=9E=E5=BC=BA=E6=98=A0=E5=B0=84?= =?UTF-8?q?=E8=A1=A8=E8=AE=B0=E5=BD=95=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=8F=90?= =?UTF-8?q?=E5=8D=87=E7=94=A8=E6=88=B7=E4=BD=93=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- douyin/fudai/douyin_fudai_core.py | 159 +++++++++------ .../reservation/douyin_reservation_logic.py | 150 +++++++++++++-- gui/gui_app.py | 182 +++++++++++++++--- 3 files changed, 382 insertions(+), 109 deletions(-) diff --git a/douyin/fudai/douyin_fudai_core.py b/douyin/fudai/douyin_fudai_core.py index 0c2f517..3fb2dd8 100644 --- a/douyin/fudai/douyin_fudai_core.py +++ b/douyin/fudai/douyin_fudai_core.py @@ -2035,7 +2035,7 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo pass # 处理直播间标题结果 - if live_room_title_raw and live_room_title_text_raw: + if live_room_title_raw or live_room_title_text_raw: try: analyser.current_room_title = live_room_title_raw analyser.current_room_title_raw = live_room_title_text_raw @@ -2047,64 +2047,6 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo # raw_text=live_room_title_text_raw, # log_type="INFO", # ) - - # 更新映射表中的标题信息和福袋详情(总是记录完整信息) - try: - room_pos = int(getattr(analyser, "room_pos", 0) or 0) - room_pos_map = getattr(analyser, "_room_pos_to_title_map", {}) - - # 创建或更新完整的房间信息 - room_info = { - "title": live_room_title_raw or "", - "title_raw": live_room_title_text_raw or "", - "has_fudai": True, - "fudai_content": fudai_content_text, - "fudai_detail": { - "content": fudai_content_text, - "time_text": time_text, - "participants": participants, - "copies": copies, - "prize_value_yuan": prize_value_yuan, - "button_text": getattr(analyser, "button_text_raw", ""), - "timestamp": time.time(), - }, - "timestamp": time.time(), - } - - room_pos_map[room_pos] = room_info - analyser._room_pos_to_title_map = room_pos_map - - analyser._log_kv( - "预约", - "[预约] 并行OCR完成后记录完整映射表信息", - room_pos=room_pos, - title=live_room_title_raw[:30] if live_room_title_raw else "", - fudai_content=fudai_content_text[:30] if fudai_content_text else "", - prize_value_yuan=prize_value_yuan, - log_type="DEBUG", - ) - - # 序列化运行时映射表到文件,供 GUI 刷新使用 - try: - from douyin.reservation import ( - douyin_reservation_handler as _rh, - ) - - if hasattr(_rh, "_dump_room_pos_map_to_state"): - _rh._dump_room_pos_map_to_state(analyser) - except Exception: - pass - - except Exception as e_update: - try: - analyser._log_kv( - "错误", - "并行OCR完成后记录映射表信息失败", - error=str(e_update), - level="WARNING", - ) - except Exception: - pass except Exception as e_inner: try: analyser._log_kv( @@ -2117,6 +2059,105 @@ def get_fudai_contain(analyser, renwu: int = 2) -> Tuple[str, str, int, int, flo except Exception: pass + # 在有福袋的情况下,记录完整OCR详情到映射表(即使标题未识别) + try: + room_pos = int(getattr(analyser, "room_pos", 0) or 0) + room_pos_map = getattr(analyser, "_room_pos_to_title_map", {}) + + # 兼容旧格式:若已有旧元组格式,先转为 dict + existing_info = room_pos_map.get(room_pos, {}) + base_info = {} + if isinstance(existing_info, dict): + base_info = dict(existing_info) + elif existing_info: + try: + base_info = { + "title": existing_info[0] if len(existing_info) > 0 else "", + "title_raw": existing_info[1] if len(existing_info) > 1 else "", + } + except Exception: + base_info = {} + + title_for_map = ( + live_room_title_raw + or base_info.get("title") + or str(getattr(analyser, "current_room_title", "") or "") + ) + title_raw_for_map = ( + live_room_title_text_raw + or base_info.get("title_raw") + or str(getattr(analyser, "current_room_title_raw", "") or "") + ) + + button_text_for_map = ( + str(button_text_raw or "") + or str(getattr(analyser, "button_text_raw", "") or "") + ) + + room_info = dict(base_info) + room_info.update( + { + "title": title_for_map or "", + "title_raw": title_raw_for_map or "", + "has_fudai": True, + "fudai_content": fudai_content_text, + "fudai_detail": { + "content": fudai_content_text, + "task_list": str(task_list_raw or ""), + "countdown_mm": str(countdown_mm_raw or ""), + "countdown_ss": str(countdown_ss_raw or ""), + "time_text": time_text, + "participants": participants, + "copies": copies, + "prize_value_yuan": prize_value_yuan, + "value_raw": str(value_raw or ""), + "button_text": button_text_for_map, + "button_text_raw": str(button_text_raw or ""), + "live_room_title": str(live_room_title_raw or title_for_map or ""), + "live_room_title_raw": str( + live_room_title_text_raw or title_raw_for_map or "" + ), + "timestamp": time.time(), + }, + "timestamp": time.time(), + } + ) + + room_pos_map[room_pos] = room_info + analyser._room_pos_to_title_map = room_pos_map + + analyser._log_kv( + "预约", + "[预约] 并行OCR完成后记录完整映射表信息", + room_pos=room_pos, + title=(title_for_map[:30] if title_for_map else ""), + fudai_content=fudai_content_text[:30] if fudai_content_text else "", + prize_value_yuan=prize_value_yuan, + log_type="DEBUG", + ) + + # 序列化运行时映射表到文件,供 GUI 刷新使用 + try: + from douyin.reservation import ( + douyin_reservation_handler as _rh, + ) + + if hasattr(_rh, "_dump_room_pos_map_to_state"): + _rh._dump_room_pos_map_to_state(analyser) + except Exception: + pass + + except Exception as e_update: + try: + analyser._log_kv( + "错误", + "并行OCR完成后记录映射表信息失败", + error=str(e_update), + level="WARNING", + ) + except Exception: + pass + return fudai_content_text, time_text, participants, copies, prize_value_yuan diff --git a/douyin/reservation/douyin_reservation_logic.py b/douyin/reservation/douyin_reservation_logic.py index e62b0fd..9d2f4ad 100644 --- a/douyin/reservation/douyin_reservation_logic.py +++ b/douyin/reservation/douyin_reservation_logic.py @@ -1869,33 +1869,30 @@ def process_reservation_attend_window( except Exception: pass - # 校验失败意味着位置信息可能不准确,先尝试重新查找目标直播间的当前位置 - updated_target = dict( - target - ) # 复制一份,避免修改原预约项 + # 校验失败意味着位置信息可能不准确,尝试用映射表识别“当前/目标”位置差 + updated_target = dict(target) + mapped_target_pos = None + mapped_current_pos = None try: - from douyin.reservation import ( - douyin_reservation_handler, - ) from douyin.reservation.douyin_reservation_handler import ( ReservationHandler, ) - current_pos = ( + mapped_target_pos = ( ReservationHandler.find_room_pos_by_title( analyser, target_room_title, target_room_title_raw, ) ) - if current_pos is not None and current_pos > 0: - updated_target["pos_seen"] = current_pos + if mapped_target_pos is not None and mapped_target_pos > 0: + updated_target["pos_seen"] = int(mapped_target_pos) try: analyser._log_kv( "预约", "[预约] 重新查找目标直播间位置成功", old_pos=int(target.get("pos_seen", 0)), - new_pos=current_pos, + new_pos=int(mapped_target_pos), target_room_title=( target_room_title[:50] if target_room_title @@ -1920,28 +1917,139 @@ def process_reservation_attend_window( ) except Exception: pass + + # 识别当前直播间标题(优先缓存,必要时重新识别) + current_room_title = str( + getattr(analyser, "current_room_title", "") or "" + ).strip() + current_room_title_raw = str( + getattr(analyser, "current_room_title_raw", "") or "" + ).strip() + if not current_room_title and not current_room_title_raw: + try: + from douyin.fudai import douyin_fudai_detail + + y_pianyi_val = int( + getattr(analyser, "y_pianyi", 0) or 0 + ) + ( + current_room_title, + current_room_title_raw, + ) = ( + douyin_fudai_detail.read_live_room_title_core( + analyser, y_pianyi_val + ) + ) + try: + analyser.current_room_title = ( + current_room_title + ) + analyser.current_room_title_raw = ( + current_room_title_raw + ) + except Exception: + pass + except Exception: + current_room_title = "" + current_room_title_raw = "" + + if current_room_title or current_room_title_raw: + mapped_current_pos = ( + ReservationHandler.find_room_pos_by_title( + analyser, + current_room_title, + current_room_title_raw, + ) + ) + if ( + mapped_current_pos is not None + and mapped_current_pos > 0 + ): + try: + old_room_pos = int( + getattr(analyser, "room_pos", 0) or 0 + ) + except Exception: + old_room_pos = 0 + try: + analyser.room_pos = int(mapped_current_pos) + except Exception: + pass + try: + analyser._log_kv( + "预约", + "[预约] 通过映射表识别当前直播间位置", + old_pos=int(old_room_pos), + new_pos=int(mapped_current_pos), + current_room_title=( + current_room_title[:50] + if current_room_title + else "" + ), + ) + except Exception: + pass except Exception as e: try: analyser._log_kv( "预约", - "[预约] 重新查找目标直播间位置时出错", + "[预约] 重新查找目标/当前直播间位置时出错", error=str(e), target_pos=int(target.get("pos_seen", 0)), ) except Exception: pass - # 尝试使用回退回调根据更新后的位置定位到目标直播间 + # 尝试使用回退回调根据映射表差值重新回退 ok2 = False - try: - if seek_reservation_room_callback: - ok2 = bool( - seek_reservation_room_callback( - updated_target - ) + should_retry_seek = True + mapped_pos_diff = None + if ( + mapped_current_pos is not None + and mapped_target_pos is not None + ): + try: + mapped_pos_diff = int(mapped_target_pos) - int( + mapped_current_pos ) - except Exception: - ok2 = False + except Exception: + mapped_pos_diff = None + try: + analyser._log_kv( + "预约", + "[预约] 映射表差值回退准备", + mapped_current_pos=int(mapped_current_pos), + mapped_target_pos=int(mapped_target_pos), + mapped_pos_diff=( + int(mapped_pos_diff) + if mapped_pos_diff is not None + else None + ), + ) + except Exception: + pass + if mapped_pos_diff == 0: + should_retry_seek = False + try: + analyser._log_kv( + "预约", + "[预约] 映射表显示已在目标位置,但校验失败,跳过二次回退", + mapped_current_pos=int(mapped_current_pos), + mapped_target_pos=int(mapped_target_pos), + ) + except Exception: + pass + + if should_retry_seek: + try: + if seek_reservation_room_callback: + ok2 = bool( + seek_reservation_room_callback( + updated_target + ) + ) + except Exception: + ok2 = False if ok2: # 第二次回退成功,设置锁定期并标记待处理 diff --git a/gui/gui_app.py b/gui/gui_app.py index dc60d87..238d780 100644 --- a/gui/gui_app.py +++ b/gui/gui_app.py @@ -145,6 +145,9 @@ def main(): root = tk.Tk() + # 常量兜底:部分主题常量未提供时使用字符串 + BOTTOM = "bottom" + root.title("抖音福袋挂机 - 可视化控制台") root.geometry("1400x900") # 增大窗口尺寸,提供更多空间 @@ -2757,7 +2760,7 @@ def main(): # 将运行时映射表加入右侧 Notebook mapping_tab = ttk.Frame(notebook) map_box = ttk.LabelFrame( - mapping_tab, text="运行时映射表(room_pos -> (title, title_raw))" + mapping_tab, text="运行时映射表(包含福袋详情)" ) map_box.pack(fill=X, padx=10, pady=(10, 10)) @@ -2774,44 +2777,76 @@ def main(): mapping_source_var = tk.StringVar(value="未加载") ttk.Label(map_ctrl, textvariable=mapping_source_var).pack(side=LEFT, padx=(8, 0)) + mapping_tree_box = ttk.Frame(mapping_tab) + mapping_tree_box.pack(fill=BOTH, expand=True, padx=10, pady=(6, 10)) + mapping_tree = ttk.Treeview( - mapping_tab, + mapping_tree_box, columns=( "pos", "title", "title_raw", "has_fudai", "fudai_content", - "fudai_time", + "task_list", + "countdown", "fudai_value", "participants", "copies", + "button_text", "is_reserved", ), show="headings", height=12, ) - mapping_tree.heading("pos", text="位置") - mapping_tree.heading("title", text="标题") - mapping_tree.heading("title_raw", text="原始标题") - mapping_tree.heading("has_fudai", text="有福袋") - mapping_tree.heading("fudai_content", text="福袋内容") - mapping_tree.heading("fudai_time", text="开奖时间") - mapping_tree.heading("fudai_value", text="价值(元)") - mapping_tree.heading("participants", text="参与人数") - mapping_tree.heading("copies", text="份数") - mapping_tree.heading("is_reserved", text="已预约") - mapping_tree.column("pos", width=50, anchor="e") - mapping_tree.column("title", width=150, anchor="w") - mapping_tree.column("title_raw", width=150, anchor="w") - mapping_tree.column("has_fudai", width=60, anchor="center") - mapping_tree.column("fudai_content", width=150, anchor="w") - mapping_tree.column("fudai_time", width=120, anchor="center") - mapping_tree.column("fudai_value", width=80, anchor="e") - mapping_tree.column("participants", width=80, anchor="e") - mapping_tree.column("copies", width=60, anchor="e") - mapping_tree.column("is_reserved", width=60, anchor="center") - mapping_tree.pack(fill=BOTH, expand=True, padx=10, pady=(6, 10)) + # 为表头指定对齐,便于阅读;长列允许 stretch 以便在窗口拉伸时扩展 + mapping_tree.heading("pos", text="位置", anchor="center") + mapping_tree.heading("title", text="标题", anchor="w") + mapping_tree.heading("title_raw", text="原始标题", anchor="w") + mapping_tree.heading("has_fudai", text="有福袋", anchor="center") + mapping_tree.heading("fudai_content", text="福袋内容", anchor="w") + mapping_tree.heading("task_list", text="任务列表", anchor="w") + mapping_tree.heading("countdown", text="倒计时", anchor="center") + mapping_tree.heading("fudai_value", text="价值(元)", anchor="e") + mapping_tree.heading("participants", text="参与人数", anchor="e") + mapping_tree.heading("copies", text="份数", anchor="e") + mapping_tree.heading("button_text", text="按钮文本", anchor="w") + mapping_tree.heading("is_reserved", text="已预约", anchor="center") + + # 优化列宽与伸缩行为:让标题/内容类列随窗口拉伸,数值列保持固定宽度 + mapping_tree.column("pos", width=50, anchor="e", stretch=False) + mapping_tree.column("title", width=220, anchor="w", stretch=True) + mapping_tree.column("title_raw", width=220, anchor="w", stretch=True) + mapping_tree.column("has_fudai", width=60, anchor="center", stretch=False) + mapping_tree.column("fudai_content", width=300, anchor="w", stretch=True) + mapping_tree.column("task_list", width=240, anchor="w", stretch=True) + mapping_tree.column("countdown", width=90, anchor="center", stretch=False) + mapping_tree.column("fudai_value", width=90, anchor="e", stretch=False) + mapping_tree.column("participants", width=90, anchor="e", stretch=False) + mapping_tree.column("copies", width=70, anchor="e", stretch=False) + mapping_tree.column("button_text", width=180, anchor="w", stretch=True) + mapping_tree.column("is_reserved", width=70, anchor="center", stretch=False) + + mapping_tree.tag_configure("odd", background="#fafafa") + mapping_tree.tag_configure("even", background="#ffffff") + + map_scroll_y = ttk.Scrollbar( + mapping_tree_box, orient="vertical", command=mapping_tree.yview + ) + map_scroll_x = ttk.Scrollbar( + mapping_tree_box, orient="horizontal", command=mapping_tree.xview + ) + mapping_tree.configure( + yscrollcommand=map_scroll_y.set, xscrollcommand=map_scroll_x.set + ) + + # 使用 grid 布局使横向/纵向滚动条与 Treeview 精确对齐 + mapping_tree_box.grid_rowconfigure(0, weight=1) + mapping_tree_box.grid_columnconfigure(0, weight=1) + + mapping_tree.grid(row=0, column=0, sticky="nsew") + map_scroll_y.grid(row=0, column=1, sticky="ns") + map_scroll_x.grid(row=1, column=0, sticky="ew") # 加载映射表到表格(尝试多个可能的路径) def _load_mapping_to_view(): @@ -2863,6 +2898,7 @@ def main(): ] found = False + row_index = 0 for p in possible_paths: try: if os.path.exists(p): @@ -2891,7 +2927,10 @@ def main(): "", "", "", + "", + "", ), + tags=("even",), ) else: # 移除元信息 @@ -2921,7 +2960,54 @@ def main(): # 提取福袋详细信息 fudai_detail = v.get("fudai_detail") - if ( + task_list = "-" + button_text = "-" + countdown_text = "-" + if isinstance(fudai_detail, dict): + task_list = ( + str(fudai_detail.get("task_list") or "") + .strip() + or "-" + ) + button_text = ( + str(fudai_detail.get("button_text") or "") + .strip() + or "-" + ) + fudai_time = ( + str(fudai_detail.get("time_text") or "") + .strip() + or "-" + ) + fudai_value = ( + f"{float(fudai_detail.get('prize_value_yuan') or 0):.2f}" + if fudai_detail.get("prize_value_yuan") + else "-" + ) + participants = ( + str(fudai_detail.get("participants") or 0) + if fudai_detail.get("participants") + else "-" + ) + copies = ( + str(fudai_detail.get("copies") or 0) + if fudai_detail.get("copies") + else "-" + ) + mm = str(fudai_detail.get("countdown_mm") or "").strip() + ss = str(fudai_detail.get("countdown_ss") or "").strip() + if fudai_time and fudai_time != "-": + countdown_text = fudai_time + elif mm or ss: + if mm.isdigit() and len(mm) == 1: + mm = f"0{mm}" + if ss.isdigit() and len(ss) == 1: + ss = f"0{ss}" + if mm and ss: + countdown_text = f"{mm}:{ss}" + else: + countdown_text = mm or ss + elif ( fudai_detail and isinstance(fudai_detail, (tuple, list)) and len(fudai_detail) >= 5 @@ -2943,11 +3029,13 @@ def main(): if fudai_detail[3] else "-" ) # copies + countdown_text = fudai_time or "-" else: fudai_time = "-" fudai_value = "-" participants = "-" copies = "-" + countdown_text = "-" # 检查是否已预约 is_reserved = ( @@ -2961,9 +3049,12 @@ def main(): has_fudai = "" fudai_content = "-" fudai_time = "-" + countdown_text = "-" fudai_value = "-" participants = "-" copies = "-" + task_list = "-" + button_text = "-" is_reserved = ( "✓" if int(k) in reservations_data else "" ) @@ -2973,12 +3064,27 @@ def main(): title_raw, has_fudai, fudai_content, - fudai_time, + task_list, + countdown_text, fudai_value, participants, copies, + button_text, is_reserved, - ) = ("-", "-", "", "-", "-", "-", "-", "-", "-") + ) = ( + "-", + "-", + "", + "-", + "-", + "-", + "-", + "-", + "-", + "-", + "-", + ) + row_tag = "even" if row_index % 2 == 0 else "odd" mapping_tree.insert( "", "end", @@ -2988,13 +3094,17 @@ def main(): title_raw, has_fudai, fudai_content, - fudai_time, + task_list, + countdown_text, fudai_value, participants, copies, + button_text, is_reserved, ), + tags=(row_tag,), ) + row_index += 1 found = True break @@ -3007,7 +3117,21 @@ def main(): mapping_tree.insert( "", "end", - values=("-", "未找到映射表文件", "", "", "", "", "", "", "", ""), + values=( + "-", + "未找到映射表文件", + "", + "", + "", + "", + "", + "", + "", + "", + "", + "", + ), + tags=("even",), ) # 绑定刷新按钮与自动刷新