import tempfile import webbrowser import time import os from six moves

 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
import tempfile
import webbrowser
import time
import os
from six.moves import input
from captcha_solver.captcha_backend.base import CaptchaBackend
import sys
import locale
class BrowserBackend(CaptchaBackend):
def get_submit_captcha_request_data(self, data):
fd, path = tempfile.mkstemp()
with open(path, 'wb') as out:
out.write(data)
url = 'file://' + path
return {'url': url, 'post_data': None}
def parse_submit_captcha_response(self, res):
return res['url'].replace('file://', '')
def get_check_solution_request_data(self, captcha_id):
url = 'file://' + captcha_id
return {'url': url, 'post_data': None}
def parse_check_solution_response(self, res):
webbrowser.open(url=res['url'])
# Wait some time, skip some debug messages
# which browser could dump to console
time.sleep(0.5)
solution = input('Enter solution: ')
path = res['url'].replace('file://', '')
os.unlink(path)
return solution.decode(sys.stdin.encoding or locale.getpreferredencoding(True))