public static List <RssItem> ProcessRSS(string feed) { if (System.Web.HttpContext.Current.Cache[CacheName] != null) { return((List <RssItem>)System.Web.HttpContext.Current.Cache[CacheName]); } List <RssItem> feedList = new List <RssItem>(); string feedURL = feed; StringBuilder rssText = new StringBuilder(); HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(feed); myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-GB; rv:1.8.1.4) Gecko/20070515 Firefox/2"; WebResponse myResponse = myRequest.GetResponse(); Stream rssStream = myResponse.GetResponseStream(); XmlDocument rssDoc = new XmlDocument(); try { rssDoc.Load(rssStream); } catch { RssItem dummyFeed = new RssItem() { Link = "", Description = "Problem connecting to facebook, please check back later!", Title = "Error" }; feedList.Add(dummyFeed); return(feedList); } XmlNodeList rssItems = rssDoc.SelectNodes("rss/channel/item"); for (int i = 0; i < rssItems.Count; i++) { // article // string heading; string description; string hyperlink; DateTime date = UKTime.Now.AddDays(-1); XmlNode rssDetail; rssDetail = rssItems.Item(i).SelectSingleNode("title"); heading = rssDetail.InnerText; rssDetail = rssItems.Item(i).SelectSingleNode("description"); description = rssDetail.InnerText; rssDetail = rssItems.Item(i).SelectSingleNode("link"); hyperlink = rssDetail.InnerText; rssDetail = rssItems.Item(i).SelectSingleNode("pubDate"); try { //Date in the format: "Wed, 07 Mar 2012 14:44:00" date = DateTime.Parse(rssDetail.InnerText); } catch { } RssItem newFeed = new RssItem(heading, description, date, hyperlink, RssItem.FeedTypes.Facebook); feedList.Add(newFeed); } int expirationDuration = 6; if (!feedList.Any()) { expirationDuration = 1; } System.Web.HttpContext.Current.Cache.Add(CacheName, feedList, null, DateTime.UtcNow.AddHours(expirationDuration), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Default, null); return(feedList); }
public void AddItem(RssItem item) { rssList.Add(item); }
/// <summary> /// Returns an IEnumerable of RssItem based on a given uri, which /// can be an internet URL or a local filesystem/network path /// </summary> /// <param name="uri">URI of RSS Feed</param> /// <param name="timeout">Timeout, after which /// data will be refreshed</param> /// <returns>IEnumerable of RssItem</returns> public IEnumerable <RssItem> Read(string uri, double timeout) { // attempt to fetch from cache CachedFeed feed = feeds.FirstOrDefault(p => p.Uri == uri); if (feed != null && feed.Expiry > DateTime.UtcNow) { return(feed.RssItems); } string url = uri; using (XmlTextReader reader = new XmlTextReader(url)) { XmlDocument doc = new XmlDocument(); for (int i = 0; i < 5; i++) { try { doc.Load(reader); break; } catch (Exception ex) { if (i == 4) { throw; } } } List <RssItem> rssItems = new List <RssItem>(); if (doc["rss"] == null) { rssItems.Add(new RssItem { Description = "This is unavailable right now... sorry!" }); return(rssItems); } XmlNode channel = doc["rss"]["channel"]; foreach (XmlNode n in channel.ChildNodes) { if (n.Name == "item") { RssItem rssItem = new RssItem { Description = n["description"].InnerText, Title = n["title"].InnerText, Date = DateTime.Parse(n["pubDate"].InnerText), Link = n["link"].InnerText, }; rssItems.Add(rssItem); } } // store this item in the cache if (feed == null) { feed = new CachedFeed(); feeds.Add(feed); } feed.Expiry = DateTime.UtcNow.AddHours(timeout); feed.RssItems = rssItems; feed.Uri = uri; HttpContext.Current.Cache[CACHEID] = feeds; return(rssItems); } }
public static List <RssItem> ProcessRSS(string feed) { List <RssItem> feedList = new List <RssItem>(); string feedURL = feed; StringBuilder rssText = new StringBuilder(); HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(feed); myRequest.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-GB; rv:1.8.1.4) Gecko/20070515 Firefox/2"; WebResponse myResponse = myRequest.GetResponse(); Stream rssStream = myResponse.GetResponseStream(); XmlDocument rssDoc = new XmlDocument(); string twitterUsername = feed.Substring(feed.IndexOf("screen_name=") + "screen_name=".Length); try { rssDoc.Load(rssStream); } catch { RssItem dummyFeed = new RssItem() { Link = "https://twitter.com/#!/" + twitterUsername, Description = "Problem connecting to Twitter, please check back later!", Title = "Error" }; feedList.Add(dummyFeed); return(feedList); } XmlNodeList rssItems = rssDoc.SelectNodes("statuses/status"); for (int i = 0; i < rssItems.Count; i++) { // article // string heading; string description; string hyperlink; DateTime date = UKTime.Now.AddDays(-1); XmlNode rssDetail; rssDetail = rssItems.Item(i).SelectSingleNode("id"); heading = rssDetail.InnerText; rssDetail = rssItems.Item(i).SelectSingleNode("text"); description = rssDetail.InnerText; //rssDetail = rssItems.Item(i).SelectSingleNode("link"); hyperlink = "https://twitter.com/#!/" + twitterUsername; rssDetail = rssItems.Item(i).SelectSingleNode("created_at"); try { //Date in the format: "Wed Mar 07 16:51:31 +0000 2012" date = ParseTwitterDate(rssDetail.InnerText); } catch { } RssItem newFeed = null; newFeed = new RssItem(heading, Twitter.Format(description), date, hyperlink, RssItem.FeedTypes.Twitter); feedList.Add(newFeed); } return(feedList); }