using Newtonsoft.Json;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.Net;
using System.Threading;
namespace RuriLib.CaptchaServices
{
///
/// Implements the API of https://captchas.io/
///
public class CaptchasIO : CaptchaService
{
///
/// Creates a CaptchasIO client.
///
/// The CaptchasIO API key
/// The maximum time to wait for the challenge to be solved
public CaptchasIO(string apiKey, int timeout) : base(apiKey, timeout) { }
///
public override double GetBalance()
{
using (CWebClient client = new CWebClient())
{
if (Timeout > 0) client.Timeout = Timeout;
var response = client.DownloadString($"http://api.captchas.io/res.php?key={ApiKey}&action=getbalance&json=1");
GenericResponse gbr = JsonConvert.DeserializeObject(response);
if (gbr.status == 0) throw new Exception(gbr.request);
return double.Parse(gbr.request, CultureInfo.InvariantCulture);
}
}
///
public override string SolveRecaptcha(string siteKey, string siteUrl)
{
// Create task
using (CWebClient client = new CWebClient())
{
if (Timeout > 0) client.Timeout = Timeout;
var response = client.DownloadString($"http://api.captchas.io/in.php?key={ApiKey}&method=userrecaptcha&googlekey={siteKey}&pageurl={siteUrl}&json=1");
GenericResponse ctr = JsonConvert.DeserializeObject(response);
if (ctr.status == 0) throw new Exception(ctr.request);
TaskId = ctr.request;
Status = CaptchaStatus.Processing;
// Check if task has been completed
DateTime start = DateTime.Now;
while (Status == CaptchaStatus.Processing && (DateTime.Now - start).TotalSeconds < Timeout)
{
Thread.Sleep(5000);
response = client.DownloadString($"http://api.captchas.io/res.php?key={ApiKey}&action=get&id={TaskId}&json=1");
GenericResponse gtrr = JsonConvert.DeserializeObject(response);
if (gtrr.request.Contains("NOT_READY")) continue;
if (gtrr.status == 0) throw new Exception(gtrr.request);
Status = CaptchaStatus.Completed;
return gtrr.request;
}
throw new TimeoutException();
}
}
///
public override string SolveCaptcha(Bitmap bitmap)
{
// Create task
using (CWebClient client = new CWebClient())
{
if (Timeout > 0) client.Timeout = Timeout;
client.Headers.Add(HttpRequestHeader.ContentType, "application/x-www-form-urlencoded");
var response = client.UploadString("http://api.captchas.io.com/in.php",
$"key={ApiKey}&method=base64®sense=1&body={GetBase64(bitmap, ImageFormat.Bmp)}&json=1");
GenericResponse ctr = JsonConvert.DeserializeObject(response);
if (ctr.status == 0) throw new Exception(ctr.request);
TaskId = ctr.request;
Status = CaptchaStatus.Processing;
// Check if task has been completed
DateTime start = DateTime.Now;
while (Status == CaptchaStatus.Processing && (DateTime.Now - start).TotalSeconds < Timeout)
{
Thread.Sleep(5000);
response = client.DownloadString($"http://api.captchas.io/res.php?key={ApiKey}&action=get&id={TaskId}&json=1");
GenericResponse gtrr = JsonConvert.DeserializeObject(response);
if (gtrr.request.Contains("NOT_READY")) continue;
if (gtrr.status == 0) throw new Exception(gtrr.request);
Status = CaptchaStatus.Completed;
return gtrr.request;
}
throw new TimeoutException();
}
}
#pragma warning disable 0414
#pragma warning disable 0649
struct GenericResponse
{
public int? status;
public string request;
}
#pragma warning restore 0414
#pragma warning restore 0649
}
}