confirm_only.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """仅确认:login + trade-confirmations/operate(读 config.json)"""
  4. import json
  5. import sys
  6. import time
  7. from pathlib import Path
  8. import requests
  9. ROOT = Path(__file__).resolve().parent
  10. CFG = ROOT / "config.json"
  11. def load_cfg():
  12. with CFG.open("r", encoding="utf-8") as f:
  13. return json.load(f)
  14. def resolve_mafile(mafile: str) -> str:
  15. p = Path(mafile)
  16. if not p.is_absolute():
  17. p = ROOT / p
  18. return str(p.resolve())
  19. def normalize_proxy(proxy: str):
  20. raw = (proxy or "").strip()
  21. if not raw:
  22. return None
  23. if raw.lower().startswith(("http://", "https://", "socks5://", "socks4://", "socks://")):
  24. return raw
  25. return f"http://{raw}"
  26. def login(cfg, proxy):
  27. body = {
  28. "Username": cfg["username"],
  29. "Password": cfg["password"],
  30. "MaFilePath": resolve_mafile(cfg["mafile"]),
  31. }
  32. if proxy:
  33. body["ProxyUrl"] = proxy
  34. api = cfg.get("api_base") or "http://localhost:5000"
  35. for attempt in range(1, 6):
  36. print(f"[login] attempt {attempt}/5")
  37. r = requests.post(f"{api.rstrip('/')}/login", json=body, timeout=90)
  38. print(f"[login] HTTP {r.status_code} {r.text[:300]}")
  39. if r.status_code == 200:
  40. return r.json(), api
  41. time.sleep(3)
  42. raise RuntimeError("login failed")
  43. def main():
  44. if len(sys.argv) < 2:
  45. print("用法: python confirm_only.py <confirmation_id> [proxy]", file=sys.stderr)
  46. return 2
  47. confirmation_id = sys.argv[1]
  48. cfg = load_cfg()
  49. proxy = normalize_proxy(sys.argv[2] if len(sys.argv) > 2 else cfg.get("proxy") or "")
  50. data, api = login(cfg, proxy)
  51. sess = data["session"]
  52. session = {
  53. "SteamId": sess["steamId"],
  54. "AccessToken": sess["accessToken"],
  55. "RefreshToken": sess["refreshToken"],
  56. "SessionId": sess["sessionId"],
  57. }
  58. mafile = resolve_mafile(cfg["mafile"])
  59. list_body = {"MaFilePath": mafile, "Session": session}
  60. if proxy:
  61. list_body["ProxyUrl"] = proxy
  62. print("[list] fetching confirmations...")
  63. lr = requests.post(f"{api.rstrip('/')}/trade-confirmations/list", json=list_body, timeout=60)
  64. print(f"[list] HTTP {lr.status_code} {lr.text[:2000]}")
  65. if lr.status_code == 200:
  66. lj = lr.json()
  67. if lj.get("session"):
  68. s = lj["session"]
  69. session = {
  70. "SteamId": s.get("steamId", session["SteamId"]),
  71. "AccessToken": s.get("accessToken", session["AccessToken"]),
  72. "RefreshToken": s.get("refreshToken", session["RefreshToken"]),
  73. "SessionId": s.get("sessionId", session["SessionId"]),
  74. }
  75. op_body = {
  76. "MaFilePath": mafile,
  77. "Session": session,
  78. "ConfirmationId": int(confirmation_id),
  79. "Operation": "accept",
  80. }
  81. if proxy:
  82. op_body["ProxyUrl"] = proxy
  83. for attempt in range(1, 11):
  84. print(f"[confirm] attempt {attempt}/10 ConfirmationId={confirmation_id}")
  85. r = requests.post(f"{api.rstrip('/')}/trade-confirmations/operate", json=op_body, timeout=60)
  86. print(f"[confirm] HTTP {r.status_code}")
  87. print(r.text[:2000])
  88. if r.status_code == 200:
  89. j = r.json()
  90. if j.get("success") in (True, 1, "1"):
  91. print("[DONE] confirm success")
  92. return 0
  93. if j.get("session"):
  94. s = j["session"]
  95. op_body["Session"] = {
  96. "SteamId": s.get("steamId", session["SteamId"]),
  97. "AccessToken": s.get("accessToken", session["AccessToken"]),
  98. "RefreshToken": s.get("refreshToken", session["RefreshToken"]),
  99. "SessionId": s.get("sessionId", session["SessionId"]),
  100. }
  101. time.sleep(3)
  102. print("[DONE] confirm failed")
  103. return 1
  104. if __name__ == "__main__":
  105. try:
  106. sys.stdout.reconfigure(encoding="utf-8")
  107. except Exception:
  108. pass
  109. raise SystemExit(main())