private void lV_Definitions_SelectedIndexChanged(object sender, EventArgs e) { object ob = null; if (lV_Definitions.SelectedItems != null) { if (lV_Definitions.SelectedItems.Count > 0) { ob = lV_Definitions.SelectedItems[0].Tag; if (ob.GetType() == typeof(WebPageFeedDef)) { WebPageFeedDef def = (WebPageFeedDef)ob; if (def != null) { tb_DateID.Text = def.ClassID_UpdateTime; tB_FeeditemID.Text = def.ClassID_Title; tB_BaseURL.Text = def.BaseURL; tB_Name.Text = def.Name; tB_TestpageUrl.Text = ""; } } } } rTB_Testresults.Text = ""; }
private void bTest_Click(object sender, EventArgs e) { string result = ""; WebPageFeedDef newDef = GetWebPageDef(); string testpage = tB_TestpageUrl.Text; bool b_testPage = !string.IsNullOrEmpty(testpage) && !string.IsNullOrWhiteSpace(testpage); if (newDef != null && (b_testPage)) { result = parentController.GetDefTestResults(newDef, testpage); } else { result = "No valid input for definition and/or testpage given."; } rTB_Testresults.Text = result; }
private void bAdd_Click(object sender, EventArgs e) { WebPageFeedDef newDef = GetWebPageDef(); if (newDef != null) { if (webPageFeedDefinitionList == null) { webPageFeedDefinitionList = new List <WebPageFeedDef>(); } webPageFeedDefinitionList.Add(newDef); UpdateDefListview(); } else { MessageBox.Show("No valid input for definition and/or testpage given.", "Add new Definition", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } }
private WebPageFeedDef GetWebPageDef() { WebPageFeedDef result = null; string dateID = tb_DateID.Text; string feeditemID = tB_FeeditemID.Text; string baseURL = tB_BaseURL.Text; string name = tB_Name.Text; bool b_dateID = !string.IsNullOrEmpty(dateID) && !string.IsNullOrWhiteSpace(dateID); bool b_baseURL = !string.IsNullOrEmpty(baseURL) && !string.IsNullOrWhiteSpace(baseURL); bool b_feedID = !string.IsNullOrEmpty(feeditemID) && !string.IsNullOrWhiteSpace(feeditemID); bool b_Name = !string.IsNullOrEmpty(name) && !string.IsNullOrWhiteSpace(name); if (b_Name && b_feedID && b_baseURL && b_dateID) { result = new WebPageFeedDef(name, name.ToLower().Replace(" ", ""), baseURL, feeditemID, dateID); } return(result); }
/// <summary> /// try to get a feed form a url using the given definition /// </summary> /// <param name="pageUrl"></param> /// <param name="feed"></param> /// <param name="webPageFeedDef"></param> public void Read(string pageUrl, ref Feed feed, WebPageFeedDef webPageFeedDef) { feed = null; if (webPageFeedDef != null) { if (!pageUrl.ToLower().Contains(webPageFeedDef.BaseURL.ToLower())) { return; } } else { return; } bool loadSuccess = false; string content = ""; /* * try * { * //try to download the main page * content = new System.Net.WebClient().DownloadString(pageUrl); * //Console.WriteLine(content); * loadSuccess = true; * } * catch (Exception ex) * { * Console.WriteLine("Error while getting feed-webpage: " + Environment.NewLine + ex.Message); * } */ if (loadSuccess) { var htmlDoc = new HtmlAgilityPack.HtmlDocument() { OptionAutoCloseOnEnd = true, OptionFixNestedTags = true }; htmlDoc.LoadHtml(content); try { Clipboard.SetText(content); } catch (Exception ex) { Console.WriteLine(ex.Message); //Console.WriteLine(content); } if (htmlDoc.DocumentNode != null) { feed = new Feed(); feed.FeedURL = pageUrl; //get title (if not properly loaded already) HtmlNode titleNode = htmlDoc.DocumentNode.Descendants("title").FirstOrDefault(); if (titleNode != null) { feed.Title = System.Web.HttpUtility.HtmlDecode(titleNode.InnerText); } else { Console.WriteLine("titlenode is null!"); } //get feeditems string classID_Title = webPageFeedDef.ClassID_Title; string classID_UpdateTime = webPageFeedDef.ClassID_UpdateTime; HtmlNodeCollection titleNodes = htmlDoc.DocumentNode.SelectNodes(classID_Title); HtmlNodeCollection timeNodes = htmlDoc.DocumentNode.SelectNodes(classID_UpdateTime); try { if (titleNodes != null && timeNodes != null) { if (timeNodes.Count > 0 && titleNodes.Count > 0) { feed.Items = new List <FeedItem>(); //create List of items from detected entries with their upload-Dates for (int i = 0; i < Math.Min(timeNodes.Count, titleNodes.Count); i++) { string suburl = titleNodes[i].OuterHtml; if (suburl.Contains("/")) { suburl = suburl.Remove(0, titleNodes[i].OuterHtml.IndexOf("/")); } string url = webPageFeedDef.BaseURL; if (suburl.Contains("\"")) { url += suburl.Remove(suburl.IndexOf("\"")); } string title = System.Web.HttpUtility.HtmlDecode(titleNodes[i].InnerHtml); //search for feeditem-title in the innerHtml foreach (string titleMarker in titleMarkers) { if (title.ToLower().Contains(titleMarker)) { title = title.Remove(0, title.ToLower().LastIndexOf(titleMarker) + titleMarker.Length); title = title.Remove(title.IndexOf("\"")); } } DateTime updateTime = getDateTime(timeNodes[i].InnerHtml); FeedItem item = new FeedItem(); item.Id = url; item.Link = url; item.PublishingDate = updateTime; item.Read = false; item.Title = title; feed.Items.Add(item); } feed.Updated = true; } } else { Console.WriteLine("FeedURL: " + feed.FeedURL + " Title = " + feed.Title + ": no Chapter-Nodes detected. DetectionText: '" + classID_Title + "'"); if (timeNodes == null) { Console.WriteLine("TimeNodes is null."); } else { Console.WriteLine("Number of timenodes = " + timeNodes.Count()); } } } catch (Exception ex) { Console.WriteLine("Innerexception 1: " + ex.Message); } } } }
public string TestRead(string pageUrl, WebPageFeedDef webPageFeedDef) { string result = ""; if (webPageFeedDef != null) { if (!pageUrl.ToLower().Contains(webPageFeedDef.BaseURL.ToLower())) { return(result); } } else { return(result); } bool loadSuccess = false; string content = ""; try { //try to download the main page content = new System.Net.WebClient().DownloadString(pageUrl); loadSuccess = true; } catch (Exception ex) { result = "Error while getting feed-webpage: " + Environment.NewLine + ex.Message; } if (loadSuccess) { var htmlDoc = new HtmlAgilityPack.HtmlDocument() { OptionAutoCloseOnEnd = true, OptionFixNestedTags = true }; htmlDoc.LoadHtml(content); if (htmlDoc.DocumentNode != null) { string feedTitle = ""; //get title (if not properly loaded already) HtmlNode titleNode = htmlDoc.DocumentNode.Descendants("title").FirstOrDefault(); if (titleNode != null) { feedTitle = System.Web.HttpUtility.HtmlDecode(titleNode.InnerText); result += "Title = " + feedTitle + Environment.NewLine; } else { result += "No Title found" + Environment.NewLine; } //get feeditems string classID_Title = webPageFeedDef.ClassID_Title; string classID_UpdateTime = webPageFeedDef.ClassID_UpdateTime; HtmlNodeCollection titleNodes = htmlDoc.DocumentNode.SelectNodes(classID_Title); HtmlNodeCollection timeNodes = htmlDoc.DocumentNode.SelectNodes(classID_UpdateTime); try { if (titleNodes != null && timeNodes != null) { if (timeNodes.Count > 0 && titleNodes.Count > 0) { //create List of feeditems from detected entries with their upload-Dates for (int i = 0; i < Math.Min(timeNodes.Count, titleNodes.Count); i++) { string suburl = titleNodes[i].OuterHtml; if (suburl.Contains("/")) { suburl = suburl.Remove(0, titleNodes[i].OuterHtml.IndexOf("/")); } string url = webPageFeedDef.BaseURL; if (suburl.Contains("\"")) { url += suburl.Remove(suburl.IndexOf("\"")); } //Debug.WriteLine("suburl = " + suburl + " url = " + url); string title = System.Web.HttpUtility.HtmlDecode(titleNodes[i].InnerHtml); //search for feeditem-title in the innerHtml foreach (string titleMarker in titleMarkers) { if (title.ToLower().Contains(titleMarker)) { title = title.Remove(0, title.ToLower().LastIndexOf(titleMarker) + titleMarker.Length); title = title.Remove(title.IndexOf("\"")); } } DateTime updateTime = getDateTime(timeNodes[i].InnerHtml); result += "Feeditem: Title: '" + title + "' Link: '" + url + "' publishing-Date: '" + updateTime.ToString("dd.MM.yyyy hh:mm") + "' " + Environment.NewLine; } } } else { result += feedTitle + ": no Feeditems detected." + Environment.NewLine; } } catch (Exception ex) { result += "Error getting Feed: " + ex.Message; } } } return(result); }