// TODO make function effective with removing old items public IEnumerable <AddNewArticleEvent> GetFeedContent(string url) { XmlReader reader = XmlReader.Create(url); SyndicationFeed feed = SyndicationFeed.Load(reader); reader.Close(); ConcurrentBag <AddNewArticleEvent> articles = new ConcurrentBag <AddNewArticleEvent>(); //TODO check for paralell execution Parallel.ForEach(feed.Items, (currentArticle) => { string newsUrl = currentArticle.Links.FirstOrDefault().Uri.ToString(); AddNewArticleEvent result = new AddNewArticleEvent() { Description = currentArticle.Summary.Text, Title = currentArticle.Title.Text, Link = currentArticle.Links.FirstOrDefault().Uri.ToString(), PublishDate = currentArticle.PublishDate.DateTime, // TODO Add picture Picture = _metaDataReader.GetWebsiteMetadata(newsUrl).ImageUrl }; articles.Add(result); }); return(articles.OrderByDescending(e => e.PublishDate)); }
public async Task AddNewArticleToData(AddNewArticleEvent newItem) { HttpResponseMessage response = await _client.PostAsync("article", new StringContent(JsonConvert.SerializeObject(newItem), Encoding.UTF8, "application/json")); string content = await response.Content.ReadAsStringAsync(); if (!response.IsSuccessStatusCode) { throw new Exception(content); } }
private async Task ProcessOneResource(Resource actualItem) { Lastsynchronizedresource lastSource = _databasecontext.Lastsynchronizedresource.FirstOrDefault(e => e.ResourceId == actualItem.Id); int i = 0; IEnumerable <AddNewArticleEvent> feedContent = _reader.GetFeedContent(actualItem.Url); if (lastSource == null) { lastSource = new Lastsynchronizedresource() { ResourceId = actualItem.Id, Title = String.Empty, Description = String.Empty }; _databasecontext.Add(lastSource); _databasecontext.SaveChanges(); // New item. Not Synchornized yet. foreach (var item in feedContent) { try { item.FeedId = actualItem.FeedId.Value; // Publish the new article to the hub // TODO handle failure _eventHub.Publish(item); lastSource.Title = item.Title; lastSource.Description = item.Description; _databasecontext.Update(lastSource); _databasecontext.SaveChanges(); // TODO add integration event and pubish to hub } catch (Exception e) { _logger.LogError("Failed to add new article"); _logger.LogError(e.Message); } } } else { // TODO Add update to description and grouping AddNewArticleEvent item = feedContent.ElementAt(i); while (!item.Title.Equals(lastSource.Title, StringComparison.OrdinalIgnoreCase) && !item.Description.Equals(lastSource.Description, StringComparison.OrdinalIgnoreCase)) { item = feedContent.ElementAt(i); try { item.FeedId = actualItem.FeedId.Value; _eventHub.Publish(item); lastSource.Title = item.Title; lastSource.Description = item.Description; _databasecontext.Update(lastSource); _databasecontext.SaveChanges(); } catch (Exception e) { _logger.LogError("Failed to add new article"); _logger.LogError(e.Message); } i++; } } }