Example #1
0
        private void CreateApiTask(TwoCaptchaTask task, RecaptchaParameter parameter)
        {
            task.Id = null;

            Task <string> apiTask = TwoCaptchaAPI.CreateReCaptchaTask(ApiKey, parameter.SiteKey, parameter.SitePath, CancelToken);

            lock (m_lock)
            {
                m_tasks[task] = apiTask;
            }
        }
Example #2
0
        public ICaptchaSolutionSourceTask GetSolution(object parameter)
        {
            TwoCaptchaTask     res = new TwoCaptchaTask();
            RecaptchaParameter p   = parameter as RecaptchaParameter;

            res.Parameter = p;

            CreateApiTask(res, p);

            m_checkTasksTimer.Change(m_checkPeriod, Timeout.Infinite);

            return(res);
        }
Example #3
0
        private void CheckTasksStatus()
        {
            Dictionary <TwoCaptchaTask, Task <string> > allTasks = null;
            List <TwoCaptchaTask> goodTasks     = new List <TwoCaptchaTask>();
            List <TwoCaptchaTask> badTasks      = new List <TwoCaptchaTask>();
            List <TwoCaptchaTask> canceledTasks = new List <TwoCaptchaTask>();

            lock (m_lock)
            {
                allTasks = new Dictionary <TwoCaptchaTask, Task <string> >(m_tasks);
            }

            foreach (KeyValuePair <TwoCaptchaTask, Task <string> > pair in allTasks)
            {
                if (pair.Value.IsCompleted)
                {
                    if (!pair.Value.IsCanceled)
                    {
                        string id = pair.Value.Result;

                        if (id != null)
                        {
                            pair.Key.Id = id;
                            goodTasks.Add(pair.Key);
                        }
                        else
                        {
                            badTasks.Add(pair.Key);
                        }
                    }
                    else
                    {
                        pair.Key.Solution = null;
                        canceledTasks.Add(pair.Key);
                    }
                }
            }

            if (goodTasks.Any())
            {
                Task <List <string> > checkTask = TwoCaptchaAPI.CheckReCaptchaTask(ApiKey, goodTasks.Select(t => t.Id).ToList(), CancelToken);

                List <string> solutions = null;

                try
                {
                    checkTask.Wait(CancelToken);
                    solutions = checkTask.Result;
                }
                catch (Exception e)
                {
                }

                if (solutions != null)
                {
                    for (int i = 0; i < solutions.Count; i++)
                    {
                        TwoCaptchaTask task = goodTasks[i];

                        if (solutions[i] == TwoCaptchaResponseCode.ERROR_CAPTCHA_UNSOLVABLE.ToString() ||
                            solutions[i] ==
                            TwoCaptchaResponseCode.ERROR_WRONG_CAPTCHA_ID.ToString())
                        {
                            goodTasks.Remove(task);
                            badTasks.Add(task);
                        }
                        else if (solutions[i] != TwoCaptchaResponseCode.CAPCHA_NOT_READY.ToString())
                        {
                            task.Solution = solutions[0];
                        }
                    }
                }
            }

            lock (m_lock)
            {
                m_tasks = m_tasks.Where(p => (!goodTasks.Contains(p.Key) || p.Key.Solution == null) && !canceledTasks.Contains(p.Key)).ToDictionary(p => p.Key, p => p.Value);
            }

            foreach (TwoCaptchaTask task in badTasks)
            {
                CreateApiTask(task, task.Parameter);
            }

            lock (m_lock)
            {
                if (m_tasks.Any())
                {
                    m_checkTasksTimer.Change(m_checkPeriod, Timeout.Infinite);
                }
            }
        }