Created
April 24, 2012 11:11
-
-
Save bramd/2478827 to your computer and use it in GitHub Desktop.
Solve CAPTCHAs using the Webvisum API, requires a valid Webvisum username and password.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import urllib2 | |
import requests | |
from xml.dom import minidom | |
from time import sleep | |
API_URL = 'http://api.webvisum.com/api_rest' | |
USERNAME = '' | |
PASSWORD = '' | |
def login(username, password): | |
response = requests.get(API_URL, | |
params={'api_version': '1.0', | |
'app_version': '0.7.7', | |
'user_name': username, | |
'password': password, | |
'method': 'auth.login'}) | |
doc = minidom.parseString(response.content) | |
return doc.getElementsByTagName('session_key')[0].firstChild.nodeValue | |
def solve_captcha(session_id, captcha_image): | |
data = {'session_key': session_id, | |
'page_url': 'http://foobar.com/', | |
'method': 'captcha.submit', | |
} | |
files = {'file': captcha_image} | |
response = requests.post(API_URL, data=data, | |
files=files) | |
doc = minidom.parseString(response.content) | |
return doc.getElementsByTagName('hash')[0].firstChild.nodeValue | |
def get_captcha_result(session_id, captcha_hash): | |
data = {'session_key': session_id, 'hash': captcha_hash, 'method': | |
'captcha.result'} | |
response = requests.get(API_URL, params=data) | |
doc = minidom.parseString(response.content) | |
eta = doc.getElementsByTagName('eta')[0].firstChild.nodeValue | |
text = doc.getElementsByTagName('text')[0].firstChild | |
if text: | |
text = text.nodeValue | |
return eta, text | |
def main(): | |
session_id = login(USERNAME, PASSWORD) | |
print 'Logged in, session ID:', session_id | |
captcha_image = urllib2.urlopen(sys.argv[1]) | |
captcha_hash = solve_captcha(session_id, captcha_image) | |
result = '' | |
while not result: | |
sleep(1) | |
eta, result = get_captcha_result(session_id, captcha_hash) | |
if result: | |
print 'Captcha result:', result | |
else: | |
print 'ETA:', eta | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment