Пример #1
0
        /// <summary>
        /// 点击结果集
        /// </summary>
        /// <param name="browser">浏览器实例</param>
        /// <param name="results">要点击的结果集</param>
        /// <param name="record">当前执行的任务</param>
        /// <param name="startTime">当前任务开始的时间</param>
        /// <param name="cts"></param>
        /// <returns>是否超时,是则返回true</returns>
        private async Task <bool> ExecuteClick(BrowserForm browser, IEnumerable <SearchResult> results, Record record, int startTime, CancellationToken cts)
        {
            foreach (var result in results ?? Enumerable.Empty <SearchResult>())
            {
                //超时
                if (Environment.TickCount - startTime >= Global.Setting.TimeSpent * 60 * 1000)
                {
                    return(true);
                }
                if (IsLegal(result.Title, result.PartOfRealUri))                                                        //判断标题和uri是否符合黑白名单规则
                {
                    var code = result.IsAdv ? _jsCodeHelper.ClickAdv(result.Id) : _jsCodeHelper.ClickResult(result.Id); //打开页面
                    await browser.ExecuteJsAsync(code, cts);

                    var dwellTime = _random.Next(Global.Setting.ClickMin, Global.Setting.ClickMax);
                    await Task.Delay(dwellTime * 1000, cts);    //停留时间

                    record.DwellTime = $"{dwellTime} s";
                    record.Ip        = await HttpHelper.GetCurrentIpAsync(cts);

                    browser.OwnedForms.FirstOrDefault()?.Close();
                }
                else
                {
                    record.Ip        = string.Empty;
                    record.DwellTime = Resources.ignoreTask;
                }

                record.Title = result.Title;
                record.Url   = result.IsAdv ? $"{Resources.advertising} {result.PartOfRealUri}" : result.PartOfRealUri;
                AddLogItem(record); //打印日志
            }
            return(false);
        }
Пример #2
0
        /// <summary>
        /// 以点击模式执行任务
        /// </summary>
        /// <param name="cts"></param>
        /// <returns></returns>
        private async Task ExecuteWithClickMode(CancellationToken cts)
        {
            do
            {
                ResetTasks();

                foreach (var record in Global.Tasks)
                {
                    SelectTask(record.Id);

                    var startTime = Environment.TickCount;

                    var browser = new BrowserForm(visible: _isShowBrowser);
                    AddOwnedForm(browser);
                    await Task.Delay(1000, cts);                                                                        //等待初始化Cef

                    await browser.NavigateToAsync("https://www.baidu.com", cts);                                        //打开百度首页

                    await Task.Delay(_random.Next(Global.Setting.IntervalMin, Global.Setting.IntervalMax) * 1000, cts); //间隔时间

                    var code = _jsCodeHelper.Search(record.Keyword);                                                    //搜索关键词
                    await browser.ExecuteJsAsync(code, cts);

                    var target = Global.Setting.PageMin;
                    var delay  = _random.Next(Global.Setting.SearchMin, Global.Setting.SearchMax) * 1000;
                    await browser.PageTurnTo(target, cts, delay);    //跳到目标起始页

                    while (target <= Global.Setting.PageMax)
                    {
                        code = _jsCodeHelper.InitObject(); //js获取搜索结果列表,创建 mousedown 事件
                        await browser.ExecuteJsAsync(code, cts, delay);

                        var source = await browser.GetSourceAsync();

                        var advs      = source.GetAdvs(); //取得广告列表
                        var isTimeout = await ExecuteClick(browser, advs, record, startTime, cts);

                        if (isTimeout)
                        {
                            break;
                        }

                        var results = source.GetResults();   //取得搜索结果列表
                        isTimeout = await ExecuteClick(browser, results, record, startTime, cts);

                        if (isTimeout)
                        {
                            break;
                        }

                        if (++target <= Global.Setting.PageMax)
                        {
                            await browser.ExecuteJsAsync(_jsCodeHelper.NextPage(), cts);   //下一页
                        }
                    }

                    EndTask(record.Id);
                    browser.Close();
                }
            } while (loopCheckBox.Checked);
        }