private void cmbTitleXpath_SelectionChanged(object sender, SelectionChangedEventArgs e) { var senderCombox = ((ComboBox)sender).SelectedValue; if (iscmbLoading || senderCombox == null || (selectedPattern.Path.TitleXPath != null && selectedPattern.Path.TitleXPath.Equals(senderCombox.ToString()))) { return; } selectedPattern.Path.TitleXPath = senderCombox.ToString(); var contents = XpathParser.ParseList(httpResult.Content, JsonConvert.SerializeObject(selectedPattern.Path), channelUrl); selectedPattern.Contents = contents.Articles.ToArray(); ChangeSelectedXpath(); }
private void btnOutPut_Click(object sender, RoutedEventArgs e) { try { var saveFileDialog1 = new SaveFileDialog { Filter = "Excel|*.xlsx|Excel 2003|*.xls", Title = "Save File" }; saveFileDialog1.ShowDialog(); if (string.IsNullOrEmpty(saveFileDialog1.FileName)) { return; } if (selectedPattern != null && selectedPattern.Path != null) { var contents = XpathParser.ParseList(httpResult.Content, JsonConvert.SerializeObject(selectedPattern.Path), channelUrl); List <Article> articles = new List <Article>(); foreach (var article in contents.Articles) { if (!string.IsNullOrWhiteSpace(article.Author) && article.Author.Contains(" ")) { article.Author = article.Author.Split(' ')[0]; } //Article _article = ThrinaxHelper.GetArticleAndFormatter(article.Url); //article.Content = _article.Content; //article.HtmlContent = _article.HtmlContent; //if (article.PubDate == null || article.PubDate <= DateTime.Parse("1970-01-01")) //{ // article.PubDate = _article.PubDate; //} articles.Add(article); } ExcelHelper.SaveExcelSheet(articles, saveFileDialog1.FileName, "Thrinax"); } } catch (Exception ex) { //log the err. } MessageBox.Show("Finish"); }
private void ChangeSelectedXpath() { iscmbLoading = true; btnRight.IsEnabled = false; btnWrong.IsEnabled = false; cmbUrlXpath.IsEnabled = false; cmbTitleXpath.IsEnabled = false; cmbTimeXpath.IsEnabled = false; cmbAuthorXpath.IsEnabled = false; cmbUrlXpath.Items.Clear(); cmbTitleXpath.Items.Clear(); cmbTimeXpath.Items.Clear(); cmbAuthorXpath.Items.Clear(); cmbUrlXpath.Items.Add(selectedPattern.Path.UrlXPath); //在 listPatterns 不为 null 的情况下绑定第一个模式的数据 cmbTitleXpath.Items.Add(selectedPattern.Path.TitleXPath); cmbTimeXpath.Items.Add(selectedPattern.Path.DateXPath ?? ""); //获取备选的时间模式 if (selectedPattern.BackUpPaths.ContainsKey(PatternType.Date)) { List <string> tempPaths = selectedPattern.BackUpPaths[PatternType.Date]; //判断是否为空,以及是否和当选的Xpath相同 if (tempPaths != null) { foreach (string tempPath in tempPaths) { if (!string.IsNullOrEmpty(tempPath) && !tempPath.Equals(selectedPattern.Path.DateXPath)) { cmbTimeXpath.Items.Add(tempPath); } } } } if (!string.IsNullOrEmpty(selectedPattern.Path.DateXPath)) { cmbTimeXpath.Items.Add(""); } cmbAuthorXpath.Items.Add(selectedPattern.Path.AuthorXPath ?? ""); //获取备选的作者模式 if (selectedPattern.BackUpPaths.ContainsKey(PatternType.Author)) { List <string> tempPaths = selectedPattern.BackUpPaths[PatternType.Author]; //判断是否为空,以及是否和当选的Xpath相同 if (tempPaths != null) { foreach (string tempPath in tempPaths) { if (!string.IsNullOrEmpty(tempPath) && !tempPath.Equals(selectedPattern.Path.AuthorXPath)) { cmbAuthorXpath.Items.Add(tempPath); } } } } if (!string.IsNullOrEmpty(selectedPattern.Path.AuthorXPath)) { cmbAuthorXpath.Items.Add(""); } cmbUrlXpath.SelectedIndex = 0; cmbTitleXpath.SelectedIndex = 0; cmbTimeXpath.SelectedIndex = 0; cmbAuthorXpath.SelectedIndex = 0; var contents = XpathParser.ParseList(httpResult.Content, JsonConvert.SerializeObject(selectedPattern.Path), channelUrl); //绑定出事DataGrid dataGridItems.ItemsSource = contents.Articles; btnRight.IsEnabled = true; btnWrong.IsEnabled = true; cmbTitleXpath.IsEnabled = true; cmbTimeXpath.IsEnabled = true; cmbAuthorXpath.IsEnabled = true; iscmbLoading = false; }
private void RefreshUI() { selectedPattern = null; if (string.IsNullOrEmpty(channelUrl) || listPagePatterns == null) { this.Dispatcher.Invoke(new Action(() => { //控制浏览区与判断区样式 geckoFormHost.IsEnabled = false; geckoWebBrowser.Navigate("about:blank"); geckoWebBrowser.IsAccessible = false; btnRight.IsEnabled = false; btnWrong.IsEnabled = false; //清除表格和下拉框样式与数据 dataGridItems.IsEnabled = false; cmbUrlXpath.IsEnabled = false; cmbTitleXpath.IsEnabled = false; cmbTimeXpath.IsEnabled = false; cmbAuthorXpath.IsEnabled = false; cmbUrlXpath.Items.Clear(); cmbTitleXpath.Items.Clear(); cmbTimeXpath.Items.Clear(); cmbAuthorXpath.Items.Clear(); //清除模式选择按钮状态 btnModel1.IsChecked = false; btnModel2.IsChecked = false; btnModel3.IsChecked = false; btnModel4.IsChecked = false; btnModel5.IsChecked = false; btnModel1.IsEnabled = false; btnModel2.IsEnabled = false; btnModel3.IsEnabled = false; btnModel4.IsEnabled = false; btnModel5.IsEnabled = false; })); } else { this.Dispatcher.Invoke(new Action(() => { //启用浏览区域与操作区域,加载缓存的网页 geckoFormHost.IsEnabled = true; try { if (geckoWebBrowser.Url != new Uri(channelUrl)) { geckoWebBrowser.Navigate(channelUrl); } } catch { } geckoWebBrowser.IsAccessible = true; btnRight.IsEnabled = true; btnWrong.IsEnabled = true; cmbUrlXpath.Items.Clear(); cmbTitleXpath.Items.Clear(); cmbTimeXpath.Items.Clear(); cmbAuthorXpath.Items.Clear(); //在 listPatterns 不为 null 的情况下绑定第一个模式的数据 if (listPagePatterns != null && listPagePatterns.Count > 0) { selectedPattern = listPagePatterns[0]; cmbUrlXpath.Items.Add(selectedPattern.Path.UrlXPath); cmbTitleXpath.Items.Add(selectedPattern.Path.TitleXPath); cmbTimeXpath.Items.Add(selectedPattern.Path.DateXPath ?? ""); //获取备选的时间模式 if (selectedPattern.BackUpPaths.ContainsKey(PatternType.Date)) { List <string> tempPaths = selectedPattern.BackUpPaths[PatternType.Date]; //判断是否为空,以及是否和当选的Xpath相同 if (tempPaths != null) { foreach (string tempPath in tempPaths) { if (!string.IsNullOrEmpty(tempPath) && !tempPath.Equals(selectedPattern.Path.DateXPath)) { cmbTimeXpath.Items.Add(tempPath); } } } } if (!string.IsNullOrEmpty(selectedPattern.Path.DateXPath)) { cmbTimeXpath.Items.Add(""); } cmbAuthorXpath.Items.Add(selectedPattern.Path.AuthorXPath ?? ""); //获取备选的作者模式 if (selectedPattern.BackUpPaths.ContainsKey(PatternType.Author)) { List <string> tempPaths = selectedPattern.BackUpPaths[PatternType.Author]; //判断是否为空,以及是否和当选的Xpath相同 if (tempPaths != null) { foreach (string tempPath in tempPaths) { if (!string.IsNullOrEmpty(tempPath) && !tempPath.Equals(selectedPattern.Path.AuthorXPath)) { cmbAuthorXpath.Items.Add(tempPath); } } } } if (!string.IsNullOrEmpty(selectedPattern.Path.AuthorXPath)) { cmbAuthorXpath.Items.Add(""); } cmbUrlXpath.SelectedIndex = 0; cmbTitleXpath.SelectedIndex = 0; cmbTimeXpath.SelectedIndex = 0; cmbAuthorXpath.SelectedIndex = 0; txtBaseXpath.Text = selectedPattern.Path?.ItemRootXPath; var contents = XpathParser.ParseList(httpResult.Content, JsonConvert.SerializeObject(selectedPattern.Path), channelUrl); //绑定出事DataGrid dataGridItems.ItemsSource = contents.Articles; } //启用表格和下拉框 dataGridItems.IsEnabled = true; cmbUrlXpath.IsEnabled = false; cmbTitleXpath.IsEnabled = true; cmbTimeXpath.IsEnabled = true; cmbAuthorXpath.IsEnabled = true; //根据数据情况确定模式按钮的可用性 btnModel1.IsEnabled = listPagePatterns.Count > 0; btnModel2.IsEnabled = listPagePatterns.Count > 1; btnModel3.IsEnabled = listPagePatterns.Count > 2; btnModel4.IsEnabled = listPagePatterns.Count > 3; btnModel5.IsEnabled = listPagePatterns.Count > 4; btnModel1.IsChecked = true; btnModel2.IsChecked = false; btnModel3.IsChecked = false; btnModel4.IsChecked = false; btnModel5.IsChecked = false; ChangeDisplayGecko(selectedPattern); })); } }
/// <summary> /// 验证一个List的模式是否能应用于某一个页面(只是检查是否明显不可能) /// </summary> /// <param name="Url"></param> /// <param name="RootNode"></param> /// <param name="XPath"></param> /// <returns></returns> public bool ValidateListXPath(string Url, HtmlNode RootNode, XpathPattern XPath) { if (string.IsNullOrEmpty(Url) || RootNode == null || XPath == null) { return(false); } List <Article> Content = XpathParser.ExtractItemFromList(Url, RootNode, XPath); if (Content == null || Content.Count < 3) { return(false); } int TitleCount = 0, DateCount = 0, ViewCount = 0, ReplyCount = 0, MediaCount = 0, AuthorCount = 0; foreach (Article ele in Content) { if (!string.IsNullOrEmpty(ele.Title) && !string.IsNullOrEmpty(ele.Url)) { TitleCount++; } if (!string.IsNullOrEmpty(XPath.DateXPath) && ele.PubDate != null) { DateCount++; } if (!string.IsNullOrEmpty(XPath.ViewXPath) && ele.ViewDataList?.FirstOrDefault()?.View >= 0) { ViewCount++; } if (!string.IsNullOrEmpty(XPath.ReplyXPath) && ele.ViewDataList?.FirstOrDefault()?.Reply >= 0) { ReplyCount++; } if (!string.IsNullOrEmpty(XPath.MediaNameXPath) && !string.IsNullOrEmpty(ele.MediaName)) { MediaCount++; } if (!string.IsNullOrEmpty(XPath.AuthorXPath) && !string.IsNullOrEmpty(ele.Author)) { AuthorCount++; } } if (TitleCount < Content.Count * 0.9) { return(false); } if (!string.IsNullOrEmpty(XPath.DateXPath) && DateCount < Content.Count * 0.9) { return(false); } if (!string.IsNullOrEmpty(XPath.ViewXPath) && ViewCount < Content.Count * 0.4) { return(false); } if (!string.IsNullOrEmpty(XPath.ReplyXPath) && ReplyCount < Content.Count * 0.1) { return(false); } if (!string.IsNullOrEmpty(XPath.MediaNameXPath) && MediaCount < Content.Count * 0.9) { return(false); } if (!string.IsNullOrEmpty(XPath.AuthorXPath) && AuthorCount < Content.Count * 0.9) { return(false); } return(true); }