-
Notifications
You must be signed in to change notification settings - Fork 1
/
captcha.py
executable file
·146 lines (116 loc) · 3.72 KB
/
captcha.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python
#-*- coding: UTF-8 -*-
__author__ = "helljump"
import logging
import requests
import time
import mimetypes
log = logging.getLogger(__name__)
class AntigateError(Exception):
pass
class BaseCaptcha(object):
TIMEOUT = 60
SLEEPTIME = 5
def get_balance(self):
return -1
def get_text(self, *args, **kwargs):
return ''
class Antigate(BaseCaptcha):
"""
>>> ag = Antigate('*****9404cccf08797ec7d76bbe5224f')
>>> float(ag.get_balance()) > -1
True
>>> egg = ag.get_text(open('/work/cm2/test/captcha.png', 'rb').read())
>>> print egg.lower()
nxkm
"""
SOFT_ID = '48'
URL = "http://antigate.com"
def __init__(self, key):
self.sess = requests.Session()
self.key = key
def check_error(self, rc):
if rc.text.startswith("ERROR_"):
raise AntigateError(rc.text)
def get_balance(self):
params = {
'key': self.key,
'action': 'getbalance'
}
rc = self.sess.get(self.URL + '/res.php', params=params)
self.check_error(rc)
return rc.text
def _send(self, data, fname, **kwargs):
t, e = mimetypes.guess_type(fname)
files = {
'file': (fname, data, t)
}
params = {
'key': self.key,
'soft_id': self.SOFT_ID,
'method': 'post'
}
params.update(kwargs)
rc = self.sess.post(self.URL + "/in.php", data=params, files=files)
#rc = self.sess.post(self.URL + "/post", data=params, files=files)
#import codecs
#codecs.open('dump.txt', 'w', 'utf8').write(rc.text)
self.check_error(rc)
try:
cap_id = rc.text.split("|")[1]
except IndexError:
log.exception(rc.text)
return cap_id
def _recv(self, cap_id):
params = {
'key': self.key,
'soft_id': self.SOFT_ID,
'action': 'get',
'id': cap_id
}
rc = self.sess.get(self.URL + "/res.php", params=params)
self.check_error(rc)
if rc.text == "CAPCHA_NOT_READY":
return None
log.debug('rc %s', rc.text)
text = rc.text.split("|")[-1]
return text
def get_text(self, data, fname="captcha.png", **kwargs):
cap_id = self._send(data, fname, **kwargs)
log.debug("cap_id %s", cap_id)
t = time.time()
while time.time() - t < self.TIMEOUT:
answer = self._recv(cap_id)
if answer:
log.debug("captcha_text %s", answer)
return answer
log.debug("catcha is not ready, sleep %i seconds", self.SLEEPTIME)
time.sleep(self.SLEEPTIME)
else:
raise AntigateError("ERROR_ANTIGATE_TIMEOUT")
class CaptchaBot(Antigate):
""" captchabot with antigate interface
>>> ag = CaptchaBot('*****504750060896ad6f13b5d3e7a9e')
>>> float(ag.get_balance()) > -1
True
>>> egg = ag.get_text(open('/work/cm2/test/captcha.png', 'rb').read())
>>> print egg.lower()
nxkm
"""
SOFT_ID = '174015'
URL = 'http://captchabot.com'
class RipCaptcha(Antigate):
""" ripcaptcha with antigate interface
>>> ag = RipCaptcha('*****1b3be91baab890f9154b227c569')
>>> float(ag.get_balance()) > -1
True
>>> egg = ag.get_text(open('/work/cm2/test/captcha.png', 'rb').read())
>>> print egg.lower()
nxkm
"""
SOFT_ID = '1315'
URL = 'http://ripcaptcha.com'
if __name__ == '__main__':
logging.basicConfig(level=logging.DEBUG)
import doctest
doctest.testmod()