feat: 新增交通银行数据导出插件及测试通道机制
This commit is contained in:
@@ -11,6 +11,11 @@
|
||||
2) douyin 域下存在会话票据 Cookie(sessionid* / sid_tt* / sid_guard*);
|
||||
3) 连续稳定 BACKEND_STABLE 秒,期间没有被踢回登录页。
|
||||
|
||||
登录态缓存(跨次复用):
|
||||
来客票据 sessionid_ls / sid_tt_ls / sid_guard_ls 都是**会话级 Cookie**,Chromium 退出后
|
||||
并不保证落盘保留(只靠 profile 目录不可靠),所以每次运行前把快照 douyin_cookies.json
|
||||
重新注入新启动的浏览器(page.set.cookies),profile 只当兜底;注入后仍进不了后台才转人工登录。
|
||||
|
||||
落盘(%LOCALAPPDATA%\\37duAutomation\\.sessions\\douyin_*):
|
||||
douyin_cookies.json / douyin_cookies_header.txt / douyin_tokens.json / douyin_session_meta.json
|
||||
"""
|
||||
@@ -167,6 +172,127 @@ def _save_login_state(page):
|
||||
return summary
|
||||
|
||||
|
||||
# ==================== 登录态缓存:快照 → 浏览器注入 ====================
|
||||
# 只注入 douyin 相关域;埋点/上报域(report/monitor/lx/dreport)丢掉,减少无关 Cookie 干扰。
|
||||
_INJECT_SKIP_DOMAINS = ('report', 'monitor', 'lx', 'dreport', 'open.douyin')
|
||||
|
||||
|
||||
def _load_cookies():
|
||||
"""读取落盘 Cookies 快照:去掉空值与已过期的,返回列表;无可用项返回 None。"""
|
||||
path = config.COOKIE_FILE
|
||||
if not os.path.exists(path):
|
||||
return None
|
||||
try:
|
||||
with open(path, encoding='utf-8') as f:
|
||||
cookies = json.load(f)
|
||||
except Exception as e:
|
||||
config.log(f'读取 Cookies 快照失败(忽略缓存):{e}', 'WARN')
|
||||
return None
|
||||
|
||||
now = time.time()
|
||||
alive = []
|
||||
for c in cookies or []:
|
||||
if not c.get('name') or not c.get('value'):
|
||||
continue
|
||||
try:
|
||||
exp = float(c.get('expires'))
|
||||
except (TypeError, ValueError):
|
||||
exp = -1.0
|
||||
if exp > 0 and exp <= now: # 已过期:注入了也只会被服务端判无效
|
||||
continue
|
||||
alive.append(c)
|
||||
return alive or None
|
||||
|
||||
|
||||
def _inject_item(c):
|
||||
"""快照里的 Cookie → page.set.cookies 需要的结构(只带有效字段)。"""
|
||||
item = {
|
||||
'name': c.get('name'),
|
||||
'value': c.get('value'),
|
||||
'domain': c.get('domain') or config.BACKEND_HOST,
|
||||
'path': c.get('path') or '/',
|
||||
}
|
||||
try:
|
||||
exp = float(c.get('expires'))
|
||||
except (TypeError, ValueError):
|
||||
exp = -1.0
|
||||
if exp > 0:
|
||||
item['expires'] = exp # 会话级 Cookie(-1)不传 expires,交给浏览器当会话 Cookie
|
||||
if c.get('secure'):
|
||||
item['secure'] = True
|
||||
if c.get('httpOnly'):
|
||||
item['httpOnly'] = True
|
||||
same_site = str(c.get('sameSite') or '').strip()
|
||||
if same_site in ('Strict', 'Lax', 'None'):
|
||||
item['sameSite'] = same_site
|
||||
return item
|
||||
|
||||
|
||||
def restore_cookies(page):
|
||||
"""把上次落盘的登录态注入刚启动的浏览器,返回注入条数(0=无可用缓存)。"""
|
||||
cookies = _load_cookies()
|
||||
if not cookies:
|
||||
config.log('无可用登录态快照(首次运行或已过期)', 'DEBUG')
|
||||
return 0
|
||||
|
||||
items = [_inject_item(c) for c in cookies
|
||||
if 'douyin.com' in (c.get('domain') or '')
|
||||
and not any(s in (c.get('domain') or '') for s in _INJECT_SKIP_DOMAINS)]
|
||||
if not items:
|
||||
return 0
|
||||
injected = False
|
||||
try:
|
||||
# 浏览器级注入(Storage.setCookies):此时页面还是 about:blank,不依赖加载完成
|
||||
page.browser.set.cookies(items)
|
||||
injected = True
|
||||
except Exception as e:
|
||||
config.log(f'浏览器级注入失败,改走标签页注入:{e}', 'DEBUG')
|
||||
if not injected:
|
||||
try:
|
||||
page.set.cookies(items)
|
||||
injected = True
|
||||
except Exception as e:
|
||||
config.log(f'注入缓存 Cookies 失败(改走 profile / 人工登录):{e}', 'WARN')
|
||||
return 0
|
||||
|
||||
saved_at = ''
|
||||
try:
|
||||
with open(config.META_FILE, encoding='utf-8') as f:
|
||||
saved_at = str((json.load(f) or {}).get('saved_at') or '')
|
||||
except Exception:
|
||||
pass
|
||||
config.log(f'已注入缓存登录态:{len(items)} 个 douyin Cookie'
|
||||
+ (f'(快照时间 {saved_at})' if saved_at else '')
|
||||
+ f',含会话票据={"是" if _has_session_cookie(items) else "否"}')
|
||||
return len(items)
|
||||
|
||||
|
||||
def clear_snapshot():
|
||||
"""删除登录态快照文件(强制重新登录时用,避免下次又被旧账号缓存带进去)。"""
|
||||
for path in (config.COOKIE_FILE, config.COOKIE_HEADER_FILE,
|
||||
config.TOKEN_FILE, config.META_FILE):
|
||||
try:
|
||||
if os.path.exists(path):
|
||||
os.remove(path)
|
||||
except Exception as e:
|
||||
config.log(f'删除登录态快照失败(忽略):{path} {e}', 'WARN')
|
||||
|
||||
|
||||
def clear_browser_cookies(page):
|
||||
"""清掉浏览器里的 Cookies(localStorage 保留,不影响页面偏好配置)。"""
|
||||
if page is None:
|
||||
return
|
||||
try:
|
||||
page.clear_cache(session_storage=False, local_storage=False, cache=False, cookies=True)
|
||||
return
|
||||
except Exception as e:
|
||||
config.log(f'清除浏览器 Cookies 失败(改走 CDP):{e}', 'DEBUG')
|
||||
try:
|
||||
page.set.cookies.clear() # Network.clearBrowserCookies
|
||||
except Exception as e:
|
||||
config.log(f'清除浏览器 Cookies 失败:{e}', 'WARN')
|
||||
|
||||
|
||||
def _wait_backend(page):
|
||||
"""等人工完成「登录 + 验证码」,返回 True/False。"""
|
||||
deadline = time.time() + config.LOGIN_WAIT
|
||||
@@ -211,26 +337,34 @@ def _wait_backend(page):
|
||||
def ensure_login(page, force=False):
|
||||
"""确保 page 已进入来客后台;必要时停在前台等人工登录。
|
||||
|
||||
force=True 跳过 profile 残留会话试探,直接等人工登录(换账号/排查问题用)。
|
||||
复用顺序:落盘快照注入(restore_cookies)→ profile 残留会话 → 人工登录。
|
||||
force=True 先清空浏览器 Cookies 与本地快照,再等人工重新登录(换账号/排查问题用)。
|
||||
"""
|
||||
if force:
|
||||
config.log('强制重新登录:清除浏览器 Cookies 与本地登录态快照', 'WARN')
|
||||
clear_browser_cookies(page)
|
||||
clear_snapshot()
|
||||
else:
|
||||
restore_cookies(page)
|
||||
|
||||
config.log(f'打开抖音来客:{config.HOME_URL}')
|
||||
page.get(config.HOME_URL)
|
||||
time.sleep(config.POLL_SECONDS)
|
||||
|
||||
# 复用 profile 残留会话:能直接进后台就不用打扰用户
|
||||
# 复用缓存(注入的 Cookie + profile 残留):能直接进后台就不用打扰用户
|
||||
if not force:
|
||||
reuse_end = time.time() + config.BACKEND_STABLE + 10
|
||||
while time.time() < reuse_end:
|
||||
state = classify(page.url)
|
||||
if state == 'backend' and _has_session_cookie(_cookies(page)):
|
||||
config.log(f'profile 残留会话有效,直接复用(URL={page.url})')
|
||||
config.log(f'缓存登录态有效,直接复用(URL={page.url})')
|
||||
time.sleep(config.SETTLE_WAIT)
|
||||
_save_login_state(page)
|
||||
return True, '复用缓存登录态'
|
||||
if state == 'login':
|
||||
break
|
||||
time.sleep(config.POLL_SECONDS)
|
||||
config.log('profile 无有效会话,转人工登录', 'WARN')
|
||||
config.log('缓存登录态已失效,转人工登录', 'WARN')
|
||||
|
||||
config.log(f'需要人工登录(含验证码/滑块),最多等待 {config.LOGIN_WAIT} 秒…')
|
||||
if not _wait_backend(page):
|
||||
|
||||
Reference in New Issue
Block a user