public GetSpider(SpiderSettings settings) { imgType = settings.imgType; domain = settings.domain; siteName = settings.siteName; savePath = settings.savePath + @"\" + siteName + @"\" + imgType + @"\";//合法目录 if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); //创建目录 } allDirectiories = String.Join("|", Directory.GetDirectories(savePath, "*")); //获取所有文件名 startPage = settings.startPage; endPage = settings.endPage; ws = WebsiteInfo.websiteList.Find(s => s.siteName == settings.siteName); //获取WebSites //MyWebsiteConverter = ws.websiteConverter;//获取合适Converter Settings = settings; OnPageFetched = (s, e) => { downloadPageImg(e.imgInfoResult); }; OnPageFinished = (s, e) => { if (e != null) { myWriteLine("Fetch Page Finished:Index[" + e.imgInfoResult.picIndex + "]Title:" + e.imgInfoResult.title, ConsoleColor.Yellow); } if ((finishImgCount == fetchImgCount && fetchIndexPageFlag) || workFinishFlag || stopWorkFlag) { end(); } }; }
private void HSButton_Click(object sender, EventArgs e) { if (HSButton.Text == "Start Download") { Task clickTask = new Task(() => { HSButton.Text = "Finish Download"; settingGroupBox.Enabled = false; WebsiteInfo.websiteList[WebsiteInfo.websiteList.FindIndex(s => s.siteName == siteComboBox.Text)] = new WebSiteSetting() { siteName = siteComboBox.Text, domain = urlTextBox.Text, imgType = imgTypeTextBox.Text, imgKeys = imgKeysTextBox.Text.Split('|'), pageRegex = pageRegTextBox.Text, urlPattern = urlPatTextBox.Text, firstPageUrlPattern = _1stUrlTextBox.Text, }; saveCurrentSettings(); NetworkSpeed.init(); MySettings = new SpiderSettings(); MySettings.TextKeywords.Add("P"); MySettings.imgType = imgTypeTextBox.Text; MySettings.domain = urlTextBox.Text; MySettings.siteName = siteComboBox.Text; MySettings.savePath = pathTextBox.Text; MySettings.startPage = (long)startPageNUD.Value; MySettings.endPage = (long)endPageNUD.Value; MySpider = new GetSpider(MySettings); MySpider.OnPageFetched += (s, ex) => { mylistBox.Items.Insert(0, ex.imgInfoResult.picIndex + " " + ex.imgInfoResult.title); progressBar.Maximum = (int)MySpider.fetchPageCount; }; MySpider.OnPageFinished += (s, ex) => { if (ex != null && MySpider != null) { //if (mylistBox.Items.Contains(ex.imgInfoResult.picIndex + " " + ex.imgInfoResult.title)) { mylistBox.Items.Remove(ex.imgInfoResult.picIndex + " " + ex.imgInfoResult.title); //} progressBar.Value = (int)MySpider.finishPageCount; } if (MySpider != null && MySpider.workFinishFlag) { progressBar.Value = 0; mylistBox.Items.Clear(); settingGroupBox.Enabled = true; HSButton.Enabled = true; HSButton.Text = "Start Download"; } }; MySpider.run(); //MySpider.test(); } ); clickTask.Start(); } else if (HSButton.Text == "Finish Download") { HSButton.Enabled = false; MySpider.stopWorkFlag = true; } }