refactor: 重构为可部署到服务器的模块化结构(v5.0.0)
- 拆包 fengxiang/:config/client/store/notify/monitor/admin/tts,统一入口 order_monitor.py - 服务器形态:Docker 常驻,Cookie 由本机登录后 POST 上传并热加载,不再依赖桌面弹窗 - 修复:首次启动重推历史订单、speak() 阻塞主循环、夜间 sleep 10h 不可中断 - 修复:test_push.py 残留 miservice 依赖(小爱已移除)、run_monitor.bat 写死盘符 - 配置化:webhook/shopId 移出源码(环境变量 > config.yaml > 默认值) - 可观测:滚动文件日志 + /healthz + Docker HEALTHCHECK + 告警冷却 - 飞书通道保留配置位,默认关闭(原 token 已失效 code=19001) - 新增 22 项单元测试(不联网)
This commit is contained in:
1 parent
ab6a2bb7ea
commit
4ee30399bf
30 files changed
+2410
-825
No files matched your search
+47
-39
@@ -1,52 +1,60 @@
|
||||
"""命令行工具:复用 Cookie 拉取订单列表并打印。
|
||||
|
||||
python fetch_orders.py # 第 1 页,20 条
|
||||
python fetch_orders.py 2 50 # 第 2 页,50 条
|
||||
python fetch_orders.py --json # 输出原始 JSON
|
||||
"""
|
||||
丰享商家端数据抓取 - Cookie 复用模式
|
||||
已有 cookies.json 时直接调用接口,Cookie 过期再运行主脚本重新登录
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import logging
|
||||
import sys
|
||||
import requests
|
||||
|
||||
DATA_API = "https://fs.szfx.com/saasmerchant/pcweb/order/quickpayorder/list"
|
||||
SHOP_ID = "20434543575189"
|
||||
from fengxiang.client import ApiError, FengxiangClient, NetworkError, SessionExpired
|
||||
from fengxiang.config import load_config
|
||||
from fengxiang.logger import setup_logging
|
||||
|
||||
|
||||
def load_cookies():
|
||||
def main(argv: list[str]) -> int:
|
||||
cfg = load_config()
|
||||
setup_logging(cfg.paths.logs, cfg.log.level, cfg.log.max_bytes, cfg.log.backup_count)
|
||||
logging.getLogger("fengxiang").setLevel(logging.WARNING)
|
||||
|
||||
as_json = "--json" in argv
|
||||
nums = [a for a in argv if a.isdigit()]
|
||||
page = int(nums[0]) if len(nums) > 0 else 1
|
||||
page_size = int(nums[1]) if len(nums) > 1 else 20
|
||||
|
||||
monitor_cookies = None
|
||||
if cfg.paths.cookies.exists():
|
||||
with open(cfg.paths.cookies, "r", encoding="utf-8") as f:
|
||||
monitor_cookies = json.load(f)
|
||||
if not monitor_cookies:
|
||||
print(f"未找到 Cookie:{cfg.paths.cookies}\n请先运行 fengxiang_scraper.py 完成登录", file=sys.stderr)
|
||||
return 1
|
||||
|
||||
client = FengxiangClient(cfg)
|
||||
client.set_cookies(monitor_cookies)
|
||||
try:
|
||||
with open("cookies.json", "r", encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except FileNotFoundError:
|
||||
print("未找到 cookies.json,请先运行 fengxiang_scraper.py 完成登录")
|
||||
sys.exit(1)
|
||||
orders = client.list_orders(page_size=page_size, page=page)
|
||||
except SessionExpired:
|
||||
print("Cookie 已失效,请重新运行 fengxiang_scraper.py", file=sys.stderr)
|
||||
return 2
|
||||
except (ApiError, NetworkError) as exc:
|
||||
print(f"请求失败:{exc}", file=sys.stderr)
|
||||
return 3
|
||||
|
||||
|
||||
def fetch_order_list(cookies, page=1, page_size=20):
|
||||
headers = {
|
||||
"Content-Type": "application/json",
|
||||
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36",
|
||||
"Referer": "https://fs.szfx.com/MMS",
|
||||
"Origin": "https://fs.szfx.com",
|
||||
}
|
||||
payload = {"shopId": SHOP_ID, "page": page, "pageSize": page_size}
|
||||
resp = requests.post(DATA_API, json=payload, headers=headers, cookies=cookies, timeout=30)
|
||||
return resp.json()
|
||||
|
||||
|
||||
def main():
|
||||
cookies = load_cookies()
|
||||
print(f"已加载 {len(cookies)} 个 Cookie")
|
||||
|
||||
page = int(sys.argv[1]) if len(sys.argv) > 1 else 1
|
||||
result = fetch_order_list(cookies, page=page)
|
||||
|
||||
if result.get("errno") == 0:
|
||||
data = result.get("data")
|
||||
print(json.dumps(data, ensure_ascii=False, indent=2))
|
||||
elif result.get("errno") == 110003:
|
||||
print("Cookie 已过期,请重新运行 fengxiang_scraper.py 完成登录")
|
||||
if as_json:
|
||||
print(json.dumps(orders, ensure_ascii=False, indent=2))
|
||||
else:
|
||||
print(f"请求失败: {result}")
|
||||
print(f"共 {len(orders)} 条(第 {page} 页,每页 {page_size})")
|
||||
for o in orders:
|
||||
cents = o.get("amountPayable") or 0
|
||||
print(f" {o.get('orderId')} {cents / 100:>8.2f}元 "
|
||||
f"{o.get('shopName', '')} {o.get('payTypeName', '')}")
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
sys.exit(main(sys.argv[1:]))
|
||||
Reference in new issue
Block a user