private Task<bool> RefreshAsync(Guid id, bool force = false) { var task = new TaskCompletionSource<bool>(); var feed = _context.Feeds.Include(f => f.Items).Single(f => f.Id == id); if (!force && feed.LastUpdateDateTime > GetExpiryDate(feed)) { task.SetResult(true); return task.Task; } var rssFeed = new RssFeed(new Uri(feed.FeedUrl)); // async callback rssFeed.FeedLoaded += (sender, args) => { foreach (var rssItem in rssFeed.Items) { // exist? var item = feed.Items.FirstOrDefault(i => i.LinkUrl == rssItem.Id); // update if (item != null) { item.Name = rssItem.Title; item.Raw = rssItem.Raw; item.Content = HtmlCleanerHelper.Clean(rssItem.Raw); item.Snippet = HtmlCleanerHelper.GetSnippet(rssItem.Raw, 200); item.PublishedDateTime = GetPublishedDateTime(rssItem.PublishedDateTime); continue; } Debug.WriteLine("found new items"); // insert feed.Items.Add(new Item { Id = Guid.NewGuid(), LinkUrl = rssItem.Id, Name = rssItem.Title, Raw = rssItem.Raw, Content = HtmlCleanerHelper.Clean(rssItem.Raw), Snippet = HtmlCleanerHelper.GetSnippet(rssItem.Raw, 200), PublishedDateTime = GetPublishedDateTime(rssItem.PublishedDateTime) }); } feed.LastUpdateDateTime = DateTime.UtcNow; task.SetResult(true); }; // begin async load rssFeed.GetItemsFromWeb(); return task.Task; }
public Guid Add(Uri feedUrl, Guid? folderId) { var feed = _context.Feeds.Create(); feed.Id = Guid.NewGuid(); feed.FeedUrl = feedUrl.ToString(); feed.FolderId = folderId; feed.UpdateFrequency = 3; feed.UpdatePeriod = "Daily"; var rssFeed = new Manager.Feed(feedUrl); RefreshResetEvent.Reset(); rssFeed.FeedLoaded += (sender, args) => { feed.Name = rssFeed.Title ?? "can't find title"; feed.HtmlUrl = rssFeed.HtmlUri.ToString(); // oh man this is dodgy // ah feel a rewrite a coming on _context.Feeds.Add(feed); _context.SaveChanges(); _context.Feeds.Add(feed); RefreshResetEvent.Set(); }; rssFeed.GetItemsFromWeb(); // TODO: consider async/await, but it's abit tricky without modifying the interface signature RefreshResetEvent.WaitOne(TimeSpan.FromSeconds(20)); return feed.Id; }