public async Task GetNews() { var newsResponse = await _sportsRuApiService .GetNewsAsync(NewsType.HomePage, NewsPriority.Main, NewsContentOrigin.Mixed, 10).ConfigureAwait(false); Assert.True(newsResponse.IsSuccess, newsResponse.ErrorMessage); Assert.NotNull(newsResponse.Content); Assert.NotEmpty(newsResponse.Content); foreach (var newsArticle in newsResponse.Content) { Assert.NotNull(newsArticle.Title); Assert.NotNull(newsArticle.DesktopUrl); Assert.NotEqual(0, newsArticle.Id); Assert.NotNull(newsArticle.Published); Assert.NotEqual(0, newsArticle.Published.Timestamp); if (newsArticle.ContentOption != null) { Assert.NotEmpty(newsArticle.ContentOption.Name); } Assert.NotNull(newsArticle.Section); Assert.NotNull(newsArticle.Section.WebName); Assert.NotNull(newsArticle.Main); } WritePrettyJson(newsResponse.Content); }
public async Task SyncNewsAsync() { var newsResponse = await _sportsRuApiService.GetNewsAsync(NewsType.HomePage, NewsPriority.Main, NewsContentOrigin.Mixed, 100).ConfigureAwait(false); var hotContent = await _sportsRuApiService.GetHotContentAsync().ConfigureAwait(false); IEnumerable <int> hotNewsIds = null; if (hotContent.IsSuccess) { hotNewsIds = hotContent.Content.News; } if (newsResponse.IsSuccess) { foreach (var newsArticle in newsResponse.Content) { if (newsArticle.BodyIsEmpty || newsArticle.ContentOption?.Name == "special" || !SportsRuHelper.IsInternalUrl(newsArticle.DesktopUrl)) //usually this is not a news article but some promotion { continue; } string idString = newsArticle.Id.ToString(CultureInfo.InvariantCulture); var existingArticle = _sportsContext.NewsArticles.FirstOrDefault(x => x.ExternalId == idString); if (existingArticle == null) { var newArticle = new NewsArticle() { ExternalId = idString }; Map(newsArticle, newArticle, hotNewsIds); _sportsContext.NewsArticles.Add(newArticle); } else { Map(newsArticle, existingArticle, hotNewsIds); _sportsContext.NewsArticles.Update(existingArticle); } } _sportsContext.SaveChanges(); } }