parse-resp.py 764 B

123456789101112131415161718192021
  1. import json, re, sys
  2. # stdin管道接收响应(宿主curl | 容器python)
  3. raw = sys.stdin.read()
  4. d = json.loads(raw)
  5. text = json.dumps(d.get("data", {}))
  6. m = re.search(r"sk-[A-Za-z0-9_-]{20,}", text)
  7. tok = m.group(0) if m else ""
  8. print("got_len:", len(tok))
  9. if tok:
  10. p = "/opt/data/fmode-identity.json"
  11. ident = json.load(open(p))
  12. ident["newapi_token"] = tok
  13. json.dump(ident, open(p, "w"), ensure_ascii=False, indent=2)
  14. cfg = open("/opt/data/config.yaml").read()
  15. if "api_key:" in cfg:
  16. cfg = re.sub(r"( api_key: ).*", lambda _: " api_key: " + tok, cfg, count=1)
  17. open("/opt/data/config.yaml", "w").write(cfg)
  18. print("identity+config updated")
  19. else:
  20. print("NO_TOKEN in response, keys:", list(d.get("data", {}).keys()))