Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
4.2 kB
12
Indexable
Never
 public class RequestFull : IDisposable
        {
            public RequestHTTP Client = new RequestHTTP();
            public RequestFull()
            {
                Client = new RequestHTTP();
            }

            public void Dispose()
            {
                Client?.TryDispose();
            }
            ~RequestFull()
            {
                Client?.TryDispose();
            }    

        }
        public class ImageToTextNope
        {
            private string _APIKey { get; set; }

            public ImageToTextNope(string apikey)
            {
                _APIKey = apikey;
            }
            
            public CaptchaResult SolveImageToText(string base64String, string customize = "")
            {
                using (RequestFull httpClient = new RequestFull())
                {
                    try
                    {                       
                        string content = "{\"key\":\""+ _APIKey + "\",\"type\":\"textcaptcha\",\"image_data\":[\"" + base64String + "\"]}";
                        string result2 = httpClient.Client.PostJson("https://api.nopecha.com/", content).Result;
                        if (string.IsNullOrEmpty(result2))
                            return new CaptchaResult
                            {
                                IsSuccess = false,
                                Message = "Lỗi xảy ra"
                            };                       
                        string taskId = Regex.Match(result2.ClearJson(), "\"data\":\"(.*?)\"").Groups[1].Value;
                        if (string.IsNullOrEmpty(taskId))
                            return new CaptchaResult
                            {
                                IsSuccess = false,
                                Message = "Lỗi xảy ra"
                            };
                        return GetResult(taskId);
                    }
                    catch { }                    
                    return new CaptchaResult
                    {
                        IsSuccess = false,
                        Message = "Lỗi xảy ra"
                    };
                }
            }

            private CaptchaResult GetResult(string _taskId)
            {
                using (RequestFull httpClient = new RequestFull())
                {
                    DateTime now = DateTime.Now;
                    while ((DateTime.Now - now).TotalSeconds < 120.0)
                    {
                        try
                        {                           
                            string content = "{\"key\":\"" + _APIKey + "\",\"id\":" + _taskId + "}";
                            string result2 = httpClient.Client.Get($"https://api.nopecha.com?key={_APIKey}&id={_taskId}").Result;
                            if (string.IsNullOrEmpty(result2))
                            {
                                return new CaptchaResult
                                {
                                    IsSuccess = false,
                                    Message = "Lỗi mạng"
                                };
                            }


                            string code = Regex.Match(result2.ClearJson(), "[[]\"(.*?)\"").Groups[1].Value;
                            if (string.IsNullOrEmpty(code))
                            {
                                Task.Delay(2000).Wait();
                                continue;
                            } 
                                
                            return new CaptchaResult
                            {
                                IsSuccess = true,
                                Result = code
                            };

                        }
                        catch
                        {
                        }
                    }
                }

                return new CaptchaResult
                {
                    IsSuccess = false,
                    Message = "CLIENT TIMEOUT"
                };
            }
        }
Leave a Comment