private void CreateApiTask(AntiCaptchaTask task, RecaptchaParameter parameter) { task.Id = null; Task <string> apiTask = AntiCaptchaApi.CreateReCaptchaTask(ApiKey, parameter.SiteKey, parameter.SitePath, CancelToken); lock (m_lock) { m_tasks[task] = apiTask; } }
public ICaptchaSolutionSourceTask GetSolution(object parameter) { AntiCaptchaTask res = new AntiCaptchaTask(); RecaptchaParameter p = parameter as RecaptchaParameter; res.Parameter = p; CreateApiTask(res, p); lock (m_lock) { if (!m_isCheckTimerLaunched) { m_isCheckTimerLaunched = true; m_checkTasksTimer.Change(m_checkPeriod, Timeout.Infinite); } } return(res); }
private void CheckTasksStatus() { Dictionary <AntiCaptchaTask, Task <string> > allTasks = null; List <AntiCaptchaTask> goodTasks = new List <AntiCaptchaTask>(); List <AntiCaptchaTask> badTasks = new List <AntiCaptchaTask>(); List <AntiCaptchaTask> canceledTasks = new List <AntiCaptchaTask>(); lock (m_lock) { allTasks = new Dictionary <AntiCaptchaTask, Task <string> >(m_tasks); } foreach (KeyValuePair <AntiCaptchaTask, 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 <Dictionary <string, string> > checkTask = AntiCaptchaApi.CheckReCaptchaTask(ApiKey, goodTasks.Select(t => t.Id).ToList(), CancelToken); Dictionary <string, string> solutions = null; try { checkTask.Wait(CancelToken); solutions = checkTask.Result; } catch (Exception e) { } if (solutions != null) { foreach (KeyValuePair <string, string> pair in solutions) { AntiCaptchaTask task = goodTasks.First(t => t.Id == pair.Key); if (pair.Value == AntiCaptchaResponseCode.ERROR_CAPTCHA_UNSOLVABLE.ToString() || pair.Value == AntiCaptchaResponseCode.ERROR_NO_SUCH_CAPCHA_ID.ToString()) { goodTasks.Remove(task); badTasks.Add(task); } else if (pair.Value != AntiCaptchaResponseCode.CAPTCHA_IS_NOT_READY.ToString()) { task.Solution = pair.Value.Substring(1); } } } } 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 (AntiCaptchaTask task in badTasks) { task.Solution = null; //CreateApiTask(task, task.Parameter); } lock (m_lock) { if (m_tasks.Any()) { m_checkTasksTimer.Change(m_checkPeriod, Timeout.Infinite); } else { m_isCheckTimerLaunched = false; } } }