Skip to content

Commit

Permalink
Merge pull request #5 from mrAdm/binary_captcha
Browse files Browse the repository at this point in the history
Added support for sending binary CAPTCHA files
  • Loading branch information
gotlium committed Apr 7, 2014
2 parents 54fad38 + cdf0c03 commit 253a36a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 12 deletions.
32 changes: 21 additions & 11 deletions antigate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from xmltodict import parse
from sys import exc_info
from time import sleep
import base64

from grab import Grab, UploadFile

Expand All @@ -24,9 +25,9 @@ class AntiGateError(Exception):


class AntiGate(object):
def __init__(self, key, filename='', auto_run=True,
def __init__(self, key, captcha_file='', auto_run=True,
grab_config=None, send_config=None,
domain='antigate.com'):
domain='antigate.com', binary=False):
self.g = Grab()
if grab_config:
self.g.setup(**grab_config)
Expand All @@ -37,8 +38,8 @@ def __init__(self, key, filename='', auto_run=True,
self.domain = domain
self.logger = getLogger(__name__)

if auto_run and filename:
self.run(filename)
if auto_run and captcha_file:
self.run(captcha_file, binary)

def _get_domain(self, path):
if DEBUG:
Expand Down Expand Up @@ -87,17 +88,26 @@ def _go(self, url, err):
self.g.response.code, err, self.g.response.body
))

def _send(self, filename):
self.g.setup(multipart_post=self._update_params(
{'key': self.key, 'file': UploadFile(filename)}, self.send_config))
def _send(self, captcha_file, binary=False):
if binary:
body = base64.b64encode(captcha_file)
self.g.setup(post=self._update_params(
{'method': 'base64', 'key': self.key, 'body': body},
self.send_config
))
else:
self.g.setup(multipart_post=self._update_params(
{'key': self.key, 'file': UploadFile(captcha_file)},
self.send_config
))
self._go(self._get_input_url(), 'Can not send captcha')
return self._body('captcha_id')

def send(self, filename):
def send(self, captcha_file, binary=False):
self.logger.debug('Sending captcha')
while True:
try:
return self._send(filename)
return self._send(captcha_file, binary)
except AntiGateError:
msg = exc_info()[1]
self.logger.debug(msg)
Expand Down Expand Up @@ -152,8 +162,8 @@ def load(self):
self._go(self._get_domain('load.php'), 'Can not get loads')
return self._response_to_dict()

def run(self, filename):
self.send(filename)
def run(self, captcha_file, binary=False):
self.send(captcha_file, binary)
self.get()

def __str__(self):
Expand Down
27 changes: 26 additions & 1 deletion test_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from antigate import AntiGate

API_KEY = "5407a05a14762a7413723d478cb10096"
API_KEY = "2609aa867da257483fc81ce6024ebb46"
IMAGE1 = "captcha/123.jpg"
IMAGE2 = "captcha/456.jpg"

Expand All @@ -21,6 +21,9 @@ def test_load(self):
def test_base(self):
self.assertEqual(str(AntiGate(API_KEY, IMAGE1)), '123')

def test_base_binary(self):
self.assertEqual(str(AntiGate(API_KEY, open(IMAGE1, 'rb').read(), binary=True)), '123')

def test_abuse(self):
gate = AntiGate(API_KEY, IMAGE1)
if str(gate) != 'qwerty':
Expand All @@ -35,6 +38,15 @@ def test_manual(self):
captcha_value = gate.get(captcha_id)
self.assertEqual(str(captcha_value), '123')

def test_manual_binary(self):
gate = AntiGate(API_KEY, auto_run=False)

captcha_id = gate.send(open(IMAGE1, 'rb').read(), binary=True)
self.assertTrue(str(captcha_id).isdigit())

captcha_value = gate.get(captcha_id)
self.assertEqual(str(captcha_value), '123')

def test_multiple(self):
gate = AntiGate(API_KEY, auto_run=False)
captcha_id1 = gate.send(IMAGE1)
Expand All @@ -48,6 +60,19 @@ def test_multiple(self):
#self.assertListEqual(results, ['123', '456'])
self.assertTrue(results == ['123', '456'])

def test_multiple_binary(self):
gate = AntiGate(API_KEY, auto_run=False)
captcha_id1 = gate.send(open(IMAGE1, 'rb').read(), binary=True)
captcha_id2 = gate.send(open(IMAGE2, 'rb').read(), binary=True)

self.assertTrue(str(captcha_id1).isdigit())
self.assertTrue(str(captcha_id2).isdigit())

results = gate.get_multi([captcha_id1, captcha_id2])

#self.assertListEqual(results, ['123', '456'])
self.assertTrue(results == ['123', '456'])


if __name__ == '__main__':
unittest.main()

0 comments on commit 253a36a

Please sign in to comment.