public ActionResult Create(AddFeedModel feed) { if (ModelState.IsValid) { if (isValidFeed(feed.Url)) { SyndicationFeed f = SyndicationFeed.Load(System.Xml.XmlReader.Create(feed.Url)); Feed Coming = new Feed() { Url = feed.Url, Name = f.Title.Text, Lastupdate = new DateTime(2000, 1, 1), Description = f.Description.Text, Website = f.Links.Count > 0 ? f.Links[0].Uri.OriginalString : "", CategoryID = feed.CategoryID, UserId = userId }; _db.Feeds.Add(Coming); _db.SaveChanges(); //On recupere les derniers articles pour le flux int feedMaxEvent = (int)_db.UserProfiles.Find(WebSecurity.CurrentUserId).feedMaxEvent; getLastEvent(Coming, feedMaxEvent); return RedirectToAction("Index"); } else { ModelState.AddModelError("", "Error, the feed cannot be read. Sorry"); } } ViewBag.CategoryList = new SelectList(_db.Categories, "CategoryID", "Name"); return View(feed); }
private void getLastEvent(Feed f, int maxItem,int? uId = null) { var Event = new List<Event>(); string content = ""; string creator = ""; int itemCounter = 0; SyndicationFeed r = SyndicationFeed.Load(System.Xml.XmlReader.Create(f.Url)); SyndicationItem preCheck = r.Items.First(); IEnumerable<SyndicationItem> items = null; //Sometimes the feeds don't have a publishate so we take the lastUpdate if (preCheck.PublishDate.DateTime == DateTime.MinValue) { items = r.Items.Where(d => d.LastUpdatedTime > f.Lastupdate).Take(maxItem); } else { items = r.Items.Where(d => d.PublishDate.DateTime > f.Lastupdate).Take(maxItem); } foreach (SyndicationItem item in items) { if (item.Authors.Count > 0) { creator = item.Authors[0].Name; } foreach (SyndicationElementExtension ext in item.ElementExtensions) { if (ext.GetObject<XElement>().Name.LocalName == "encoded") content = ext.GetObject<XElement>().Value; if (ext.GetObject<XElement>().Name.LocalName == "creator") creator = ext.GetObject<XElement>().Value; } Event.Add(new Event { FeedID = f.FeedID, Title = item.Title.Text, Link = item.Links.First().Uri.IsAbsoluteUri ? item.Links.First().Uri.AbsoluteUri : item.Links.First().Uri.OriginalString, Favorite = false, Unread = null, Creator = creator, Content = content, Description = item.Summary.Text, Guid = item.Id, Pubdate = item.PublishDate.DateTime == DateTime.MinValue ? item.LastUpdatedTime.DateTime : item.PublishDate.DateTime }); itemCounter++; } //if It cannot read the lastUpdateDate frome the date with set it to now f.Lastupdate = r.LastUpdatedTime.DateTime == DateTime.MinValue ? DateTime.Now : r.LastUpdatedTime.DateTime; Event.ForEach(s => _db.Events.Add(s)); uId = uId == null ? userId : uId; //Check if is a cron update UserProfile u = _db.UserProfiles.Find(uId); u.eventDownloaded += itemCounter; _db.SaveChanges(); }
private void flushCache(Feed feedInfo, int feedMaxEvent) { //Check if the cache > limit defined by the user if (feedInfo.Events.Where(f => f.Favorite == false && f.Unread != true).Count() > feedMaxEvent) { IEnumerable<Event> Ev = feedInfo.Events .Where(f => f.Favorite == false && f.Unread != true) .OrderBy(i => i.EventID) .TakeWhile(c => feedInfo.Events.Count() > feedMaxEvent); foreach (Event e in Ev) { _db.Events.Remove(e); } _db.SaveChanges(); } }