예제 #1
0
        /**
         * Sends captcha to `/in.php` and waits for it's result.
         * This helper can be used instead of manual using of `send` and `getResult` functions.
         *
         * @param captcha
         * @param waitOptions
         * @throws Exception
         */
        public async Task Solve(Captcha.Captcha captcha, Dictionary <string, int> waitOptions)
        {
            captcha.Id = await Send(captcha);

            if (!lastCaptchaHasCallback)
            {
                await WaitForResult(captcha, waitOptions);
            }
        }
예제 #2
0
        /**
         * Sends captcha to `/in.php` and waits for it's result.
         * This helper can be used instead of manual using of `send` and `getResult` functions.
         *
         * @param captcha
         * @throws Exception
         */
        public async Task Solve(Captcha.Captcha captcha)
        {
            var waitOptions = new Dictionary <string, int>();

            if (captcha.GetType() == typeof(ReCaptcha))
            {
                waitOptions["timeout"] = RecaptchaTimeout;
            }

            await Solve(captcha, waitOptions);
        }
예제 #3
0
        /**
         * Sends captcha to '/in.php', and returns its `id`
         *
         * @param captcha
         * @return
         * @throws Exception
         */
        public async Task <string> Send(Captcha.Captcha captcha)
        {
            var parameters = captcha.GetParameters();
            var files      = captcha.GetFiles();

            SendAttachDefaultParameters(parameters);

            ValidateFiles(files);

            string response = await apiClient.In(parameters, files);

            if (!response.StartsWith("OK|"))
            {
                throw new ApiException("Cannot recognise api response (" + response + ")");
            }

            return(response.Substring(3));
        }
예제 #4
0
        /**
         * This helper waits for captcha result, and when result is ready, returns it
         *
         * @param captcha
         * @param waitOptions
         * @throws Exception
         */
        public async Task WaitForResult(Captcha.Captcha captcha, Dictionary <string, int> waitOptions)
        {
            long startedAt = CurrentTime();

            int timeout         = waitOptions.TryGetValue("timeout", out timeout) ? timeout : DefaultTimeout;
            int pollingInterval = waitOptions.TryGetValue("pollingInterval", out pollingInterval)
                ? pollingInterval
                : PollingInterval;

            while (true)
            {
                long now = CurrentTime();

                if (now - startedAt < timeout)
                {
                    await Task.Delay(pollingInterval * 1000).ConfigureAwait(false);
                }
                else
                {
                    break;
                }

                try
                {
                    string result = await GetResult(captcha.Id);

                    if (result != null)
                    {
                        captcha.Code = result;
                        return;
                    }
                }
                catch (NetworkException)
                {
                    // ignore network errors
                }
            }

            throw new TimeoutException("Timeout " + timeout + " seconds reached");
        }