get_imgcode.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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://123.57.163.80:2119/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"]).upper()
  34. return None
  35. def _arithmetic_captcha(file):
  36. """算术验证码"""
  37. url = "http://123.57.163.80:2119/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"]).upper()
  43. return None
  44. def _get_click_verify_captcha(file):
  45. """点触式验证码"""
  46. url = "http://123.57.163.80:2119/v1/images/verify_det"
  47. files = _pack_file(file)
  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. with open(file, 'rb') as f:
  64. image_bytes = f.read()
  65. files = {'file': image_bytes}
  66. url = f"http://123.57.163.80:2119/v1/images/discern?pic_type={pic_type}"
  67. headers = {'accept': 'application/json'}
  68. data = {
  69. 'grant_type': '',
  70. 'username': 'jianyu001',
  71. 'password': '123qwe!A',
  72. 'scope': '',
  73. 'client_id': '',
  74. 'client_secret': ''
  75. }
  76. response = requests.post(url, headers=headers, data=data, files=files, timeout=10)
  77. json_resp = response.json()
  78. # print(json_resp)
  79. '''code 返回0时,打码平台正常返回数据'''
  80. pic_str = json_resp["r"]["pic_str"]
  81. pic_id = json_resp["r"]["pic_id"]
  82. print("pic_id >>", pic_id)
  83. if 0 == json_resp["code"]:
  84. return pic_str
  85. def chaojiying_report(pic_id: str):
  86. """超级鹰平台识别验证码错误时,提交识别错误的验证码pic_id"""
  87. url = f"http://123.57.163.80:2119/v1/images/report_err?pic_id={pic_id}"
  88. headers = {
  89. 'accept': 'application/json',
  90. 'Content-Type': 'application/x-www-form-urlencoded'
  91. }
  92. data = {
  93. 'grant_type': '',
  94. 'username': 'jianyu001',
  95. 'password': '123qwe!A',
  96. 'scope': '',
  97. 'client_id': '',
  98. 'client_secret': ''
  99. }
  100. response = requests.post(url, headers=headers, data=data, timeout=10)
  101. '''
  102. 回调成功:{'msg': 'OK', 'code': 0}
  103. 此接口不能随便调用!程序逻辑里要这样判断: 如果 识别结果是错的 再调用 报错返分 接口。 如果没有这个判断或是无法判断,就不要调用!
  104. '''
  105. # print(response.json())
  106. return response.json()
  107. def get_code(file_path: str) -> dict:
  108. return swordfish_platform(file_path) or {}
  109. def get_code_det(image_bytes) -> dict:
  110. return swordfish_platform(image_bytes, mode="det")
  111. # 算术
  112. def arithmetic_captcha(image_stream):
  113. return swordfish_platform(image_stream, mode="arithmetic")