From cdf0c03fdd6fc333b80174c05dd77263dbc82be2 Mon Sep 17 00:00:00 2001 From: int Date: Sun, 6 Apr 2014 11:57:30 +0400 Subject: [PATCH] Added support for sending binary CAPTCHA files --- antigate/__init__.py | 32 +++++++++++++++++++++----------- test_cases.py | 27 ++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 12 deletions(-) diff --git a/antigate/__init__.py b/antigate/__init__.py index 86e05a9..27d5deb 100644 --- a/antigate/__init__.py +++ b/antigate/__init__.py @@ -10,6 +10,7 @@ from xmltodict import parse from sys import exc_info from time import sleep +import base64 from grab import Grab, UploadFile @@ -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) @@ -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: @@ -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) @@ -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): diff --git a/test_cases.py b/test_cases.py index 4ae82ee..7b66b7c 100644 --- a/test_cases.py +++ b/test_cases.py @@ -3,7 +3,7 @@ from antigate import AntiGate -API_KEY = "5407a05a14762a7413723d478cb10096" +API_KEY = "2609aa867da257483fc81ce6024ebb46" IMAGE1 = "captcha/123.jpg" IMAGE2 = "captcha/456.jpg" @@ -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': @@ -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) @@ -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()