Skip to content

Commit

Permalink
Update captcha solver
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Pitkov committed Feb 26, 2023
1 parent ee9c768 commit 560ee44
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 34 deletions.
44 changes: 12 additions & 32 deletions account_generator_helper/captcha_solver/__init__.py
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']
6 changes: 6 additions & 0 deletions account_generator_helper/captcha_solver/exceptions.py
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"""
4 changes: 2 additions & 2 deletions examples/captcha_solver/captcha_solver.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit 560ee44

Please sign in to comment.