-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Denis Pitkov
committed
Feb 26, 2023
1 parent
ee9c768
commit 560ee44
Showing
3 changed files
with
20 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class CantRecognize(Exception): | ||
"""Could not recognize your captcha""" | ||
|
||
|
||
class DailyLimit(Exception): | ||
"""Daily limit""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters