
he ruCaptcha.com service solves, as already mentioned, a wide variety of captchas , from reCaptcha of all types and versions to keyCaptcha , hCaptcha and FunCaptcha ; As a basis for the experiment, we will take the most probably currently popular solution on the web - reCaptcha v.2 , and this is the official demo from Google:
URL_RECAPTCHA = 'https://www.google.com/recaptcha/api2/demo'
By the way, instead of google.com, we could easily substitute the address of a page, for example, my blog, written on the basis of the Ruby on Rails framework , where reCaptcha works through the ambethia / recaptcha jam ; everything will turn out the same. I will not say that it is always like this: for example, reCaptcha on the pages of the blog that you are currently leafing through is called by the scripts of K2, a Joomla component , with which everything is probably not so unambiguous. But more often than not, the described scenario will work, which we will now check.
We read the ruCaptcha.com API documentation : so, first of all, we need the data-sitekey value on the captcha page. Ok, let's parse HTML and quickly find what we need:
Code:
def data_sitekey
# Parsing url and getting a data-sitekey of recaptcha
url = URL_RECAPTCHA
html = open(url)
doc = Nokogiri::HTML(html)
doc.xpath('//@data-sitekey')
end
Now, having received the data-sitekey reCaptcha , we can already form the first request to the ruCaptcha.com API , as a response to which we will receive the identifier of the task set for the RuCaptcha.com service ...
Code:
def first_request
target = 'https://rucaptcha.com/in.php'
params = {
key: APIKEY,
method: 'userrecaptcha',
googlekey: data_sitekey,
pageurl: URL_RECAPTCHA
}
request(target, params)
end
Something like that:
Code:
def request(target, params)
uri = URI.parse(target)
uri.query = URI.encode_www_form(params)
uri.open.read
end
Code:
target = 'https://rucaptcha.com/res.php'
params = {
key: APIKEY,
action: 'get',
id: answer.gsub('OK|', '')
}
1.times do
begin
sleep 10
request = request(target, params)
raise unless request.include? 'OK'
rescue StandardError
retry
end
end
Documentation https://github.com/cmirnow/rucaptcha-client