feat: 新增 --upload-only 补传模式与 reupload.bat(NAS 重启后无需重新登录);上传内网地址自动绕过系统代理
This commit is contained in:
1 parent
73462d94a0
commit
4ad6ecd5c9
4 files changed
+133
-24
No files matched your search
+2
-4
@@ -7,10 +7,8 @@ timezone: Asia/Shanghai
|
|||||||
api:
|
api:
|
||||||
base_url: https://fs.szfx.com
|
base_url: https://fs.szfx.com
|
||||||
order_path: /saasmerchant/pcweb/order/quickpayorder/list
|
order_path: /saasmerchant/pcweb/order/quickpayorder/list
|
||||||
login_url: >-
|
# 必须写在一行:用 >- 折行会在 query 里折出空格,导致登录链接失效
|
||||||
https://fspass.szfx.com/ucenter/userlogin?platform=saas
|
login_url: "https://fspass.szfx.com/ucenter/userlogin?platform=saas&redirect_url=https%3A%2F%2Ffs.szfx.com%2FMMS%23%2F&return_url=https%3A%2F%2Ffs.szfx.com%2Fsaasmerchant%2Fsetstoken"
|
||||||
&redirect_url=https%3A%2F%2Ffs.szfx.com%2FMMS%23%2F
|
|
||||||
&return_url=https%3A%2F%2Ffs.szfx.com%2Fsaasmerchant%2Fsetstoken
|
|
||||||
timeout: 20
|
timeout: 20
|
||||||
max_retries: 2
|
max_retries: 2
|
||||||
|
|
||||||
|
|||||||
+2
-1
@@ -244,7 +244,8 @@ def load_config(path: str | os.PathLike | None = None) -> Config:
|
|||||||
api=ApiCfg(
|
api=ApiCfg(
|
||||||
base_url=data["api"]["base_url"],
|
base_url=data["api"]["base_url"],
|
||||||
order_path=data["api"]["order_path"],
|
order_path=data["api"]["order_path"],
|
||||||
login_url=data["api"]["login_url"],
|
# YAML 多行折叠可能往 query 里折出空格,登录链接会失效,这里兜底清掉
|
||||||
|
login_url="".join(str(data["api"]["login_url"]).split()),
|
||||||
timeout=int(data["api"]["timeout"]),
|
timeout=int(data["api"]["timeout"]),
|
||||||
max_retries=int(data["api"]["max_retries"]),
|
max_retries=int(data["api"]["max_retries"]),
|
||||||
),
|
),
|
||||||
|
|||||||
+107
-19
@@ -20,6 +20,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import time
|
import time
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
@@ -29,42 +30,90 @@ import requests
|
|||||||
from fengxiang.config import load_config
|
from fengxiang.config import load_config
|
||||||
|
|
||||||
MMS_BASE = "https://fs.szfx.com/MMS"
|
MMS_BASE = "https://fs.szfx.com/MMS"
|
||||||
LOGIN_TIMEOUT_MS = 600_000 # 10 分钟
|
LOGIN_TIMEOUT_SEC = 1800 # 30 分钟,够你在忙的时候回来登
|
||||||
|
PROFILE_DIR = Path("data/browser-profile") # 持久化会话,减少重复登录
|
||||||
|
|
||||||
|
|
||||||
def login_with_browser(login_url: str) -> dict[str, str] | None:
|
def prefill_credentials(page, account: str, password: str) -> None:
|
||||||
|
"""可选:自动填入账号密码,验证码仍由人工完成。"""
|
||||||
|
try:
|
||||||
|
page.fill('input[placeholder*="手机号"]', account, timeout=8000)
|
||||||
|
page.fill('input[type="password"]', password, timeout=8000)
|
||||||
|
print(" 已自动填入账号密码,请手动完成验证码并点登录")
|
||||||
|
except Exception as exc: # noqa: BLE001 - 填不上就交给人工
|
||||||
|
print(f" 自动填写失败({exc}),请手动输入")
|
||||||
|
|
||||||
|
|
||||||
|
def login_with_browser(login_url: str, timeout_sec: int = LOGIN_TIMEOUT_SEC,
|
||||||
|
account: str | None = None, password: str | None = None) -> dict[str, str] | None:
|
||||||
try:
|
try:
|
||||||
from playwright.sync_api import sync_playwright
|
from playwright.sync_api import sync_playwright
|
||||||
except ImportError:
|
except ImportError:
|
||||||
print("[错误] 未安装 playwright:pip install playwright && playwright install chromium", file=sys.stderr)
|
print("[错误] 未安装 playwright:pip install playwright && playwright install chromium", file=sys.stderr)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
PROFILE_DIR.mkdir(parents=True, exist_ok=True)
|
||||||
print("[1/3] 启动浏览器,请在窗口中完成登录(账号密码 + 验证码)...")
|
print("[1/3] 启动浏览器,请在窗口中完成登录(账号密码 + 验证码)...")
|
||||||
with sync_playwright() as p:
|
with sync_playwright() as p:
|
||||||
browser = p.chromium.launch(headless=False, args=["--start-maximized"])
|
ctx = p.chromium.launch_persistent_context(
|
||||||
context = browser.new_context(no_viewport=True)
|
str(PROFILE_DIR),
|
||||||
page = context.new_page()
|
headless=False,
|
||||||
page.goto(login_url)
|
args=["--start-maximized"],
|
||||||
|
no_viewport=True,
|
||||||
print("[2/3] 等待登录完成(最多 10 分钟)...")
|
)
|
||||||
|
page = ctx.pages[0] if ctx.pages else ctx.new_page()
|
||||||
try:
|
try:
|
||||||
page.wait_for_url("**/MMS**", timeout=LOGIN_TIMEOUT_MS)
|
page.goto(login_url, wait_until="domcontentloaded", timeout=60_000)
|
||||||
print(" 登录成功,正在提取 Cookie...")
|
except Exception as exc: # noqa: BLE001 - 网络慢不该直接判死
|
||||||
|
print(f" 打开登录页失败:{exc}", file=sys.stderr)
|
||||||
|
|
||||||
|
if account and password:
|
||||||
|
prefill_credentials(page, account, password)
|
||||||
|
|
||||||
|
print(f"[2/3] 等待登录完成(最多 {timeout_sec // 60} 分钟,请勿关闭窗口)...")
|
||||||
|
shot = PROFILE_DIR.parent / "login-state.png"
|
||||||
|
deadline = time.time() + timeout_sec
|
||||||
|
last_note = 0.0
|
||||||
|
ok = False
|
||||||
|
while time.time() < deadline:
|
||||||
|
try:
|
||||||
|
if "fs.szfx.com/mms" in page.url.lower():
|
||||||
|
ok = True
|
||||||
|
break
|
||||||
|
page.wait_for_timeout(1000)
|
||||||
except Exception:
|
except Exception:
|
||||||
print(" 等待超时,已退出。请重新运行。", file=sys.stderr)
|
print(" 浏览器窗口已被关闭,退出。", file=sys.stderr)
|
||||||
browser.close()
|
ctx.close()
|
||||||
|
return None
|
||||||
|
if time.time() - last_note >= 60:
|
||||||
|
last_note = time.time()
|
||||||
|
left = int((deadline - time.time()) / 60) + 1
|
||||||
|
# 截图留证:窗口到底渲染成什么样
|
||||||
|
try:
|
||||||
|
page.screenshot(path=str(shot))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
print(f" [心跳] 仍在等待登录,剩余约 {left} 分钟(当前:{page.url[:80]})")
|
||||||
|
|
||||||
|
if not ok:
|
||||||
|
try:
|
||||||
|
print(f" 等待超时,当前页面:{page.url}", file=sys.stderr)
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
print(" 已退出,请重新运行。", file=sys.stderr)
|
||||||
|
ctx.close()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
print(" 登录成功,正在提取 Cookie...")
|
||||||
# 访问一次首页,确保必要的 Cookie 都下发了
|
# 访问一次首页,确保必要的 Cookie 都下发了
|
||||||
try:
|
try:
|
||||||
page.goto(MMS_BASE)
|
page.goto(MMS_BASE, wait_until="networkidle", timeout=30_000)
|
||||||
page.wait_for_load_state("networkidle", timeout=30_000)
|
|
||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
|
|
||||||
cookies = {c["name"]: c["value"] for c in context.cookies()}
|
cookies = {c["name"]: c["value"] for c in ctx.cookies()}
|
||||||
browser.close()
|
ctx.close()
|
||||||
|
|
||||||
print(f"[3/3] 提取到 {len(cookies)} 个 Cookie")
|
print(f"[3/3] 提取到 {len(cookies)} 个 Cookie")
|
||||||
return cookies
|
return cookies
|
||||||
@@ -91,11 +140,20 @@ def upload(server: str, token: str, cookies: dict[str, str]) -> bool:
|
|||||||
headers = {"Content-Type": "application/json"}
|
headers = {"Content-Type": "application/json"}
|
||||||
if token:
|
if token:
|
||||||
headers["X-Auth-Token"] = token
|
headers["X-Auth-Token"] = token
|
||||||
|
# 局域网地址必须绕过系统代理:本机代理连不上内网会把请求变成 502
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
|
host = urlparse(url).hostname or ""
|
||||||
|
session = requests.Session()
|
||||||
|
if host.startswith(("192.168.", "10.", "127.", "localhost", "172.1", "172.2", "172.3")):
|
||||||
|
session.trust_env = False
|
||||||
|
print(f" 目标为内网地址,已绕过系统代理")
|
||||||
print(f" 上传到 {url} ...")
|
print(f" 上传到 {url} ...")
|
||||||
try:
|
try:
|
||||||
resp = requests.post(url, json={"cookies": cookies}, headers=headers, timeout=20)
|
resp = session.post(url, json={"cookies": cookies}, headers=headers, timeout=20)
|
||||||
except requests.RequestException as exc:
|
except requests.RequestException as exc:
|
||||||
print(f" 上传失败(网络):{exc}", file=sys.stderr)
|
print(f" 上传失败(网络):{exc}", file=sys.stderr)
|
||||||
|
print(" 若 NAS 刚开机,等 1-2 分钟再试;或确认 192.168.8.210 是否在线。", file=sys.stderr)
|
||||||
return False
|
return False
|
||||||
print(f" HTTP {resp.status_code} {resp.text[:300]}")
|
print(f" HTTP {resp.status_code} {resp.text[:300]}")
|
||||||
return resp.status_code == 200
|
return resp.status_code == 200
|
||||||
@@ -106,10 +164,41 @@ def main() -> int:
|
|||||||
parser.add_argument("--upload", action="store_true", help="登录成功后上传到服务器")
|
parser.add_argument("--upload", action="store_true", help="登录成功后上传到服务器")
|
||||||
parser.add_argument("--server", default=None, help="服务器地址,如 http://192.168.8.210:52100")
|
parser.add_argument("--server", default=None, help="服务器地址,如 http://192.168.8.210:52100")
|
||||||
parser.add_argument("--token", default=None, help="管理接口 token")
|
parser.add_argument("--token", default=None, help="管理接口 token")
|
||||||
|
parser.add_argument("--timeout", type=int, default=LOGIN_TIMEOUT_SEC, help="等待登录的秒数")
|
||||||
|
parser.add_argument("--account", default=None, help="可选:自动填入的账号/手机号")
|
||||||
|
parser.add_argument("--password", default=None, help="可选:自动填入的密码")
|
||||||
|
parser.add_argument(
|
||||||
|
"--upload-only",
|
||||||
|
action="store_true",
|
||||||
|
help="不打开浏览器:直接把本地已保存的 Cookie 校验并上传(服务器恢复后补传用)",
|
||||||
|
)
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
cfg = load_config()
|
cfg = load_config()
|
||||||
cookies = login_with_browser(cfg.api.login_url)
|
out = Path(cfg.paths.cookies)
|
||||||
|
|
||||||
|
# 补传模式:NAS 重启/网络恢复后,用已保存的有效 Cookie 直接推上去,不用再登录
|
||||||
|
if args.upload_only:
|
||||||
|
if not out.exists():
|
||||||
|
print(f"[错误] 本地没有 Cookie 文件:{out},请先正常登录一次", file=sys.stderr)
|
||||||
|
return 3
|
||||||
|
cookies = json.loads(out.read_text(encoding="utf-8"))
|
||||||
|
print(f"[补传] 读取本地 Cookie({len(cookies)} 个):{out}")
|
||||||
|
if not verify(cfg, cookies):
|
||||||
|
print("本地 Cookie 已失效,需要重新登录(去掉 --upload-only 再跑)", file=sys.stderr)
|
||||||
|
return 2
|
||||||
|
server = args.server or os.environ.get("FX_SERVER")
|
||||||
|
token = args.token or os.environ.get("FX_ADMIN_TOKEN") or cfg.admin.token
|
||||||
|
if not server:
|
||||||
|
print("[错误] 未指定服务器:用 --server 或环境变量 FX_SERVER", file=sys.stderr)
|
||||||
|
return 3
|
||||||
|
return 0 if upload(server, token, cookies) else 4
|
||||||
|
|
||||||
|
print(f" 登录页:{cfg.api.login_url[:100]}")
|
||||||
|
account = args.account or os.environ.get("FX_ACCOUNT")
|
||||||
|
password = args.password or os.environ.get("FX_PASSWORD")
|
||||||
|
cookies = login_with_browser(cfg.api.login_url, timeout_sec=args.timeout,
|
||||||
|
account=account, password=password)
|
||||||
if not cookies:
|
if not cookies:
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
@@ -124,7 +213,6 @@ def main() -> int:
|
|||||||
print(f" 已保存到 {out}")
|
print(f" 已保存到 {out}")
|
||||||
|
|
||||||
if args.upload:
|
if args.upload:
|
||||||
import os
|
|
||||||
server = args.server or os.environ.get("FX_SERVER")
|
server = args.server or os.environ.get("FX_SERVER")
|
||||||
token = args.token or os.environ.get("FX_ADMIN_TOKEN") or cfg.admin.token
|
token = args.token or os.environ.get("FX_ADMIN_TOKEN") or cfg.admin.token
|
||||||
if not server:
|
if not server:
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
@echo off
|
||||||
|
chcp 65001 >nul
|
||||||
|
title 补传丰享 Cookie 到服务器
|
||||||
|
cd /d "%~dp0"
|
||||||
|
|
||||||
|
echo ============================================
|
||||||
|
echo 用本地已保存的 Cookie 补传到服务器
|
||||||
|
echo (不打开浏览器,NAS 恢复后用这个)
|
||||||
|
echo ============================================
|
||||||
|
echo.
|
||||||
|
|
||||||
|
set "PYTHON="
|
||||||
|
if exist ".venv\Scripts\python.exe" set "PYTHON=.venv\Scripts\python.exe"
|
||||||
|
if not defined PYTHON if defined FX_PYTHON set "PYTHON=%FX_PYTHON%"
|
||||||
|
if not defined PYTHON set "PYTHON=python"
|
||||||
|
|
||||||
|
if not defined FX_SERVER set "FX_SERVER=http://192.168.8.210:52100"
|
||||||
|
|
||||||
|
"%PYTHON%" fengxiang_scraper.py --upload-only --server "%FX_SERVER%"
|
||||||
|
echo.
|
||||||
|
echo 若提示 Cookie 已失效:改跑 login.bat 重新登录一次即可。
|
||||||
|
pause
|
||||||
Reference in new issue
Block a user