test_jfbym_live.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Live-test JfbymSliderSolver with the REAL token from docs/passwd.txt.
  2. Downloads jfbym's own sample slider image (from the 22222 product page)
  3. and runs solve_distance against it. Verifies:
  4. 1. token is valid (not 10003)
  5. 2. account has balance (not 10002)
  6. 3. response parses to an int distance (not 10007)
  7. Cost: 0.008 yuan per run (one 22222 call).
  8. """
  9. import asyncio
  10. import base64
  11. import sys
  12. from pathlib import Path
  13. _HERE = Path(__file__).resolve().parent
  14. sys.path.insert(0, str(_HERE.parent / "src"))
  15. from cq2_sdk.env_loader import load_env
  16. load_env(_HERE.parent / ".env")
  17. import httpx
  18. from cq2_sdk.login.jfbym import JfbymError, JfbymSliderSolver
  19. # 官方示例图 (来自 /test/286.html 产品页, 单图滑块 22222 的例图)
  20. SAMPLES = [
  21. "https://www.jfbym.com/static/pic/hk/dthk/4.bmp",
  22. "https://obs.jfbym.com/public/upload/22222_1.png",
  23. "https://obs.jfbym.com/public/upload/22222_2.png",
  24. ]
  25. async def main() -> int:
  26. solver = JfbymSliderSolver() # token from .env
  27. if not solver.token:
  28. print("FAIL: CQ2_JFBYM_TOKEN not set")
  29. return 1
  30. print(f"token: {solver.token[:8]}...{solver.token[-4:]}")
  31. async with httpx.AsyncClient(timeout=15, verify=False) as c:
  32. img = None
  33. for url in SAMPLES:
  34. try:
  35. r = await c.get(url)
  36. if r.status_code == 200 and len(r.content) > 1000:
  37. img = base64.b64encode(r.content).decode()
  38. print(f"sample image: {url} ({len(r.content)} bytes)")
  39. break
  40. except Exception as e:
  41. print(f" [skip] {url}: {type(e).__name__}")
  42. if not img:
  43. print("FAIL: could not download any sample image")
  44. return 1
  45. print("\ncalling jfbym customApi (type 22222, costs 0.008 yuan)...")
  46. try:
  47. distance = await solver.solve_distance(img)
  48. print(f"[OK] recognized distance = {distance} px")
  49. print(" token valid + balance OK + response parses correctly")
  50. except JfbymError as e:
  51. print(f"[FAIL] jfbym code={e.code}: {e.msg}")
  52. if e.code == 10002:
  53. print(" → 需要充值 https://console.jfbym.com/own/charge/index.html")
  54. elif e.code == 10003:
  55. print(" → token 无效, 检查 docs/passwd.txt 是否复制完整")
  56. return 1
  57. finally:
  58. await solver.close()
  59. return 0
  60. if __name__ == "__main__":
  61. sys.exit(asyncio.run(main()))