初始化项目 v2.0:中国邮政揽收系统自动填单工具
基于 HTTP API + Node.js token bridge 方案,不依赖浏览器自动化。
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
"""登录助手 — 解析浏览器导出的 cookies JSON,保存为 session.json。"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def get_session_path() -> str:
|
||||
if getattr(sys, "frozen", False):
|
||||
base = os.path.dirname(sys.executable)
|
||||
else:
|
||||
base = os.path.dirname(os.path.abspath(__file__))
|
||||
return os.path.join(base, "session.json")
|
||||
|
||||
|
||||
def parse_json_and_save(json_text: str) -> int:
|
||||
"""解析浏览器导出的 cookies JSON 数组,提取目标域名的 cookie 并保存。
|
||||
返回保存的 cookie 数量。
|
||||
"""
|
||||
raw = json.loads(json_text)
|
||||
if not isinstance(raw, list):
|
||||
raise ValueError("JSON 必须是数组格式")
|
||||
|
||||
# 过滤目标域名
|
||||
target = "10.4.188.1"
|
||||
cookies = []
|
||||
seen = set()
|
||||
for c in raw:
|
||||
domain = c.get("domain", "")
|
||||
if target not in domain:
|
||||
continue
|
||||
name = c.get("name", "").strip()
|
||||
path = c.get("path", "/")
|
||||
if not name:
|
||||
continue
|
||||
key = (name, domain, path)
|
||||
if key in seen:
|
||||
continue
|
||||
seen.add(key)
|
||||
cookies.append({
|
||||
"name": name,
|
||||
"value": c.get("value", ""),
|
||||
"domain": domain,
|
||||
"path": path,
|
||||
})
|
||||
|
||||
data = {"cookies": cookies, "saved_from": f"https://{target}"}
|
||||
session_path = get_session_path()
|
||||
os.makedirs(os.path.dirname(session_path) or ".", exist_ok=True)
|
||||
with open(session_path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False, indent=2)
|
||||
return len(cookies)
|
||||
|
||||
|
||||
def load_session_info() -> dict | None:
|
||||
"""读取已保存的 session 信息用于 UI 展示。"""
|
||||
try:
|
||||
with open(get_session_path(), "r") as f:
|
||||
return json.load(f)
|
||||
except (FileNotFoundError, json.JSONDecodeError):
|
||||
return None
|
||||
Reference in New Issue
Block a user