Files
eink-push/jm_api.py
2026-06-29 09:43:20 +08:00

44 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
橘喵 jm-devops 后端客户端 —— 直接调用免鉴权的统计接口(无需登录/token
接口(参考 jm-devops 首页 src/api/dashboard.ts
- GET /system/statistics/realtime 今日经营实时数据
- GET /system/statistics/recent-days?days 近 N 天每日数据(按日期降序、不含今天)
"""
import json
import urllib.request
import urllib.error
import config
def _get(path):
url = config.BASE_API + path
headers = {"Content-Type": "application/json;charset=utf-8", "clientid": config.CLIENT_ID}
req = urllib.request.Request(url, headers=headers, method="GET")
try:
with urllib.request.urlopen(req, timeout=30) as resp:
data = json.loads(resp.read().decode("utf-8"))
except urllib.error.HTTPError as e:
raise RuntimeError(f"HTTP {e.code}: {e.read().decode('utf-8', 'ignore')[:200]}") from e
if data.get("code") != 200:
raise RuntimeError(f"{path} 返回 code={data.get('code')}: {data.get('msg')}")
return data["data"]
def get_realtime() -> dict:
"""今日经营实时数据(单量/流水/毛利 + 环比/同比)。"""
return _get("/system/statistics/realtime")
def get_recent_days(days=30) -> list:
"""近 N 天每日经营数据API 按日期降序、不含今天)。"""
return _get(f"/system/statistics/recent-days?days={days}")["days"]
if __name__ == "__main__":
print("realtime:", json.dumps(get_realtime(), ensure_ascii=False)[:300])
print("recent[0]:", json.dumps(get_recent_days(30)[0], ensure_ascii=False))