Exemplo n.º 1
0
        internal bool ResolveAntiRobot(string qwantCaptchaId, string ruCaptchaId, List <RuClickCaptchaCoordinates> coordinatesList)
        {
            bool   result   = false;
            string url      = "https://api.qwant.com/api/anti_robot/resolve";
            string resQuery = string.Empty;

            foreach (RuClickCaptchaCoordinates coords in coordinatesList)
            {
                var parameters = new Dictionary <string, string>()
                {
                    { "id", qwantCaptchaId },
                    { "x", coords.x },
                    { "y", coords.y }
                };

                // post to QwantAPI robot resolve
                bool bResult    = false;
                var  postResult = HttpPostHelper.PostRequest(url, parameters, out bResult);

                if (bResult)
                {
                    QwantAntiRobotResolveResponse response = JsonConvert.DeserializeObject <QwantAntiRobotResolveResponse>(postResult);

                    if (response.status == "success")
                    {
                        // send reportgood
                        logger.Warn("Sending 'reportgood' to ruCaptcha");
                        resQuery = string.Format(RuCaptchaApi.ruCaptchaResUrl,
                                                 this.options.ruCaptchaApiKey,
                                                 "reportgood",
                                                 ruCaptchaId
                                                 );

                        result = true;
                        break;
                    }
                }

                Thread.Sleep(3000);
            }

            if (!result)
            {
                logger.Error("Sending 'reportbad' to ruCaptcha");
                // send reportbad
                resQuery = string.Format(RuCaptchaApi.ruCaptchaResUrl,
                                         this.options.ruCaptchaApiKey,
                                         "reportbad",
                                         ruCaptchaId
                                         );
            }


            var getResponse = HttpGetHelper.HttpGet(resQuery);

            return(result);
        }
Exemplo n.º 2
0
        internal bool GetAntiRobot()
        {
            string qwantCaptchaId = string.Empty;
            string ruCaptchaId    = string.Empty;
            string url            = "https://api.qwant.com/api/anti_robot/get";

            string response = HttpGetHelper.HttpGet(url);
            QwantAntiRobotResponse result = JsonConvert.DeserializeObject <QwantAntiRobotResponse>(response);

            if (result.status == "success")
            {
                qwantCaptchaId = result.data.id;

                /* sample code for saving captcha image
                 *
                 *
                 * string base64str = result.data.img.Split(',')[1];
                 * byte[] bytes = Convert.FromBase64String(base64str);
                 *
                 * Image image;
                 * using (MemoryStream ms = new MemoryStream(bytes))
                 * {
                 *  image = Image.FromStream(ms);
                 * }
                 *
                 * Bitmap img = new Bitmap(image);
                 * img.Save($"{outputPath}\\antiRobot.png");*/


                var parameters = new Dictionary <string, string>()
                {
                    { "coordinatescaptcha", "1" },
                    { "method", "base64" },
                    { "key", this.options.ruCaptchaApiKey },
                    { "body", result.data.img },
                    { "json", "1" },
                    { "textinstructions", "Найдите фигуру, отличающуюся от остальных" }
                };

                bool bResult    = false;
                var  postResult = HttpPostHelper.PostRequest(RuCaptchaApi.ruCaptchaInUrl, parameters, out bResult);

                if (bResult)
                {
                    bResult = false;
                    var captchaInResult = JsonConvert.DeserializeObject <RuCaptchaApi.RuCaptchaResponse>(postResult);

                    if (captchaInResult.status != "1")
                    {
                        logger.Warn($"RuCaptcha/in.php post request error {captchaInResult.request}");
                    }
                    else
                    {
                        // captcha post request successfull, try get result
                        ruCaptchaId = captchaInResult.request;
                        bool captchaRecognized = false;
                        int  errorsCount       = 0;

                        do
                        {
                            Thread.Sleep(5000);

                            string resQuery = string.Format(RuCaptchaApi.ruCaptchaResUrl,
                                                            this.options.ruCaptchaApiKey,
                                                            "get",
                                                            ruCaptchaId
                                                            );

                            response = HttpGetHelper.HttpGet(resQuery);
                            RuCaptchaApi.RuCaptchaBasicResponse captchaGetResult;

                            try
                            {
                                captchaGetResult  = JsonConvert.DeserializeObject <RuCaptchaApi.RuCaptchaResponse>(response);
                                captchaRecognized = captchaGetResult.status == "1";

                                if (!captchaRecognized)
                                {
                                    logger.Warn($"RuCaptcha/res.php get result error {(captchaGetResult as RuCaptchaApi.RuCaptchaResponse).request}");
                                    errorsCount++;
                                }
                            }
                            catch (Exception ex)
                            {
                                captchaGetResult  = JsonConvert.DeserializeObject <RuCaptchaApi.RuClickCaptchaResponse>(response);
                                captchaRecognized = captchaGetResult.status == "1";

                                if (!captchaRecognized)
                                {
                                    logger.Warn($"RuCaptcha/res.php get result unknown error");
                                    errorsCount++;
                                }
                            }

                            if (captchaRecognized)
                            {
                                var captchaCoordinatesResult = JsonConvert.DeserializeObject <RuCaptchaApi.RuClickCaptchaResponse>(response);
                                //var coordinates = captchaCoordinatesResult.request[0];

                                // send results to QwantApi
                                bResult = this.ResolveAntiRobot(qwantCaptchaId, ruCaptchaId, captchaCoordinatesResult.request);
                            }

                            Thread.Sleep(5000);
                        }while (!captchaRecognized && (errorsCount < 10));
                    }
                }

                return(bResult);
            }

            return(false);
        }