get_imgcode.py 3.9 KB

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