public FeedRetrievedEventArgs(FeedInfo feed) { this.feed = feed; }
public FeedInfo Parse(XmlReader reader) { try { FeedInfo info = new FeedInfo(); //create a List of type Dictionary<string,string> for the element names and values var items = new List<Dictionary<string, string>>(); // declare a Dictionary to capture each current Item in the while loop Dictionary<string, string> currentItem = null; bool parsingItems = false; /// Read each element with the reader while (reader.Read()) { // if it's an element, we want to process it if (reader.NodeType == XmlNodeType.Element) { string name = reader.Name; // rss1.0 - channel/title // rss2.0 - channel/title // atom0.3 - feed/title // atom1.0 - feed/title if (!parsingItems && name.ToLowerInvariant() == "title") { reader.Read(); info.ActualTitle = reader.Value; } if (name.ToLowerInvariant() == "item" || name.ToLowerInvariant() == "entry") { parsingItems = true; // Save previous item if (currentItem != null) items.Add(currentItem); // Create new item currentItem = new Dictionary<string, string>(); } else if (currentItem != null) { reader.Read(); // some feeds can have duplicate keys, so we don't want to blow up here: if (!currentItem.ContainsKey(name)) currentItem.Add(name, reader.Value.Trim()); } } } // Save previous item if (currentItem != null) items.Add(currentItem); // now create a List of type GenericFeedItem var itemList = new List<FeedItem>(); // iterate all our items from the reader foreach (var d in items) { var itm = new FeedItem(); //do a switch on the Key of the Dictionary <string, string> of each item foreach (string k in d.Keys) { switch (k) { case "title": itm.Title = d[k]; break; case "link": itm.Link = d[k]; break; case "published": case "pubDate": case "issued": DateTime dt; bool ok = Rfc822DateTime.TryParse(d[k], out dt); itm.PubDate = ok ? dt : DateTime.Now; break; case "content": case "description": itm.Description = d[k]; break; default: break; } } // add the created item to our List itemList.Add(itm); } info.Items = itemList; return info; } catch { return null; } }
/// <summary> /// Internal method used to raise the feed event /// </summary> protected void OnFeedRetrieved(FeedInfo feed) { this.actualName = feed.ActualTitle; DateTimeOffset mostRecentItem = this.feedLastUpdated; if (mostRecentItem == DateTimeOffset.MaxValue) mostRecentItem = DateTimeOffset.MinValue; System.Diagnostics.Debug.WriteLine(String.Format("Feed Retrieved: {0} - Most Recent Item: {1}", this.Name, mostRecentItem)); if (FeedRetrieved != null) { FeedRetrievedEventArgs e = new FeedRetrievedEventArgs(feed); FeedRetrieved(this, e); } if (FeedUpdated != null) { List<FeedItem> newitems = new List<FeedItem>(); foreach (FeedItem item in feed.Items) { DateTimeOffset itemDate = item.PubDate; //if (itemDate == DateTimeOffset.MinValue) itemDate = item.PublishDate; System.Diagnostics.Debug.WriteLine(String.Format("Item Published at: {0} - (last update at: {1})", itemDate, this.feedLastUpdated)); if (itemDate > mostRecentItem) mostRecentItem = itemDate; if (itemDate > this.feedLastUpdated) { newitems.Add(item); item.SourceFeed = feed; } } FeedUpdatedEventArgs args = new FeedUpdatedEventArgs(newitems); FeedUpdated(this, args); } this.feedLastUpdated = mostRecentItem; // feed.LastUpdatedTime is not always set =( }
public FeedInfo Parse(XmlReader reader) { try { FeedInfo info = new FeedInfo(); //create a List of type Dictionary<string,string> for the element names and values var items = new List <Dictionary <string, string> >(); // declare a Dictionary to capture each current Item in the while loop Dictionary <string, string> currentItem = null; bool parsingItems = false; /// Read each element with the reader while (reader.Read()) { // if it's an element, we want to process it if (reader.NodeType == XmlNodeType.Element) { string name = reader.Name; // rss1.0 - channel/title // rss2.0 - channel/title // atom0.3 - feed/title // atom1.0 - feed/title if (!parsingItems && name.ToLowerInvariant() == "title") { reader.Read(); info.ActualTitle = reader.Value; } if (name.ToLowerInvariant() == "item" || name.ToLowerInvariant() == "entry") { parsingItems = true; // Save previous item if (currentItem != null) { items.Add(currentItem); } // Create new item currentItem = new Dictionary <string, string>(); } else if (currentItem != null) { reader.Read(); // some feeds can have duplicate keys, so we don't want to blow up here: if (!currentItem.ContainsKey(name)) { currentItem.Add(name, reader.Value.Trim()); } } } } // Save previous item if (currentItem != null) { items.Add(currentItem); } // now create a List of type GenericFeedItem var itemList = new List <FeedItem>(); // iterate all our items from the reader foreach (var d in items) { var itm = new FeedItem(); //do a switch on the Key of the Dictionary <string, string> of each item foreach (string k in d.Keys) { switch (k) { case "title": itm.Title = d[k]; break; case "link": itm.Link = d[k]; break; case "published": case "pubDate": case "issued": DateTime dt; bool ok = Rfc822DateTime.TryParse(d[k], out dt); itm.PubDate = ok ? dt : DateTime.Now; break; case "content": case "description": itm.Description = d[k]; break; default: break; } } // add the created item to our List itemList.Add(itm); } info.Items = itemList; return(info); } catch { return(null); } }
void webclient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { WebClient wc = (WebClient)sender; // process the feed if (e.Error == null && e.Result != null) { /* THIS IS JUST FOR TESTING * List<byte> chars = new List<byte>(); * System.IO.StreamReader r = new System.IO.StreamReader(e.Result); * using (r) * { * while (r.Peek() > 0) * { * chars.Add((byte) r.Read()); * } * } * byte[] bytes = chars.ToArray(); * string s = System.Text.Encoding.UTF8.GetString(bytes); * Console.WriteLine(s); * */ FeedInfo info = null; XmlReaderSettings settings = new XmlReaderSettings(); settings.ProhibitDtd = false; using (XmlReader reader = XmlReader.Create(e.Result, settings)) { GenericFeedParser parser = new GenericFeedParser(); info = parser.Parse(reader); } if (info != null) { info.CustomTitle = this.CustomName; info.Url = this.Url; OnFeedRetrieved(info); } else { // the loader couldn't load the feed FeedErrorEventArgs args = new FeedErrorEventArgs(new FeedException(_parseErrorMessage)); OnFeedError(args); } } else { if (e.Error != null) { // there was an error returned from the call FeedErrorEventArgs args = new FeedErrorEventArgs(e.Error); OnFeedError(args); } else { // an empty stream. FeedErrorEventArgs args = new FeedErrorEventArgs(new FeedException(_nullResultErrorMessage)); OnFeedError(args); } } // restart the timer this.timer.Start(); }
/// <summary> /// Internal method used to raise the feed event /// </summary> protected void OnFeedRetrieved(FeedInfo feed) { this.actualName = feed.ActualTitle; DateTimeOffset mostRecentItem = this.feedLastUpdated; if (mostRecentItem == DateTimeOffset.MaxValue) { mostRecentItem = DateTimeOffset.MinValue; } System.Diagnostics.Debug.WriteLine(String.Format("Feed Retrieved: {0} - Most Recent Item: {1}", this.Name, mostRecentItem)); if (FeedRetrieved != null) { FeedRetrievedEventArgs e = new FeedRetrievedEventArgs(feed); FeedRetrieved(this, e); } if (FeedUpdated != null) { List <FeedItem> newitems = new List <FeedItem>(); foreach (FeedItem item in feed.Items) { DateTimeOffset itemDate = item.PubDate; //if (itemDate == DateTimeOffset.MinValue) itemDate = item.PublishDate; System.Diagnostics.Debug.WriteLine(String.Format("Item Published at: {0} - (last update at: {1})", itemDate, this.feedLastUpdated)); if (itemDate > mostRecentItem) { mostRecentItem = itemDate; } if (itemDate > this.feedLastUpdated) { newitems.Add(item); item.SourceFeed = feed; } } FeedUpdatedEventArgs args = new FeedUpdatedEventArgs(newitems); FeedUpdated(this, args); } this.feedLastUpdated = mostRecentItem; // feed.LastUpdatedTime is not always set =( }