- Deleted obsolete markdown files related to follow tab coordinates testing and results. - Removed outdated batch scripts for launching the Web GUI. - Updated `config.json` to improve reservation handling and adjust prize value settings. - Enhanced logging and error handling in `web_gui.py` and `app.js` for better user experience. - Added automatic screenshot functionality during Web GUI startup for improved monitoring.
322 lines
14 KiB
Python
322 lines
14 KiB
Python
from __future__ import annotations
|
||
|
||
import os
|
||
import time
|
||
from PIL import Image
|
||
|
||
import douyin_pixel
|
||
|
||
|
||
def check_coupon_popup(analyser) -> bool:
|
||
"""從 `douyin_fudai.fudai_analyse._check_coupon_popup` 抽離。
|
||
|
||
像素優先,OCR 僅作補充且不推翻像素結論。
|
||
analyser 需提供:
|
||
- cut_pic(...)
|
||
- operation.analyse_pic_word(...)
|
||
- _log_kv(...)
|
||
"""
|
||
check_start = time.time()
|
||
|
||
# 步驟0:快速像素檢測彈窗背景(粉色/紅色區域)
|
||
pixel_check_start = time.time()
|
||
has_popup_background = False
|
||
try:
|
||
pic_path = os.path.join(os.path.dirname(__file__), "pic", "screenshot.png")
|
||
pic = Image.open(pic_path).convert("RGB")
|
||
has_popup_background = bool(douyin_pixel.detect_coupon_popup_background(pic))
|
||
_ = time.time() - pixel_check_start
|
||
if not has_popup_background:
|
||
return False
|
||
except Exception:
|
||
_ = time.time() - pixel_check_start
|
||
has_popup_background = False
|
||
|
||
# 步驟1:截圖標題區域
|
||
cut_title_start = time.time()
|
||
try:
|
||
analyser.cut_pic(60, 1260, 1020, 1480, "", "診斷優惠券標題")
|
||
cut_title_elapsed = time.time() - cut_title_start
|
||
except Exception as e:
|
||
cut_title_elapsed = time.time() - cut_title_start
|
||
analyser._log_kv("彈窗檢測步驟", "優惠券-截圖標題區域失敗", error=str(e), 耗時秒=f"{cut_title_elapsed:.3f}")
|
||
return False
|
||
|
||
# 步驟2:OCR 標題(模式0)
|
||
ocr_title0_start = time.time()
|
||
try:
|
||
t1 = str(analyser.operation.analyse_pic_word("診斷優惠券標題", 0) or "")
|
||
ocr_title0_elapsed = time.time() - ocr_title0_start
|
||
except Exception as e:
|
||
t1 = ""
|
||
ocr_title0_elapsed = time.time() - ocr_title0_start
|
||
analyser._log_kv("彈窗檢測步驟", "優惠券-OCR標題區域(模式0)失敗", error=str(e), 耗時秒=f"{ocr_title0_elapsed:.3f}")
|
||
|
||
# 步驟3:模式1(僅在模式0為空時)
|
||
ocr_title1_elapsed = 0.0
|
||
if not t1:
|
||
ocr_title1_start = time.time()
|
||
try:
|
||
t1 = str(analyser.operation.analyse_pic_word("診斷優惠券標題", 1) or "")
|
||
ocr_title1_elapsed = time.time() - ocr_title1_start
|
||
except Exception as e:
|
||
ocr_title1_elapsed = time.time() - ocr_title1_start
|
||
analyser._log_kv("彈窗檢測步驟", "優惠券-OCR標題區域(模式1)失敗", error=str(e), 耗時秒=f"{ocr_title1_elapsed:.3f}")
|
||
|
||
if not t1:
|
||
if has_popup_background:
|
||
total_elapsed = time.time() - check_start
|
||
analyser._log_kv(
|
||
"彈窗檢測步驟",
|
||
"優惠券-標題OCR為空但像素判定有彈窗,按像素結果返回",
|
||
總耗時秒=f"{total_elapsed:.3f}",
|
||
截圖耗時秒=f"{cut_title_elapsed:.3f}",
|
||
像素判定="有彈窗背景",
|
||
)
|
||
return True
|
||
return False
|
||
|
||
strong_keywords_title = ("購物券", "新客", "專享券")
|
||
if any(kw in t1 for kw in strong_keywords_title):
|
||
total_elapsed = time.time() - check_start
|
||
matched_strong = [kw for kw in strong_keywords_title if kw in t1]
|
||
analyser._log_kv(
|
||
"彈窗檢測步驟",
|
||
"優惠券-標題區域檢測到強關鍵詞,快速返回",
|
||
匹配到的強關鍵詞=matched_strong,
|
||
識別文本=t1[:50],
|
||
總耗時秒=f"{total_elapsed:.3f}",
|
||
截圖耗時秒=f"{cut_title_elapsed:.3f}",
|
||
OCR耗時秒=f"{(ocr_title0_elapsed + ocr_title1_elapsed):.3f}",
|
||
節省="跳過按鈕檢測",
|
||
)
|
||
return True
|
||
|
||
import re
|
||
|
||
t1_stripped = t1.strip()
|
||
possible_chars = ("券", "新", "客", "購", "物", "專", "享", "領", "取", "收", "下", "一", "得", "元", "分鐘")
|
||
if not any(char in t1_stripped for char in possible_chars):
|
||
return False
|
||
|
||
t1_clean = re.sub(r"[\s\d\W]", "", t1_stripped)
|
||
if len(t1_clean) < 2:
|
||
if has_popup_background:
|
||
total_elapsed = time.time() - check_start
|
||
analyser._log_kv(
|
||
"彈窗檢測步驟",
|
||
"優惠券-標題文本過短但像素判定有彈窗,按像素結果返回",
|
||
總耗時秒=f"{total_elapsed:.3f}",
|
||
截圖耗時秒=f"{cut_title_elapsed:.3f}",
|
||
像素判定="有彈窗背景",
|
||
標題文本=t1[:50],
|
||
)
|
||
return True
|
||
return False
|
||
|
||
# 步驟4:截圖按鈕區域
|
||
cut_btn_start = time.time()
|
||
try:
|
||
analyser.cut_pic(260, 2080, 820, 2240, "", "診斷優惠券按鈕")
|
||
cut_btn_elapsed = time.time() - cut_btn_start
|
||
except Exception as e:
|
||
cut_btn_elapsed = time.time() - cut_btn_start
|
||
analyser._log_kv("彈窗檢測步驟", "優惠券-截圖按鈕區域失敗", error=str(e), 耗時秒=f"{cut_btn_elapsed:.3f}")
|
||
cut_btn_elapsed = 0.0
|
||
|
||
# 步驟5:OCR 按鈕(模式0)
|
||
ocr_btn0_start = time.time()
|
||
try:
|
||
t2 = str(analyser.operation.analyse_pic_word("診斷優惠券按鈕", 0) or "")
|
||
ocr_btn0_elapsed = time.time() - ocr_btn0_start
|
||
except Exception as e:
|
||
t2 = ""
|
||
ocr_btn0_elapsed = time.time() - ocr_btn0_start
|
||
analyser._log_kv("彈窗檢測步驟", "優惠券-OCR按鈕區域(模式0)失敗", error=str(e), 耗時秒=f"{ocr_btn0_elapsed:.3f}")
|
||
|
||
# 步驟6:模式1(僅在模式0為空時)
|
||
ocr_btn1_elapsed = 0.0
|
||
if not t2:
|
||
ocr_btn1_start = time.time()
|
||
try:
|
||
t2 = str(analyser.operation.analyse_pic_word("診斷優惠券按鈕", 1) or "")
|
||
ocr_btn1_elapsed = time.time() - ocr_btn1_start
|
||
except Exception as e:
|
||
ocr_btn1_elapsed = time.time() - ocr_btn1_start
|
||
analyser._log_kv("彈窗檢測步驟", "優惠券-OCR按鈕區域(模式1)失敗", error=str(e), 耗時秒=f"{ocr_btn1_elapsed:.3f}")
|
||
|
||
hay = (t1 + " " + t2).strip()
|
||
if not hay:
|
||
if has_popup_background:
|
||
total_elapsed = time.time() - check_start
|
||
analyser._log_kv(
|
||
"彈窗檢測步驟",
|
||
"優惠券-OCR標題和按鈕均為空,但像素判定有彈窗,按像素結果返回",
|
||
總耗時秒=f"{total_elapsed:.3f}",
|
||
像素判定="有彈窗背景",
|
||
)
|
||
return True
|
||
return False
|
||
|
||
strong_keywords = ("購物券", "新客", "專享券")
|
||
weak_keywords = ("領取", "收下", "開心收下")
|
||
has_strong_keyword = any(kw in hay for kw in strong_keywords)
|
||
has_weak_keyword = any(kw in hay for kw in weak_keywords)
|
||
has_coupon_keyword = ("券" in hay) or ("新客" in hay) or ("購物券" in hay)
|
||
|
||
result = has_strong_keyword or (has_weak_keyword and has_coupon_keyword)
|
||
if not result and has_popup_background:
|
||
result = True
|
||
|
||
total_elapsed = time.time() - check_start
|
||
ocr_total = ocr_title0_elapsed + ocr_title1_elapsed + ocr_btn0_elapsed + ocr_btn1_elapsed
|
||
if result:
|
||
matched_keys = [kw for kw in strong_keywords if kw in hay] + [kw for kw in weak_keywords if kw in hay]
|
||
analyser._log_kv("彈窗檢測步驟", "優惠券-檢測到彈窗", 匹配到的關鍵詞=matched_keys, 總耗時秒=f"{total_elapsed:.3f}", OCR耗時秒=f"{ocr_total:.3f}")
|
||
else:
|
||
matched_keys = [kw for kw in strong_keywords if kw in hay] + [kw for kw in weak_keywords if kw in hay]
|
||
analyser._log_kv(
|
||
"彈窗檢測步驟",
|
||
"優惠券-未檢測到彈窗",
|
||
合併文本=hay[:100] if hay else "空",
|
||
匹配到的關鍵詞=matched_keys if matched_keys else [],
|
||
總耗時秒=f"{total_elapsed:.3f}",
|
||
OCR耗時秒=f"{ocr_total:.3f}",
|
||
)
|
||
return result
|
||
|
||
|
||
def check_shopping_redpacket_popup(analyser) -> bool:
|
||
"""從 `douyin_fudai.fudai_analyse._check_shopping_redpacket_popup` 抽離。
|
||
|
||
像素優先,OCR 僅作補充且不推翻像素結論。
|
||
"""
|
||
check_start = time.time()
|
||
|
||
# 步驟0:快速像素檢測中央紅色彈窗背景
|
||
pixel_check_start = time.time()
|
||
has_popup_pixel = False
|
||
try:
|
||
pic_path = os.path.join(os.path.dirname(__file__), "pic", "screenshot.png")
|
||
pic = Image.open(pic_path).convert("RGB")
|
||
has_popup_pixel = bool(douyin_pixel.detect_redpacket_popup_background(pic))
|
||
_ = time.time() - pixel_check_start
|
||
if not has_popup_pixel:
|
||
return False
|
||
except Exception:
|
||
_ = time.time() - pixel_check_start
|
||
has_popup_pixel = False
|
||
|
||
# 步驟1:截圖檢測區域
|
||
cut_start = time.time()
|
||
try:
|
||
analyser.cut_pic(230, 450, 860, 550, "", "診斷購物紅包")
|
||
_ = time.time() - cut_start
|
||
except Exception as e:
|
||
cut_elapsed = time.time() - cut_start
|
||
analyser._log_kv("彈窗檢測步驟", "購物紅包-截圖區域失敗", error=str(e), 耗時秒=f"{cut_elapsed:.3f}")
|
||
return False
|
||
|
||
# OCR 模式0
|
||
ocr_mode0_start = time.time()
|
||
try:
|
||
t = str(analyser.operation.analyse_pic_word("診斷購物紅包", 0) or "")
|
||
hay = t.strip() if t else ""
|
||
ocr_mode0_elapsed = time.time() - ocr_mode0_start
|
||
except Exception as e:
|
||
hay = ""
|
||
ocr_mode0_elapsed = time.time() - ocr_mode0_start
|
||
analyser._log_kv("彈窗檢測步驟", "購物紅包-OCR(模式0)失敗", error=str(e), 耗時秒=f"{ocr_mode0_elapsed:.3f}")
|
||
|
||
if not hay:
|
||
if has_popup_pixel:
|
||
total_elapsed = time.time() - check_start
|
||
analyser._log_kv(
|
||
"彈窗檢測步驟",
|
||
"購物紅包-OCR內容為空但像素判定有紅包彈窗,按像素結果返回",
|
||
總耗時秒=f"{total_elapsed:.3f}",
|
||
像素判定="有紅包彈窗背景",
|
||
)
|
||
return True
|
||
|
||
# 嘗試按鈕區域快速 OCR(兜底)
|
||
try:
|
||
analyser.cut_pic(400, 1200, 680, 1400, "", "診斷購物紅包按鈕")
|
||
btn_text = str(analyser.operation.analyse_pic_word("診斷購物紅包按鈕", 0) or "").strip()
|
||
if "搶" in btn_text:
|
||
total_elapsed = time.time() - check_start
|
||
analyser._log_kv("彈窗檢測步驟", "購物紅包-檢測到彈窗(按鈕區域)", 識別文本=btn_text[:50], 總耗時秒=f"{total_elapsed:.3f}", 檢測方式="按鈕區域快速檢測")
|
||
return True
|
||
except Exception:
|
||
pass
|
||
return False
|
||
|
||
keywords = ("邀你搶購物紅包", "搶購物紅包", "購物紅包", "邀你搶", "搶紅包", "搶")
|
||
if any(kw in hay for kw in keywords):
|
||
matched = [kw for kw in keywords if kw in hay]
|
||
total_elapsed = time.time() - check_start
|
||
analyser._log_kv("彈窗檢測步驟", "購物紅包-檢測到彈窗", 匹配到的關鍵詞=matched, 總耗時秒=f"{total_elapsed:.3f}", OCR耗時秒=f"{ocr_mode0_elapsed:.3f}")
|
||
return True
|
||
|
||
texts = [hay]
|
||
ocr_mode1_elapsed = 0.0
|
||
ocr_mode2_elapsed = 0.0
|
||
|
||
# 模式1
|
||
ocr_mode1_start = time.time()
|
||
try:
|
||
t1 = str(analyser.operation.analyse_pic_word("診斷購物紅包", 1) or "")
|
||
hay1 = t1.strip() if t1 else ""
|
||
ocr_mode1_elapsed = time.time() - ocr_mode1_start
|
||
if hay1:
|
||
texts.append(hay1)
|
||
if any(kw in hay1 for kw in keywords):
|
||
matched = [kw for kw in keywords if kw in hay1]
|
||
total_elapsed = time.time() - check_start
|
||
analyser._log_kv("彈窗檢測步驟", "購物紅包-檢測到彈窗(模式1)", 匹配到的關鍵詞=matched, 總耗時秒=f"{total_elapsed:.3f}", OCR耗時秒=f"{(ocr_mode0_elapsed + ocr_mode1_elapsed):.3f}")
|
||
return True
|
||
except Exception as e:
|
||
ocr_mode1_elapsed = time.time() - ocr_mode1_start
|
||
analyser._log_kv("彈窗檢測步驟", "購物紅包-OCR(模式1)失敗", error=str(e), 耗時秒=f"{ocr_mode1_elapsed:.3f}")
|
||
|
||
# 模式2
|
||
ocr_mode2_start = time.time()
|
||
try:
|
||
t2 = str(analyser.operation.analyse_pic_word("診斷購物紅包", 2) or "")
|
||
hay2 = t2.strip() if t2 else ""
|
||
ocr_mode2_elapsed = time.time() - ocr_mode2_start
|
||
if hay2:
|
||
texts.append(hay2)
|
||
if any(kw in hay2 for kw in keywords):
|
||
matched = [kw for kw in keywords if kw in hay2]
|
||
total_elapsed = time.time() - check_start
|
||
analyser._log_kv(
|
||
"彈窗檢測步驟",
|
||
"購物紅包-檢測到彈窗(模式2)",
|
||
匹配到的關鍵詞=matched,
|
||
總耗時秒=f"{total_elapsed:.3f}",
|
||
OCR耗時秒=f"{(ocr_mode0_elapsed + ocr_mode1_elapsed + ocr_mode2_elapsed):.3f}",
|
||
)
|
||
return True
|
||
except Exception as e:
|
||
ocr_mode2_elapsed = time.time() - ocr_mode2_start
|
||
analyser._log_kv("彈窗檢測步驟", "購物紅包-OCR(模式2)失敗", error=str(e), 耗時秒=f"{ocr_mode2_elapsed:.3f}")
|
||
|
||
combined_text = " ".join(texts).strip()
|
||
final_keywords = ("購物紅包", "邀你搶", "搶紅包")
|
||
result = any(kw in combined_text for kw in final_keywords)
|
||
if not result and has_popup_pixel:
|
||
result = True
|
||
|
||
total_elapsed = time.time() - check_start
|
||
ocr_total = ocr_mode0_elapsed + ocr_mode1_elapsed + ocr_mode2_elapsed
|
||
if result:
|
||
matched = [kw for kw in final_keywords if kw in combined_text]
|
||
analyser._log_kv("彈窗檢測步驟", "購物紅包-檢測到彈窗(合併檢查)", 匹配到的關鍵詞=matched, 總耗時秒=f"{total_elapsed:.3f}", OCR耗時秒=f"{ocr_total:.3f}")
|
||
else:
|
||
analyser._log_kv("彈窗檢測步驟", "購物紅包-未檢測到彈窗", 合併文本=combined_text[:100] if combined_text else "空", 總耗時秒=f"{total_elapsed:.3f}", OCR耗時秒=f"{ocr_total:.3f}")
|
||
return result
|
||
|
||
|
||
|