或者这个:
```
python3 - <<'PY'
import json
import os
import urllib.request
from datetime import datetime, timezone
AUTH_PATH = os.path.expanduser("~/.codex/auth.json")
URL = "
https://chatgpt.com/backend-api/wham/rate-limit-reset-credits"
def to_local_time(value):
if not value:
return None
try:
if isinstance(value, (int, float)):
# 兼容秒或毫秒时间戳
if value > 10_000_000_000:
value = value / 1000
dt = datetime.fromtimestamp(value, tz=timezone.utc)
else:
s = str(value).replace("Z", "+00:00")
dt = datetime.fromisoformat(s)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return dt.astimezone().strftime("%Y-%m-%d %H:%M:%S %Z")
except Exception:
return value
with open(AUTH_PATH, "r", encoding="utf-8") as f:
auth = json.load(f)
token = auth.get("tokens", {}).get("access_token")
if not token:
raise SystemExit("未找到 tokens.access_token")
req = urllib.request.Request(
URL,
headers={
"Authorization": f"Bearer {token}",
"Accept": "application/json",
},
method="GET",
)
try:
with urllib.request.urlopen(req, timeout=20) as resp:
status = resp.status
body = resp.read().decode("utf-8")
except urllib.error.HTTPError as e:
status = e.code
body = e.read().decode("utf-8", errors="replace")
if status == 401:
print("HTTP 401:凭证失效,或请求没有带正确的 Authorization header")
raise SystemExit(1)
if status < 200 or status >= 300:
print(f"HTTP {status}")
print("请求失败;未输出敏感信息。")
raise SystemExit(1)
data = json.loads(body)
available_count = data.get("available_count")
credits = data.get("credits", [])
print("available_count:", available_count)
print()
for i, c in enumerate(credits, 1):
print(f"credit #{i}")
print(" status:", c.get("status"))
print(" title:", c.get("title"))
print(" granted_at:", to_local_time(c.get("granted_at")))
print(" expires_at:", to_local_time(c.get("expires_at")))
PY
```