Beispiel #1
0
 public CrawlerEngine(CrawlerDataHolder crawlerDataHolder)
 {
     this.crawlerDataHolder = crawlerDataHolder;
 }
Beispiel #2
0
        private void tsbStartCrawler_Click(object sender, EventArgs e)
        {
            tslCrawler.ForeColor = Color.Black;

            if (!isCrawlerStarted)
            {
                string target = Helper.FixStartUrl(txtUrl.Text);
                txtUrl.Text = target;
                if (!Uri.IsWellFormedUriString(target, UriKind.Absolute))
                {
                    MessageBox.Show("Invalid Url Address.", "PeNCrawLer", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                crawlerDataHolder = new Core.CrawlerDataHolder()
                {
                    targetUrl  = txtUrl.Text,
                    trwCrawler = this.trwCrawler,
                    lvwCrawler = this.lvwCrawler
                };

                if (!Advanced_Settings.CrawlerSettings.IsCustomizeSettings)
                {
                    crawlerDataHolder.httpClient = new HttpClient();
                    Advanced_Settings.CrawlerSettings.HtmlTagAndAttribute = new Dictionary <string, string>();
                    Advanced_Settings.CrawlerSettings.HtmlTagAndAttribute.Add("a", "href");
                    Advanced_Settings.CrawlerSettings.HtmlTagAndAttribute.Add("link", "href");
                }

                crawlerEngine    = new Core.CrawlerEngine(crawlerDataHolder);
                isCrawlerStarted = true;

                tslCrawler.Text = "Processing ...";
                StartTime       = DateTime.Now;
                timerCrawler.Start();

                new System.Threading.Thread(new System.Threading.ThreadStart(() =>
                {
                    taskCrawler = new Task(() =>
                    {
                        if (crawlerEngine.AnalyseTarget(crawlerDataHolder.targetUrl))
                        {
                            crawlerEngine.Crawl(crawlerDataHolder.targetUrl);
                        }

                        else
                        {
                            crawlerDataHolder.FirstCheckStatus = false;
                        }
                    });

                    taskCrawler.Start();
                    taskCrawler.Wait();

                    if (crawlerDataHolder.FirstCheckStatus)
                    {
                        toolStrip1.Invoke((MethodInvoker) delegate
                        {
                            tslCrawler.ForeColor = Color.Green;
                        });

                        tslCrawler.Text         = "Operation Successfully Completed.";
                        tslCrawlerRequests.Text = $"Total number of processed requests: {RequestCalculator.crawlerRequests}";
                    }

                    else
                    {
                        toolStrip1.Invoke((MethodInvoker) delegate
                        {
                            tslCrawler.ForeColor = Color.Red;
                        });

                        tslCrawler.Text = "The Operation Failed!";
                    }

                    timerCrawler.Stop();
                }))

                {
                    IsBackground = true
                }.Start();
            }

            else
            {
                if (crawlerDataHolder.IsPauseRequested)
                {
                    tslCrawler.Text = "Processing ...";
                    timerCrawler.Start();
                    crawlerDataHolder.Resume();
                }

                else
                {
                    DialogResult result = MessageBox.Show("Crawler is currently running\nAre you sure you want to start new one?", "PeNCrawLer 0.1.0 - Crawler", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    if (result == DialogResult.Yes)
                    {
                        // cancel the task
                        isCrawlerStarted = false;
                    }

                    else if (result == DialogResult.No)
                    {
                        return;
                    }
                }
            }
        }