public IEnumerable<NewsInfo> ChurchNews() { var feedurl = DbUtil.Db.Setting("ChurchFeedUrl", ""); var feed = DbUtil.Db.RssFeeds.FirstOrDefault(r => r.Url == feedurl); HttpWebRequest req = null; try { req = WebRequest.Create(feedurl) as HttpWebRequest; } catch { } if (feed != null) { if (feed.LastModified.HasValue) { req.IfModifiedSince = feed.LastModified.Value; req.Headers.Add("If-None-Match", feed.ETag); } } else { feed = new RssFeed(); DbUtil.Db.RssFeeds.InsertOnSubmit(feed); feed.Url = feedurl; } if (req != null) { try { var resp = req.GetResponse() as HttpWebResponse; feed.LastModified = resp.LastModified; feed.ETag = resp.Headers["ETag"]; var sr = new StreamReader(resp.GetResponseStream()); feed.Data = sr.ReadToEnd(); sr.Close(); DbUtil.Db.SubmitChanges(); } catch (WebException) { } if (feed.Data != null) { try { var reader = XmlReader.Create(new StringReader(feed.Data)); var f = SyndicationFeed.Load(reader); var posts = from item in f.Items let a = item.Authors.FirstOrDefault() let au = a == null ? "" : a.Name select new NewsInfo { Title = item.Title.Text, Published = item.PublishDate.DateTime, Url = item.Links.Single(i => i.RelationshipType == "alternate").GetAbsoluteUri().AbsoluteUri }; return posts; } catch { return new NewsInfo[] { }; } } } return new NewsInfo[] { }; }
private static IEnumerable<NewsInfo> GetNewsInfo(string feedurl) { var feed = DbUtil.Db.RssFeeds.FirstOrDefault(r => r.Url == feedurl); if (!feedurl.HasValue()) return new List<NewsInfo>(); var req = WebRequest.Create(feedurl) as HttpWebRequest; if (feed != null && req != null) { if (feed.LastModified.HasValue) req.IfModifiedSince = feed.LastModified.Value; } else { feed = new RssFeed(); DbUtil.Db.RssFeeds.InsertOnSubmit(feed); feed.Url = feedurl; } if (req == null) return new List<NewsInfo>(); try { var resp = req.GetHttpResponse(); if (resp == null) return new List<NewsInfo>(); if (resp.StatusCode != HttpStatusCode.NotModified) { feed.LastModified = resp.LastModified; var rs = resp.GetResponseStream(); if (rs == null) return new List<NewsInfo>(); var sr = new StreamReader(rs); feed.Data = sr.ReadToEnd(); sr.Close(); DbUtil.Db.SubmitChanges(); } } catch { return new List<NewsInfo>(); } if (feed.Data == null) return new List<NewsInfo>(); try { var reader = XmlReader.Create(new StringReader(feed.Data)); var f = SyndicationFeed.Load(reader); if (f == null) return new List<NewsInfo>(); var posts = from item in f.Items select new NewsInfo { Title = item.Title.Text, Published = item.PublishDate.DateTime, Url = item.Links.Single(i => i.RelationshipType == "alternate")?.GetAbsoluteUri()?.AbsoluteUri }; return posts; } catch { return new List<NewsInfo>(); } }