Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Linq;
- using System.Threading.Tasks;
- using RestSharp;
- namespace TwoCaptchaSharp
- {
- enum Language { CaptchaDefault = 0, Russian = 1, English = 2 }
- public class TwoCaptchaSharp
- {
- public string Key { get; set; }
- public string RecaptchaTextInstructions { get; set; }
- public int Language { get; set; }
- public int GetIdCaptcha;
- public TwoCaptchaSharpImage Image;
- public Task<string> ExecuteAsync()
- {
- var t = new Task<string>(() =>
- {
- return this.Execute();
- });
- return t;
- }
- public string Execute()
- {
- string result = string.Empty;
- if (String.IsNullOrEmpty(Key))
- throw new Exception("Key provided is empty of null");
- if (string.IsNullOrEmpty(RecaptchaTextInstructions))
- throw new Exception("Instructions provided is empty of null");
- if (!(new int[] { 0, 1, 2 }).ToList<int>().Contains(Language))
- throw new Exception("Unrecognizable language number. 0 is Default, Russian is 1 and English is 2");
- if (!(new int[] { 0, 1 }).ToList<int>().Contains(GetIdCaptcha))
- throw new Exception("GetIdCaptcha value is not correct.");
- if (Image == null)
- throw new System.NullReferenceException();
- var TwoCaptcha = new RestClient("http://rucaptcha.com");
- var Request = new RestRequest("/in.php", Method.POST);
- if (Image.IsPathDefined)
- Request.AddFile("file", Image.Path, null);
- else
- Request.AddFile("file", Image.ByteArr, null, null);
- Request.AddParameter("language", Language);
- Request.AddParameter("2Captcha", 1);
- Request.AddParameter("textinstructions", string.Format("%{0}%", RecaptchaTextInstructions));
- Request.AddParameter("get_id", GetIdCaptcha);
- return TwoCaptcha.Execute(Request).Content;
- }
- public TwoCaptchaSharp(string key)
- { Key = key; }
- }
- public class TwoCaptchaSharpImage
- {
- public TwoCaptchaSharpImage(string path)
- {
- Extensions = new string[] { ".jpg", ".png", ".jpeg" };
- var info = new System.IO.FileInfo(path);
- if (!info.Directory.Exists)
- { throw new System.IO.DirectoryNotFoundException(); }
- else if (!info.Exists)
- { throw new System.IO.FileNotFoundException(); }
- else if (!Extensions.ToList<string>().Exists(x => x == info.Extension.ToLower()))
- { throw new Exception("Incorrect extension. Image extension must be jpg, jpeg or png."); }
- else
- {
- Path = path;
- IsPathDefined = true;
- }
- }
- public TwoCaptchaSharpImage(Byte[] bytearr)
- {
- ByteArr = bytearr;
- IsBytesDefined = true;
- }
- public bool IsPathDefined { get; private set; }
- public bool IsBytesDefined { get; private set; }
- public string Path { get; private set; }
- public Byte[] ByteArr { get; private set; }
- string[] Extensions { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment