get_imgcode.py 4.5 KB

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