ZennoLab

Automate everything

User Tools

Site Tools


Sidebar

Translations of this page:

en:addons:capmonster:funcaptcha

FunCaptcha Audio

CapMonster 2 software starting from 2.5.0.0 version allows to recognize audio FunCaptcha. We have developed special module ZennoLab.AudioReCaptcha for it. When you get FunCaptcha on a webpage, you should select audio recognition and send it to CapMonster.

Using audio FunCaptcha in ZennoPoster

To send audio FunCaptcha from ZennoPoster (v 5.9.5.1 and higher) you should use the following snippet. Here is the code:

// Main settings
 
// waiting timeout
var waitTime = 1000;
// attempts to load captcha
var tryLoadElement = 60;
// show recognizing progress messages
var needShowMessages = false;
 
// Additional variables
 
// tab
var tab = instance.ActiveTab;
// congratulations, you are not robot
var success = false;
// audio file
var file = string.Empty;
// file has been deleted
var fileDeleted = true;
// answer to audio captcha
var answer = string.Empty;
// timeout exceeded
var timeout = false;
// error loading file
var fileLoadingError = false;
var usedURLs = new List<string>();
 
Action OpenAudioForm = () => {
	project.SendInfoToLog(" Opening the form with audio task", needShowMessages);
	for (int k = 0; k < tryLoadElement; k++)
	{
		var audioButton = tab.FindElementByAttribute("span", "class", "fc_meta_audio_btn", "regexp", 0);
		// if found
		if (!audioButton.IsVoid)
		{
			// click button
			audioButton.Click();
			break;
		}
		System.Threading.Thread.Sleep(waitTime); // pause
		if (k == (tryLoadElement - 1)) timeout = true;
	}
};
 
// Downloading mp3-file
Action GetAudioFile= () => {
 
	var href = String.Empty;
 
	// getting audio task
	for (int k = 0; k < tryLoadElement; k++)
	{
		var audioChallenge = tab.FindElementByAttribute("a", "id", "recaptcha_audio_download", "text", 0);
		// if found, getting link to audio file
		if (!audioChallenge.IsVoid)
		{
			href = audioChallenge.GetAttribute("href");
			break;
		}
		else System.Threading.Thread.Sleep(waitTime);
		if (k == (tryLoadElement - 1)) timeout = true;
	}
 
	foreach (var usedUrl in usedURLs)
	{
		if (usedUrl.Contains(href))
			throw new Exception("new audio file is absent");
	}
	usedURLs.Add(href);
 
	project.SendInfoToLog("Downloading audio file", needShowMessages);
	try
	{
		var proxy = instance.GetProxy();
		var respType = ZennoLab.InterfacesLibrary.Enums.Http.ResponceType.File;
		var timeoutRequest = 30000;
		var cookies = instance.GetCookie("google.com", true);
		var userAgent = project.Profile.UserAgent;
		var maxRedirectCount = 1;
		var downloadPath = project.Directory + "\\audiofiles";
		file = ZennoPoster.HttpGet(href, proxy, "UTF8", respType, timeoutRequest, cookies, userAgent, false, maxRedirectCount, null, downloadPath);
		fileDeleted = false;
	}
	catch (Exception ex)
	{
		throw new Exception("Failed to download audio file");
	}
};
 
Action Recognize= () => {
	project.SendInfoToLog("Recognizing", needShowMessages);
	try 
	{
		if (!File.Exists(file)) 
		{
			fileLoadingError = true;
			fileDeleted = true;
			return;
		}
		var bytes = File.ReadAllBytes(file);
		if (bytes.Length < 1024)
		{
			fileLoadingError = true;
			return;
		}
		string str = Convert.ToBase64String(bytes);
		var rc = ZennoPoster.CaptchaRecognition("CapMonster2.dll", str, "CapMonsterModule=ZennoLab.AudioReCaptcha&ParallelMode=true");
		answer = rc.Split(new [] {"-|-"}, StringSplitOptions.RemoveEmptyEntries)[0];
	} 
	finally 
	{
		if (File.Exists(file)) 
		{
			File.Delete(file);
			fileDeleted = true;
		}
	}
};
 
Action InputAnswer= () => {
	if (!String.IsNullOrEmpty(answer) && answer != "sorry")
	{
		project.SendInfoToLog("Inserting answer", needShowMessages);
		HtmlElement audioAnswerInput = tab.FindElementByAttribute("input:text", "id", "recaptcha_response_field", "text", 0);
		// if found
		if (!audioAnswerInput.IsVoid)
		{
			// inserting answer
			audioAnswerInput.SetValue(answer, "None", false);
		}
		// pause
		System.Threading.Thread.Sleep(waitTime);
	}
	else throw new Exception("Answer not received");
};
 
// Verifying
Action SubmitAnswer= () => {
	// searching "Verify" button
	HtmlElement apply = tab.FindElementById("audio_submit");
	if (!apply.IsVoid) apply.Click();
	// pause
	System.Threading.Thread.Sleep(waitTime);
};
 
Action CheckTimeOut= () => {
	if (timeout) throw new Exception("Timeout of loading element has been exceeded");
};
 
// Recognizing captcha
OpenAudioForm();
CheckTimeOut();
GetAudioFile();
CheckTimeOut();
Recognize();
if (fileLoadingError) throw new Exception("Error loading file");
CheckTimeOut();
InputAnswer();
CheckTimeOut();
if (!fileDeleted)
{
	if (File.Exists(file))
	{
		File.Delete(file);
		fileDeleted = true;
	}
}
SubmitAnswer();
return "ok";
en/addons/capmonster/funcaptcha.txt · Last modified: 2015/11/23 15:14 by afameless