From 560ee441613e6b1e2a389b9f249595eca0cbdf05 Mon Sep 17 00:00:00 2001 From: Denis Pitkov Date: Sun, 26 Feb 2023 03:57:24 +0200 Subject: [PATCH] Update captcha solver --- .../captcha_solver/__init__.py | 44 +++++-------------- .../captcha_solver/exceptions.py | 6 +++ examples/captcha_solver/captcha_solver.py | 4 +- 3 files changed, 20 insertions(+), 34 deletions(-) create mode 100644 account_generator_helper/captcha_solver/exceptions.py diff --git a/account_generator_helper/captcha_solver/__init__.py b/account_generator_helper/captcha_solver/__init__.py index a4a7432..09ef778 100644 --- a/account_generator_helper/captcha_solver/__init__.py +++ b/account_generator_helper/captcha_solver/__init__.py @@ -1,43 +1,23 @@ import requests -import re +from account_generator_helper.captcha_solver.exceptions import CantRecognize, DailyLimit -class CaptchaSolver: - def __init__(self, proxy=None): - self._s = requests.Session() - if proxy: - self._s.proxies.update({'http': proxy, 'https': proxy}) - self.__event_validation = None - self.__view_state = None - self.__save_data(self._s.get('https://cloudmersive.com/ocr-api').text) - - def __save_data(self, text): - self.__event_validation = re.findall(r'__EVENTVALIDATION".*value="(.*)"', text)[0] - self.__view_state = re.findall(r'__VIEWSTATE".*value="(.*)"', text)[0] - @staticmethod - def __get_captcha_result(text): - result = re.findall(f'TextResult":.*"(.*)"', text) - if result: - return result[0].replace('\\n', '') - return '' +class CaptchaSolver: + def __init__(self, api_key): + self.__api_key = api_key def solve(self, image: open) -> str: """ Method for solving regular text captcha. :param image: open(file, 'rb') - """ - data = { - '__EVENTVALIDATION': self.__event_validation, - '__VIEWSTATE': self.__view_state, - 'ctl00$MainContent$LanguageSelector': 'eng', - 'ctl00$MainContent$btnUpload2': 'Photos of Docs to Text' - } - r = self._s.post('https://cloudmersive.com/ocr-api', data=data, files={'ctl00$MainContent$FileUploadBox': image}) - print(r.text) - if not r.ok: - return '' - self.__save_data(r.text) - return self.__get_captcha_result(r.text) + :result: Captcha text + """ + r = requests.post('https://api.optiic.dev/process', params={'apiKey': self.__api_key}, files={'image': image}) + if r.status_code == 429: + raise DailyLimit() + if not r.ok or not r.json().get('text'): + raise CantRecognize() + return r.json()['text'] diff --git a/account_generator_helper/captcha_solver/exceptions.py b/account_generator_helper/captcha_solver/exceptions.py new file mode 100644 index 0000000..868cef1 --- /dev/null +++ b/account_generator_helper/captcha_solver/exceptions.py @@ -0,0 +1,6 @@ +class CantRecognize(Exception): + """Could not recognize your captcha""" + + +class DailyLimit(Exception): + """Daily limit""" diff --git a/examples/captcha_solver/captcha_solver.py b/examples/captcha_solver/captcha_solver.py index c30ef59..f8ceab8 100644 --- a/examples/captcha_solver/captcha_solver.py +++ b/examples/captcha_solver/captcha_solver.py @@ -1,7 +1,7 @@ from account_generator_helper import CaptchaSolver - -captcha_solver = CaptchaSolver() +# Get api key from https://optiic.dev/ +captcha_solver = CaptchaSolver('11r6wjas2zTHLTgdWvEjaap1xq7m7111ufUNFas1fwCS') print('Captcha 1 result :', captcha_solver.solve(open('images/captcha_1.png', 'rb'))) # 97823C