get_imgcode.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import requests
  2. __all__ = [
  3. "swordfish_platform",
  4. "chaojiying_platform",
  5. "chaojiying_report",
  6. "get_code",
  7. "get_code_det",
  8. "arithmetic_captcha",
  9. ]
  10. headers = {"accept": "application/json"}
  11. def _pack_file(file):
  12. """包装验证码格式"""
  13. if isinstance(file, str) and file.startswith("data:image"):
  14. img_file = {"file": file}
  15. elif isinstance(file, bytes):
  16. img_file = {"file": file}
  17. else:
  18. with open(file, "rb") as f:
  19. img_bytes = f.read()
  20. img_file = {"file": img_bytes}
  21. return img_file
  22. def _simple_captcha(file):
  23. """
  24. 普通验证码
  25. @param file: 验证码 - 可以是图片或者图片base64编码
  26. @return:
  27. """
  28. url = "http://pycaptcha.spdata.jianyu360.com/v1/images/verify"
  29. files = _pack_file(file)
  30. r = requests.post(url, headers=headers, files=files, stream=True, timeout=10)
  31. rp_json = r.json()
  32. if "msg" in rp_json and "success" == rp_json["msg"]:
  33. return str(rp_json["r"]["code"])
  34. return None
  35. def _arithmetic_captcha(file):
  36. """算术验证码"""
  37. url = "http://pycaptcha.spdata.jianyu360.com/v1/images/arithmetic"
  38. files = _pack_file(file)
  39. r = requests.post(url, headers=headers, files=files, stream=True, timeout=10)
  40. json_resp = r.json()
  41. if "msg" in json_resp and "success" == json_resp["msg"]:
  42. return str(json_resp["r"]["code"])
  43. return None
  44. def _get_click_verify_captcha(file):
  45. """点触式验证码"""
  46. url = "http://pycaptcha.spdata.jianyu360.com/v1/images/verify_det"
  47. files = {"image_content": v for k, v in _pack_file(file).items()}
  48. r = requests.post(url, headers=headers, files=files, stream=True, timeout=10)
  49. return r.json()
  50. def swordfish_platform(file, mode="simple"):
  51. """剑鱼验证码识别平台"""
  52. if mode.lower() == "arithmetic":
  53. return _arithmetic_captcha(file)
  54. elif mode.lower() == "det":
  55. return _get_click_verify_captcha(file)
  56. else:
  57. return _simple_captcha(file)
  58. def chaojiying_platform(file, pic_type: int):
  59. """
  60. 超级鹰识别平台
  61. pic_type,详情查询地址: https://www.chaojiying.com/price.html
  62. """
  63. files = _pack_file(file)
  64. url = f"http://pycaptcha.spdata.jianyu360.com/v1/images/discern?pic_type={pic_type}"
  65. headers = {'accept': 'application/json'}
  66. data = {
  67. 'grant_type': '',
  68. 'username': 'jianyu001',
  69. 'password': '123qwe!A',
  70. 'scope': '',
  71. 'client_id': '',
  72. 'client_secret': ''
  73. }
  74. response = requests.post(url, headers=headers, data=data, files=files, timeout=10)
  75. json_resp = response.json()
  76. '''code 返回0时,打码平台正常返回数据'''
  77. pic_str = json_resp["r"]["pic_str"]
  78. pic_id = json_resp["r"]["pic_id"]
  79. if 0 == json_resp["code"]:
  80. return pic_str, pic_id
  81. return None, pic_id
  82. def chaojiying_report(pic_id: str):
  83. """超级鹰平台识别验证码错误时,提交识别错误的验证码pic_id"""
  84. url = f"http://pycaptcha.spdata.jianyu360.com/v1/images/report_err?pic_id={pic_id}"
  85. headers = {
  86. 'accept': 'application/json',
  87. 'Content-Type': 'application/x-www-form-urlencoded'
  88. }
  89. data = {
  90. 'grant_type': '',
  91. 'username': 'jianyu001',
  92. 'password': '123qwe!A',
  93. 'scope': '',
  94. 'client_id': '',
  95. 'client_secret': ''
  96. }
  97. response = requests.post(url, headers=headers, data=data, timeout=10)
  98. '''
  99. 回调成功:{'msg': 'OK', 'code': 0}
  100. 此接口不能随便调用!程序逻辑里要这样判断: 如果 识别结果是错的 再调用 报错返分 接口。 如果没有这个判断或是无法判断,就不要调用!
  101. '''
  102. return response.json()
  103. def get_code(file_path: str) -> dict:
  104. return swordfish_platform(file_path) or {}
  105. def get_code_det(image_bytes) -> dict:
  106. return swordfish_platform(image_bytes, mode="det")
  107. # 算术
  108. def arithmetic_captcha(image_stream):
  109. return swordfish_platform(image_stream, mode="arithmetic")