# -*- coding: utf-8 -*- """xq0080 金色湖畔自助餐数据导出 - 配置常量与全局钩子。 插件自包含:只依赖标准库 / 第三方库 / 底座注入的 RunContext。 各业务模块统一经 config.log() / config.progress() 输出,由 run() 启动时注入 ctx。 平台链路(2026-11 抓包实测,金色湖畔经营宝): ecom.meituan.com/bizaccount/login.html 未登录(epassport iframe 内填账密) ecom.meituan.com/bizaccount/biz-choice 已登录、待选商家(该账号只有金色湖畔一家) ecom.meituan.com/meishi/ 餐饮商家后台(kdb) ecom.meituan.com/finance-kdb/profit/list 新版收益明细页(带 Yoda 签名 SDK) """ import os import time # ==================== 平台:美团经营宝 ==================== HOME_URL = 'https://ecom.meituan.com/' # 收益明细页:页面加载后会挂载 Yoda(H5guard) SDK,页面上下文发请求时自动补 mtgsig PROFIT_PAGE_URL = 'https://ecom.meituan.com/finance-kdb/profit/list' OFFLINE_PAGE_URL = 'https://ecom.meituan.com/2564/finance/download-offline/index.html' BACKEND_HINTS = ( 'ecom.meituan.com/meishi', 'ecom.meituan.com/finance-kdb', 'ecom.meituan.com/2564/', 'e.dianping.com/app/', 'e.dianping.com/meishi/', ) LOGIN_PAGE_HINT = 'bizaccount/login.html' BIZ_CHOICE_HINT = 'bizaccount/biz-choice.html' PASSPORT_HINTS = ( 'passport.meituan.com', 'epassport.meituan.com', 'unifiedlogin', 'ssologin', 'passport.dianping.com', 'account.dianping.com/login', 'e.dianping.com/login', ) # ==================== 接口(新版财务 thrift 网关) ==================== POI_API = 'https://ecom.meituan.com/emis/gw/TPcPoiService/getPcPoiList' SUBMIT_API = ('https://ecom.meituan.com/finance/gw/thrift/finance-portal/' 'DownloadService/submitDownloadTask') RECORDS_API = ('https://ecom.meituan.com/finance/gw/thrift/finance-portal/' 'DownloadService/getDownloadTaskRecords') BIZ_TYPE = 1 # 1=团购(金色湖畔自助餐券即团购业务) DOWNLOAD_TASK_TYPE = 0 # 0=收益明细(平台文件名:新版收益明细_起止_团购_xxx.xlsx) TASK_TYPE_NAME = '收益明细' # 区间预检:先看这段时间有没有收益记录,没有就不提交下载任务(省平台任务、避免限流) SUMMARY_API = ('https://ecom.meituan.com/finance/gw/thrift/finance-portal/' 'ProfitService/getSummaryProfit/V2') # 门店:金色湖畔和牛寿喜烧自助餐厅(poiId 来自抓包;接口取不到时兜底) DEFAULT_POI_ID = 1308473827 POI_KEYWORD = '金色湖畔' # ==================== 账号密码预填(可选,界面配置) ==================== # 只预填账号密码 + 勾「用户协议」,**不点登录**:登录按钮、滑块/图形验证码、 # 短信验证码、选商家全部由用户在浏览器里自己完成(脚本只等结果)。 AUTOFILL_WAIT = 45 AUTOFILL_POLL = 1.5 LOGIN_FRAME_HINTS = ( 'epassport.meituan.com', 'passport.meituan.com', 'passport.dianping.com', 'account.dianping.com', ) # 登录后的「选择商家版本」页(我是餐饮商家 / 我是非餐饮商家)。 # 金色湖畔是自助餐厅 → 优先「餐饮商家」;页面没有该卡时退回第一张可点卡片。 BIZ_LINE_KEYWORDS = ('餐饮', '美食') BIZ_LINE_MARKERS = ('我是餐饮商家', '我是非餐饮商家') BIZ_LINE_MAX_TRIES = 3 BIZ_LINE_INTERVAL = 6 # 「选择商家」页(biz-choice):优先点含门店关键词的卡片,只有一张卡时直接进。 BIZ_PICK_MAX_TRIES = 3 BIZ_PICK_INTERVAL = 6 # ==================== 运行参数 ==================== LOGIN_WAIT = 600 # 等待人工登录(含验证码/选商家)超时 BACKEND_STABLE = 20 # 后台页连续稳定秒数(/meishi 未登录也会停留约 20s 再跳) SETTLE_WAIT = 8 # 登录成功后等首屏请求结束再抓 cookie POLL_SECONDS = 2 POLL_LOG_EVERY = 10 RECORD_SECONDS = 180 # 提交任务后轮询下载记录的最长时间 REUSE_MAX_AGE_MIN = 30 # 同区间记录复用时效(分钟内),避免重复生成触发平台限流 RETRY_MAX = 3 HEADLESS = False # 人工登录必须可见浏览器 # ==================== 保存目录 / 文件名 ==================== DEFAULT_SAVE_DIR = r'D:\资料\财务\金色湖畔自助餐数据导出' FILE_PREFIX = '金色湖畔自助餐_' # ==================== 浏览器档案(与 xq0041 隔离,避免同浏览器切号风控) ==================== PROFILE_NAME = 'xq0080' BROWSER_PORT = 9444 def _app_data_dir(): return os.path.join( os.getenv('LOCALAPPDATA') or os.getenv('APPDATA') or os.path.expanduser('~'), '37duAutomation') SESSIONS_DIR = os.path.join(_app_data_dir(), '.sessions') COOKIE_FILE = os.path.join(SESSIONS_DIR, 'meituan_cookies_xq0080.json') COOKIE_HEADER_FILE = os.path.join(SESSIONS_DIR, 'meituan_cookies_header_xq0080.txt') TOKEN_FILE = os.path.join(SESSIONS_DIR, 'meituan_tokens_xq0080.json') META_FILE = os.path.join(SESSIONS_DIR, 'meituan_session_meta_xq0080.json') def get_browser_profile_dir(): """插件专属浏览器用户目录:登录态跨次保留,不占用日常 Chrome,也不与 xq0041 共享。""" return os.path.join(_app_data_dir(), 'browser_sessions', PROFILE_NAME) # ==================== 浏览器窗口(全屏 + 置顶) ==================== def apply_window_mode(page): """登录阶段把浏览器窗口设为全屏并置顶(要人工点登录/验证码,别被其它窗口挡住)。""" try: page.set.window.full() except Exception as e: log(f'设置窗口全屏失败(忽略):{e}', 'WARN') if os.name == 'nt': try: _set_topmost(page.process_id, True) except Exception as e: log(f'设置窗口置顶失败(忽略):{e}', 'WARN') def restore_window_mode(page): """进入后台后恢复普通窗口并取消置顶 —— 只有登录阶段需要全屏/置顶。失败仅告警。""" try: page.set.window.normal() except Exception as e: log(f'恢复窗口大小失败(忽略):{e}', 'WARN') if os.name == 'nt': try: _set_topmost(page.process_id, False) except Exception as e: log(f'取消窗口置顶失败(忽略):{e}', 'WARN') def _set_topmost(pid, on=True): """把指定进程的主窗口设为 HWND_TOPMOST / 取消为 HWND_NOTOPMOST(仅 Windows)。""" if not pid: return import ctypes from ctypes import wintypes user32 = ctypes.windll.user32 user32.IsWindowVisible.argtypes = [wintypes.HWND] user32.GetWindowTextLengthW.argtypes = [wintypes.HWND] user32.GetWindowThreadProcessId.argtypes = [wintypes.HWND, ctypes.POINTER(wintypes.DWORD)] hwnds = [] @ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM) def _collect(hwnd, _lparam): if user32.IsWindowVisible(hwnd) and user32.GetWindowTextLengthW(hwnd) > 0: proc = wintypes.DWORD() user32.GetWindowThreadProcessId(hwnd, ctypes.byref(proc)) if proc.value == pid: hwnds.append(hwnd) return True user32.EnumWindows.argtypes = [ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM), wintypes.LPARAM] user32.EnumWindows(_collect, 0) user32.SetWindowPos.argtypes = [wintypes.HWND, wintypes.HWND, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_uint] target = -1 if on else -2 # HWND_TOPMOST / HWND_NOTOPMOST for hwnd in hwnds: user32.SetWindowPos(hwnd, wintypes.HWND(target), 0, 0, 0, 0, 0x0001 | 0x0002) # NOSIZE | NOMOVE # ==================== 全局钩子(run() 启动时注入) ==================== _log = None _progress = None _debug = False def set_log(fn): global _log _log = fn def set_progress(fn): global _progress _progress = fn def set_debug(on): global _debug _debug = bool(on) def debug_enabled(): return _debug def log(msg, level='INFO'): text = f'[{time.strftime("%H:%M:%S")}] {msg}' if level == 'DEBUG' and not _debug: return if _log: _log(str(msg), level) else: print(f'[{level}] {text}') def progress(pct): if _progress: try: _progress(int(pct)) except Exception: pass