Skip to content

Commit

Permalink
py3 compatibility fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
gotlium committed Jul 3, 2015
1 parent a85f98b commit d7d1af5
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 28 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: python
python:
- "2.6"
- "2.7"
- "3.3"
- "3.4"
Expand Down
13 changes: 10 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ Real-time captcha-to-text decodings
:target: https://travis-ci.org/gotlium/antigate
.. image:: https://coveralls.io/repos/gotlium/antigate/badge.png?branch=master
:target: https://coveralls.io/r/gotlium/antigate?branch=master
.. image:: https://pypip.in/v/antigate/badge.png
.. image:: https://img.shields.io/badge/python-2.6,2.7,3.3,3.4-blue.svg
:alt: Python 2.6, 2.7, 3.3, 3.4
:target: https://pypi.python.org/pypi/antigate/
.. image:: https://img.shields.io/pypi/v/antigate.svg
:alt: Current version on PyPi
:target: https://crate.io/packages/antigate/
.. image:: https://pypip.in/d/antigate/badge.png
.. image:: https://img.shields.io/pypi/dm/antigate.svg
:alt: Downloads from PyPi
:target: https://crate.io/packages/antigate/
.. image:: https://img.shields.io/badge/license-GPLv2-green.svg
:target: https://pypi.python.org/pypi/antigate/
:alt: License



Documentation available at `Read the Docs <http://antigate.readthedocs.org/>`_.
Expand Down Expand Up @@ -118,7 +125,7 @@ Get results for multiple ids:
Compatibility:
-------------
* Python: 2.6, 2.7, 3.3
* Python: 2.6, 2.7, 3.3, 3.4


.. image:: https://d2weczhvl823v0.cloudfront.net/gotlium/antigate/trend.png
Expand Down
9 changes: 5 additions & 4 deletions antigate/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ class AntiGate(object):
def __init__(self, key, captcha_file='', auto_run=True,
grab_config=None, send_config=None,
domain='antigate.com', binary=False):
self.g = Grab()
self.g.setup(timeout=30)
if grab_config:
self.g.setup(**grab_config)
self.g = Grab(**(grab_config or {}))
self.key = key
self.captcha_id = None
self.captcha_key = None
Expand Down Expand Up @@ -97,6 +94,8 @@ def _go(self, url, err):
def _send(self, captcha_file, binary=False):
if binary:
body = base64.b64encode(captcha_file)
if six.PY3:
body = body.decode('utf-8')
self.g.setup(post=self._update_params(
{'method': 'base64', 'key': self.key, 'body': body},
self.send_config
Expand Down Expand Up @@ -144,6 +143,8 @@ def get(self, captcha_id=None):
def _get_multi(self, ids):
self._go(self._get_build_url(data={
'ids': ','.join(map(str, ids))}), 'Can not get result')
if six.PY3:
return self.g.response.body.decode('utf-8').split('|')
return self.g.response.body.split('|')

def get_multi(self, ids):
Expand Down
8 changes: 4 additions & 4 deletions requirements/package.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
grab==0.4.13
lxml==3.2.4
pycurl==7.19.0.2
xmltodict==0.8.3
grab==0.6.21
lxml==3.4.4
pycurl==7.19.5.1
xmltodict==0.9.2
six==1.9.0
2 changes: 1 addition & 1 deletion requirements/tests.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
flake8==2.1.0
flake8==2.4.1
coverage==3.7
coveralls==0.3
18 changes: 12 additions & 6 deletions test_cases.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
import os

from antigate import AntiGate

Expand All @@ -22,8 +21,10 @@ def test_base(self):
self.assertEqual(str(AntiGate(API_KEY, IMAGE1)), '123')

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

def test_abuse(self):
gate = AntiGate(API_KEY, IMAGE1)
Expand All @@ -41,9 +42,10 @@ def test_manual(self):

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

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

captcha_value = gate.get(captcha_id)
self.assertEqual(str(captcha_value), '123')
Expand All @@ -61,8 +63,12 @@ def test_multiple(self):

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)
fp1 = open(IMAGE1, 'rb')
fp2 = open(IMAGE2, 'rb')
captcha_id1 = gate.send(fp1.read(), binary=True)
captcha_id2 = gate.send(fp2.read(), binary=True)
fp1.close()
fp2.close()

self.assertTrue(str(captcha_id1).isdigit())
self.assertTrue(str(captcha_id2).isdigit())
Expand Down
18 changes: 9 additions & 9 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
[tox]
envlist =
py33,
pep8,
py27,
py34,
coverage,

[testenv]
usedevelop = True
Expand All @@ -13,25 +16,22 @@ commands =

[testenv:pep8]
deps =
flake8==2.1.0
flake8==2.4.1
commands =
flake8 antigate

[testenv:coverage]
deps =
coverage==3.7
coverage==3.7.1
commands =
coverage run --branch --source=antigate test_cases.py
coverage report --omit="antigate/test*"

[testenv:pypy]
basepython = pypy

[testenv:py26]
basepython = python2.6

[testenv:py27]
basepython = python2.7

[testenv:py33]
basepython = python3.3

[testenv:py34]
basepython = python3.4

0 comments on commit d7d1af5

Please sign in to comment.