public async Task <bool> RefreshAsync(RssFeed rssFeed, CancellationToken cancellationToken)
        {
            using var client   = _clientFactory.CreateClient(RefreshRssFeedsServiceProviderExtensions.DefaultHttpClient);
            using var response = await client.GetAsync(rssFeed.Uri, cancellationToken);

            if (!response.IsSuccessStatusCode)
            {
                _logger.LogWarning($"Could not refresh feed {rssFeed.Id} from {rssFeed.Uri}: response status: {response.StatusCode}, content: {await response.Content?.ReadAsStringAsync()}");
                return(false);
            }

            _logger.LogInformation($"Successfully download feed from {rssFeed.Uri}");
            FeedParseResult parseResult;

            if (!((parseResult = await _feedParser.ParseAsync(await response.Content.ReadAsStreamAsync(), cancellationToken))?.IsValid ?? false))
            {
                _logger.LogWarning($"Could not parse feed response from {rssFeed.Uri} - content: {await response.Content.ReadAsStringAsync()}");
                return(false);
            }

            _logger.LogDebug($"Feed {rssFeed.Uri} was last updated {parseResult.Feed.LastUpdated} - our version was updated: {rssFeed.LastUpdated}");
            if (!rssFeed.LastUpdated.HasValue || parseResult.Feed.LastUpdated > rssFeed.LastUpdated)
            {
                _logger.LogTrace($"Feed {rssFeed.Uri} has new items...updating articles");
                await UpdateFeedItemsAsync(rssFeed, parseResult);

                rssFeed.LastUpdated = parseResult.Feed.LastUpdated;
                rssFeed.Link        = parseResult.Feed.Link;
                return(true);
            }

            return(false);
        }
Exemple #2
0
        public async Task <ActionResult <object> > Read([Required] string uri)
        {
            if (!Uri.TryCreate(HttpUtility.UrlDecode(uri), UriKind.Absolute, out var feedUri))
            {
                _logger.LogWarning($"Could not parse uri {uri}");
                return(BadRequest());
            }

            using var client   = _clientFactory.CreateClient(RefreshRssFeedsServiceProviderExtensions.DefaultHttpClient);
            using var response = await client.GetAsync(feedUri.ToString(), CancellationToken.None);

            if (!response.IsSuccessStatusCode)
            {
                _logger.LogWarning($"Could not load feed {feedUri}: response status: {response.StatusCode}, content: {await response.Content?.ReadAsStringAsync()}");
                return(BadRequest());
            }

            _logger.LogInformation($"Successfully download feed from {feedUri}");
            FeedParseResult parseResult;

            if (!((parseResult = await _feedParser.ParseAsync(await response.Content.ReadAsStreamAsync(), CancellationToken.None))?.IsValid ?? false))
            {
                _logger.LogWarning($"Could not parse feed response from {feedUri} - content: {await response.Content.ReadAsStringAsync()}");
                return(BadRequest());
            }

            return(new { Title = parseResult.FeedTitle, parseResult.Feed.LastUpdated, parseResult.Feed.Link });
        }