get_imgcode.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import requests
  2. import feapder.setting as 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, spidercode=None):
  60. """
  61. 超级鹰识别平台
  62. pic_type,详情查询地址: https://www.chaojiying.com/price.html
  63. @param str spidercode: 爬虫代码
  64. """
  65. files = _pack_file(file)
  66. url = f"{setting.CAPTCHA_URL}/v1/images/discern?pic_type={pic_type}"
  67. if spidercode is not None:
  68. url = f"{setting.CAPTCHA_URL}/v1/images/discern?pic_type={pic_type}&jy_code={spidercode}"
  69. headers = {'accept': 'application/json'}
  70. data = {
  71. 'grant_type': '',
  72. 'username': 'jianyu001',
  73. 'password': '123qwe!A',
  74. 'scope': '',
  75. 'client_id': '',
  76. 'client_secret': ''
  77. }
  78. response = requests.post(url, headers=headers, data=data, files=files, timeout=10)
  79. json_resp = response.json()
  80. '''code 返回0时,打码平台正常返回数据'''
  81. pic_str = json_resp["r"]["pic_str"]
  82. pic_id = json_resp["r"]["pic_id"]
  83. if 0 == json_resp["code"]:
  84. return pic_str, pic_id
  85. return None, pic_id
  86. def chaojiying_report(pic_id: str):
  87. """超级鹰平台识别验证码错误时,提交识别错误的验证码pic_id"""
  88. url = f"{setting.CAPTCHA_URL}/v1/images/report_err?pic_id={pic_id}"
  89. headers = {
  90. 'accept': 'application/json',
  91. 'Content-Type': 'application/x-www-form-urlencoded'
  92. }
  93. data = {
  94. 'grant_type': '',
  95. 'username': 'jianyu001',
  96. 'password': '123qwe!A',
  97. 'scope': '',
  98. 'client_id': '',
  99. 'client_secret': ''
  100. }
  101. response = requests.post(url, headers=headers, data=data, timeout=10)
  102. '''
  103. 回调成功:{'msg': 'OK', 'code': 0}
  104. 此接口不能随便调用!程序逻辑里要这样判断: 如果 识别结果是错的 再调用 报错返分 接口。 如果没有这个判断或是无法判断,就不要调用!
  105. '''
  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")