public void SaveFeed(Feed feed) { if (feed.FeedID == 0) { _feedTable.InsertOnSubmit(feed); } _context.SubmitChanges(); }
public List<FeedViewModel> BuildFeedsViewModelList(List<Feed> feeds, Feed selectedFeed) { List<FeedViewModel> models = new List<FeedViewModel>(); foreach (var f in feeds) { var feedViewModel = new FeedViewModel { Feed = f }; feedViewModel.UnreadCount = _repository.Items.Count(x => x.FeedID == f.FeedID && x.IsRead == false); if (selectedFeed != null && f.FeedID == selectedFeed.FeedID) { feedViewModel.Selected = true; } models.Add(feedViewModel); } return models; }
public void DeleteFeed(Feed feed) { _feedTable.DeleteOnSubmit(feed); _context.SubmitChanges(); }
public ActionResult Subscribe(string url) { if (url.Trim() == string.Empty) { TempData["Error"] = "URL is required to add a feed"; } else { try { // Check for duplicates var feed = _repository.Feeds.FirstOrDefault(x => x.URL == url); if (feed == null) { feed = new Feed { FeedID = 0, URL = url }; } _services.FillFeed(feed); _repository.SaveFeed(feed); TempData["Message"] = feed.DisplayName + " added"; } catch (Exception ex) { TempData["Error"] = "Error: " + ex.Message.ToString(); } } return RedirectToAction("Index"); }
public void Fetch(Feed feed) { var source = GetFeed(HttpUtility.UrlDecode(feed.URL)); var syndicationItems = source.Items.ToList(); var items = new List<Item>(); foreach (var i in syndicationItems) { // An article link is required... this is how we uniquely identify articles. var link = i.Links.FirstOrDefault(x => x.RelationshipType == "alternate"); if (link == null) { continue; } string itemUrl = (link.Uri.ToString()); var item = new Item { ItemID = 0 }; item.FeedID = feed.FeedID; item.URL = itemUrl; item.IsStarred = false; item.IsRead = false; item.Title = (i.Title != null) ? i.Title.Text : "No title"; if (IsValidSqlDateTime(i.PublishDate)) { item.PublishDate = i.PublishDate.DateTime; } else { item.PublishDate = DateTime.Now; } // The content may be encoded. Go find the encoded content XML node and get the content. StringBuilder sb = new StringBuilder(); foreach (SyndicationElementExtension extension in i.ElementExtensions) { XElement e = extension.GetObject<XElement>(); if (e.Name.LocalName == "encoded" && e.Name.Namespace.ToString().Contains("content")) { sb.Append(e.Value); } } item.Content = sb.ToString(); // Otherwise use the summary if (item.Content == string.Empty && i.Summary != null) { item.Content = i.Summary.Text; } item.Content = this.ScrubScripts(item.Content); items.Add(item); } // Order the list and save it to the database foreach (var i in items.OrderBy(x => x.PublishDate)) { // Check if we've already saved this item if (!_repository.Items.Any(x => x.URL == i.URL)) { i.FetchDate = DateTime.Now; // Don't save advertisements if (!i.Title.ToLower().Contains("sponsored post")) { _repository.SaveItem(i); } } } feed.LastRefresh = DateTime.Now; _repository.SaveFeed(feed); }
public void FillFeed(Feed feed) { SyndicationFeed source = GetFeed(feed.URL); if (source == null) { throw new Exception("Not a valid Atom or RSS feed."); } var link = source.Links.FirstOrDefault(x => x.RelationshipType == "alternate"); feed.BlogURL = link.GetAbsoluteUri().AbsoluteUri; feed.DisplayName = source.Title.Text; if (feed.DisplayName == string.Empty) { feed.DisplayName = link.GetAbsoluteUri().Host; } // Load the favicon byte[] bytes = new byte[0]; var imageAddress = "http://" + link.Uri.Host + "/favicon.ico"; try { WebClient client = new WebClient(); MemoryStream stream = new MemoryStream(client.DownloadData(imageAddress)); feed.Favicon = stream.ToArray(); } catch { // We'll let it pass if we can't find the image } }
public void Fetch(Feed feed) { var source = GetFeed(HttpUtility.UrlDecode(feed.URL)); var syndicationItems = source.Items.ToList(); var items = new List <Item>(); foreach (var i in syndicationItems) { // An article link is required... this is how we uniquely identify articles. var link = i.Links.FirstOrDefault(x => x.RelationshipType == "alternate"); if (link == null) { continue; } string itemUrl = (link.Uri.ToString()); var item = new Item { ItemID = 0 }; item.FeedID = feed.FeedID; item.URL = itemUrl; item.IsStarred = false; item.IsRead = false; item.Title = (i.Title != null) ? i.Title.Text : "No title"; if (IsValidSqlDateTime(i.PublishDate)) { item.PublishDate = i.PublishDate.DateTime; } else { item.PublishDate = DateTime.Now; } // The content may be encoded. Go find the encoded content XML node and get the content. StringBuilder sb = new StringBuilder(); foreach (SyndicationElementExtension extension in i.ElementExtensions) { XElement e = extension.GetObject <XElement>(); if (e.Name.LocalName == "encoded" && e.Name.Namespace.ToString().Contains("content")) { sb.Append(e.Value); } } item.Content = sb.ToString(); // Otherwise use the summary if (item.Content == string.Empty && i.Summary != null) { item.Content = i.Summary.Text; } item.Content = this.ScrubScripts(item.Content); items.Add(item); } // Order the list and save it to the database foreach (var i in items.OrderBy(x => x.PublishDate)) { // Check if we've already saved this item if (!_repository.Items.Any(x => x.URL == i.URL)) { i.FetchDate = DateTime.Now; // Don't save advertisements if (!i.Title.ToLower().Contains("sponsored post")) { _repository.SaveItem(i); } } } feed.LastRefresh = DateTime.Now; _repository.SaveFeed(feed); }