bazarr/libs/deathbycaptcha.py

404 lines
13 KiB
Python
Raw Normal View History

2019-04-06 12:26:42 +00:00
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import base64
import binascii
import errno
import imghdr
import random
import os
import select
import socket
import sys
import threading
import time
2019-09-13 19:12:26 +00:00
2019-04-06 12:26:42 +00:00
try:
from json import read as json_decode, write as json_encode
except ImportError:
try:
from json import loads as json_decode, dumps as json_encode
except ImportError:
from simplejson import loads as json_decode, dumps as json_encode
2019-09-13 19:12:26 +00:00
try:
from urllib2 import build_opener, HTTPRedirectHandler, Request, HTTPError
from urllib import urlencode, urlopen
except ImportError:
from urllib.request import build_opener, HTTPRedirectHandler, Request, urlopen
from urllib.error import HTTPError
from urllib.parse import urlencode
2019-04-06 12:26:42 +00:00
# API version and unique software ID
2019-09-13 19:12:26 +00:00
API_VERSION = 'DBC/Python v4.0.11'
SOFTWARE_VENDOR_ID = 0
2019-04-06 12:26:42 +00:00
# Default CAPTCHA timeout and decode() polling interval
DEFAULT_TIMEOUT = 60
2019-09-13 19:12:26 +00:00
POLLS_INTERVAL = 5
2019-04-06 12:26:42 +00:00
# Base HTTP API url
2019-09-13 19:12:26 +00:00
HTTP_BASE_URL = 'http://api.deathbycaptcha.com/api'
2019-04-06 12:26:42 +00:00
# Preferred HTTP API server's response content type, do not change
HTTP_RESPONSE_TYPE = 'application/json'
# Socket API server's host & ports range
2019-09-13 19:12:26 +00:00
SOCKET_HOST = 'api.deathbycaptcha.com'
2019-04-06 12:26:42 +00:00
SOCKET_PORTS = range(8123, 8131)
class AccessDeniedException(Exception):
pass
class Client(object):
2019-09-13 19:12:26 +00:00
"""Death by Captcha API Client"""
2019-04-06 12:26:42 +00:00
def __init__(self, username, password):
self.is_verbose = False
2019-09-13 19:12:26 +00:00
self.userpwd = {'username': username,
'password': password}
def _load_file(self, captcha):
if hasattr(captcha, 'read'):
raw_captcha = captcha.read()
elif isinstance(captcha, bytearray):
raw_captcha = captcha
elif os.path.isfile(captcha):
raw_captcha = ''
try:
f = open(captcha, 'rb')
except Exception as e:
raise e
else:
raw_captcha = f.read()
f.close()
else:
f_stream = urlopen(captcha)
raw_captcha = f_stream.read()
if not len(raw_captcha):
raise ValueError('CAPTCHA image is empty')
elif imghdr.what(None, raw_captcha) is None:
raise TypeError('Unknown CAPTCHA image type')
else:
return raw_captcha
2019-04-06 12:26:42 +00:00
def _log(self, cmd, msg=''):
if self.is_verbose:
2019-09-13 19:12:26 +00:00
print('%d %s %s' % (time.time(), cmd, msg.rstrip()))
2019-04-06 12:26:42 +00:00
return self
def close(self):
pass
def connect(self):
pass
def get_user(self):
2019-09-13 19:12:26 +00:00
"""Fetch the user's details dict -- balance, rate and banned status."""
raise NotImplemented()
2019-04-06 12:26:42 +00:00
def get_balance(self):
2019-09-13 19:12:26 +00:00
"""Fetch the user's balance (in US cents)."""
2019-04-06 12:26:42 +00:00
return self.get_user().get('balance')
def get_captcha(self, cid):
2019-09-13 19:12:26 +00:00
"""Fetch a CAPTCHA details dict -- its ID, text and correctness."""
raise NotImplemented()
2019-04-06 12:26:42 +00:00
def get_text(self, cid):
"""Fetch a CAPTCHA text."""
return self.get_captcha(cid).get('text') or None
def report(self, cid):
"""Report a CAPTCHA as incorrectly solved."""
2019-09-13 19:12:26 +00:00
raise NotImplemented()
def remove(self, cid):
"""Remove an unsolved CAPTCHA."""
raise NotImplemented()
2019-04-06 12:26:42 +00:00
def upload(self, captcha):
"""Upload a CAPTCHA.
Accepts file names and file-like objects. Returns CAPTCHA details
dict on success.
"""
2019-09-13 19:12:26 +00:00
raise NotImplemented()
2019-04-06 12:26:42 +00:00
2019-09-13 19:12:26 +00:00
def decode(self, captcha, timeout=DEFAULT_TIMEOUT):
"""Try to solve a CAPTCHA.
2019-04-06 12:26:42 +00:00
See Client.upload() for arguments details.
Uploads a CAPTCHA, polls for its status periodically with arbitrary
timeout (in seconds), returns CAPTCHA details if (correctly) solved.
2019-09-13 19:12:26 +00:00
"""
2019-04-06 12:26:42 +00:00
deadline = time.time() + (max(0, timeout) or DEFAULT_TIMEOUT)
2019-09-13 19:12:26 +00:00
c = self.upload(captcha)
if c:
while deadline > time.time() and not c.get('text'):
time.sleep(POLLS_INTERVAL)
c = self.get_captcha(c['captcha'])
if c.get('text') and c.get('is_correct'):
return c
2019-04-06 12:26:42 +00:00
class HttpClient(Client):
"""Death by Captcha HTTP API client."""
def __init__(self, *args):
Client.__init__(self, *args)
2019-09-13 19:12:26 +00:00
self.opener = build_opener(HTTPRedirectHandler())
2019-04-06 12:26:42 +00:00
def _call(self, cmd, payload=None, headers=None):
if headers is None:
headers = {}
headers['Accept'] = HTTP_RESPONSE_TYPE
headers['User-Agent'] = API_VERSION
if hasattr(payload, 'items'):
2019-09-13 19:12:26 +00:00
payload = urlencode(payload)
2019-04-06 12:26:42 +00:00
self._log('SEND', '%s %d %s' % (cmd, len(payload), payload))
if payload is not None:
headers['Content-Length'] = len(payload)
try:
2019-09-13 19:12:26 +00:00
response = self.opener.open(Request(
2019-04-06 12:26:42 +00:00
HTTP_BASE_URL + '/' + cmd.strip('/'),
data=payload,
headers=headers
)).read()
2019-09-13 19:12:26 +00:00
except HTTPError as e:
if 403 == e.code:
raise AccessDeniedException(
'Access denied, please check your credentials and/or balance')
elif 400 == e.code or 413 == e.code:
raise ValueError("CAPTCHA was rejected by the service, check if it's a valid image")
2019-04-06 12:26:42 +00:00
else:
self._log('RECV', '%d %s' % (len(response), response))
try:
return json_decode(response)
except Exception:
raise RuntimeError('Invalid API response')
return {}
def get_user(self):
return self._call('user', self.userpwd.copy()) or {'user': 0}
def get_captcha(self, cid):
return self._call('captcha/%d' % cid) or {'captcha': 0}
def report(self, cid):
return not self._call('captcha/%d/report' % cid,
self.userpwd.copy()).get('is_correct')
2019-09-13 19:12:26 +00:00
def remove(self, cid):
return not self._call('captcha/%d/remove' % cid,
self.userpwd.copy()).get('captcha')
2019-04-06 12:26:42 +00:00
2019-09-13 19:12:26 +00:00
def upload(self, captcha):
boundary = binascii.hexlify(os.urandom(16))
data = self.userpwd.copy()
data['swid'] = SOFTWARE_VENDOR_ID
body = '\r\n'.join(('\r\n'.join(('--%s' % boundary,
'Content-Disposition: form-data; name="%s"' % k,
'Content-Type: text/plain',
'Content-Length: %d' % len(str(v)),
'',
str(v))))
for k, v in data.items())
captcha = self._load_file(captcha)
body += '\r\n'.join(('',
'--%s' % boundary,
'Content-Disposition: form-data; name="captchafile"; filename="captcha"',
'Content-Type: application/octet-stream',
'Content-Length: %d' % len(captcha),
'',
captcha,
'--%s--' % boundary,
''))
2019-04-06 12:26:42 +00:00
response = self._call('captcha', body, {
'Content-Type': 'multipart/form-data; boundary="%s"' % boundary
}) or {}
if response.get('captcha'):
return response
class SocketClient(Client):
"""Death by Captcha socket API client."""
TERMINATOR = '\r\n'
def __init__(self, *args):
Client.__init__(self, *args)
self.socket_lock = threading.Lock()
self.socket = None
def close(self):
if self.socket:
self._log('CLOSE')
try:
self.socket.shutdown(socket.SHUT_RDWR)
except socket.error:
pass
finally:
self.socket.close()
self.socket = None
def connect(self):
if not self.socket:
self._log('CONN')
host = (socket.gethostbyname(SOCKET_HOST),
random.choice(SOCKET_PORTS))
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(0)
try:
self.socket.connect(host)
2019-09-13 19:12:26 +00:00
except socket.error as e:
if errno.EINPROGRESS == e[0]:
pass
else:
2019-04-06 12:26:42 +00:00
self.close()
2019-09-13 19:12:26 +00:00
raise e
2019-04-06 12:26:42 +00:00
return self.socket
def __del__(self):
self.close()
def _sendrecv(self, sock, buf):
self._log('SEND', buf)
fds = [sock]
buf += self.TERMINATOR
response = ''
while True:
2019-09-13 19:12:26 +00:00
rd, wr, ex = select.select((not buf and fds) or [],
(buf and fds) or [],
fds,
POLLS_INTERVAL)
if ex:
2019-04-06 12:26:42 +00:00
raise IOError('select() failed')
try:
2019-09-13 19:12:26 +00:00
if wr:
2019-04-06 12:26:42 +00:00
while buf:
2019-09-13 19:12:26 +00:00
buf = buf[wr[0].send(buf):]
elif rd:
2019-04-06 12:26:42 +00:00
while True:
2019-09-13 19:12:26 +00:00
s = rd[0].recv(256)
2019-04-06 12:26:42 +00:00
if not s:
raise IOError('recv(): connection lost')
else:
response += s
2019-09-13 19:12:26 +00:00
except socket.error as e:
if e[0] not in (errno.EAGAIN, errno.EINPROGRESS):
raise e
2019-04-06 12:26:42 +00:00
if response.endswith(self.TERMINATOR):
self._log('RECV', response)
return response.rstrip(self.TERMINATOR)
raise IOError('send/recv timed out')
def _call(self, cmd, data=None):
if data is None:
data = {}
data['cmd'] = cmd
data['version'] = API_VERSION
request = json_encode(data)
response = None
2019-09-13 19:12:26 +00:00
for i in range(2):
2019-04-06 12:26:42 +00:00
self.socket_lock.acquire()
try:
sock = self.connect()
response = self._sendrecv(sock, request)
2019-09-13 19:12:26 +00:00
except IOError as e:
sys.stderr.write(str(e) + "\n")
2019-04-06 12:26:42 +00:00
self.close()
2019-09-13 19:12:26 +00:00
except socket.error as e:
sys.stderr.write(str(e) + "\n")
2019-04-06 12:26:42 +00:00
self.close()
raise IOError('Connection refused')
else:
break
finally:
self.socket_lock.release()
try:
2019-09-13 19:12:26 +00:00
if response is None:
raise IOError('Connection lost timed out during API request')
try:
response = json_decode(response)
except Exception:
raise RuntimeError('Invalid API response')
if 'error' in response:
error = response['error']
if 'not-logged-in' == error:
raise AccessDeniedException('Access denied, check your credentials')
elif 'banned' == error:
raise AccessDeniedException('Access denied, account is suspended')
elif 'insufficient-funds' == error:
raise AccessDeniedException('CAPTCHA was rejected due to low balance')
elif 'invalid-captcha' == error:
raise ValueError('CAPTCHA is not a valid image')
elif 'service-overload' == error:
raise ValueError(
'CAPTCHA was rejected due to service overload, try again later')
else:
raise RuntimeError('API server error occured: %s' % error)
except Exception as e:
2019-04-06 12:26:42 +00:00
self.socket_lock.acquire()
self.close()
self.socket_lock.release()
2019-09-13 19:12:26 +00:00
raise e
else:
return response
2019-04-06 12:26:42 +00:00
def get_user(self):
2019-09-13 19:12:26 +00:00
return self._call('user', self.userpwd.copy()) or {'user': 0}
2019-04-06 12:26:42 +00:00
def get_captcha(self, cid):
return self._call('captcha', {'captcha': cid}) or {'captcha': 0}
2019-09-13 19:12:26 +00:00
def upload(self, captcha):
data = self.userpwd.copy()
data['captcha'] = base64.b64encode(self._load_file(captcha))
2019-04-06 12:26:42 +00:00
response = self._call('upload', data)
if response.get('captcha'):
2019-09-13 19:12:26 +00:00
return dict((k, response.get(k)) for k in ('captcha', 'text', 'is_correct'))
2019-04-06 12:26:42 +00:00
def report(self, cid):
2019-09-13 19:12:26 +00:00
data = self.userpwd.copy()
data['captcha'] = cid
return not self._call('report', data).get('is_correct')
2019-04-06 12:26:42 +00:00
2019-09-13 19:12:26 +00:00
def remove(self, cid):
data = self.userpwd.copy()
data['captcha'] = cid
return not self._call('remove', data).get('captcha')
2019-04-06 12:26:42 +00:00
if '__main__' == __name__:
2019-09-13 19:12:26 +00:00
import sys
2019-04-06 12:26:42 +00:00
# Put your DBC username & password here:
2019-09-13 19:12:26 +00:00
#client = HttpClient(sys.argv[1], sys.argv[2])
2019-04-06 12:26:42 +00:00
client = SocketClient(sys.argv[1], sys.argv[2])
client.is_verbose = True
2019-09-13 19:12:26 +00:00
print('Your balance is %s US cents' % client.get_balance())
2019-04-06 12:26:42 +00:00
for fn in sys.argv[3:]:
try:
# Put your CAPTCHA image file name or file-like object, and optional
# solving timeout (in seconds) here:
captcha = client.decode(fn, DEFAULT_TIMEOUT)
2019-09-13 19:12:26 +00:00
except Exception as e:
2019-04-06 12:26:42 +00:00
sys.stderr.write('Failed uploading CAPTCHA: %s\n' % (e, ))
captcha = None
if captcha:
2019-09-13 19:12:26 +00:00
print('CAPTCHA %d solved: %s' % (captcha['captcha'], captcha['text']))
2019-04-06 12:26:42 +00:00
# Report as incorrectly solved if needed. Make sure the CAPTCHA was
# in fact incorrectly solved!
2019-09-13 19:12:26 +00:00
try:
client.report(captcha['captcha'])
except Exception as e:
sys.stderr.write('Failed reporting CAPTCHA: %s\n' % (e, ))