示例#1
0
        /// <summary>
        /// Initializes <see cref="RssMixxxer"/>'s background maintenance.
        /// </summary>
        public void ForMyNeeds()
        {
            lock (_syncRoot)
            {
                if (_isRunning)
                {
                    _log.Warn("Failed attempt to start mixing feeds - another instance is already running.");
                    return;
                }

                _backgroundOperation = new BackgroundOperation(() =>
                {
                    var config      = _configProvider.ProvideConfig();
                    var sourceFeeds = config.SourceFeeds;

                    _log.Debug("Synchronizing {0} feeds", sourceFeeds.Length);

                    process_feeds(sourceFeeds);
                }, TimeSpan.FromSeconds(_configProvider.ProvideConfig().SyncInterval_Seconds));

                _backgroundOperation.Start();

                _isRunning = true;
            }
        }
示例#2
0
        public FeedComposer(ILocalFeedsProvider localFeedsProvider, IFeedMixer feedMixer, IFeedAggregatorConfigProvider configProvider)
        {
            _localFeedsProvider = localFeedsProvider;
            _feedMixer          = feedMixer;

            _config = configProvider.ProvideConfig();
        }
示例#3
0
        public RemoteContentResponse ReadRemoteSource(LocalFeedInfo feedInfo)
        {
            _log.Debug("Trying to read remote source '{0}' (etag: {1}, last fetch: {2})", feedInfo.Url, feedInfo.Etag, feedInfo.LastFetch);

            try
            {
                if (feedInfo.LastFetch.HasValue && _configProvider.ProvideConfig().PrefetchHeadRequest)
                {
                    var head_request = _httpRequestFactory.CreateRequest(feedInfo.Url);
                    head_request.Method = "HEAD";
                    using (var head_response = (HttpWebResponse)head_request.GetResponseEx())
                    {
                        DateTime last_modified = head_response.LastModified.ToUniversalTime();
                        DateTime last_fetch    = feedInfo.LastFetch.Value.ToUniversalTime();
                        if (last_modified < last_fetch)
                        {
                            _log.Debug("HEAD request indicates that there is no new content available (last_modified={0}, last_fetch={1}), returning NotModified.", last_modified, last_fetch);
                            return(RemoteContentResponse.NotModified(head_response.StatusCode));
                        }
                    }
                }

                var get_request = _httpRequestFactory.CreateRequest(feedInfo.Url);
                if (feedInfo.LastFetch != null)
                {
                    get_request.IfModifiedSince = feedInfo.LastFetch.Value;
                }

                if (string.IsNullOrWhiteSpace(feedInfo.Etag) == false)
                {
                    get_request.Headers[HttpRequestHeader.IfNoneMatch] = feedInfo.Etag;
                }

                using (var response = (HttpWebResponse)get_request.GetResponseEx())
                {
                    if (response.StatusCode == HttpStatusCode.OK)
                    {
                        string etag = response.Headers[HttpResponseHeader.ETag];

                        Stream responseStream = response.GetResponseStream();
                        using (var streamReader = new StreamReader(responseStream))
                        {
                            string responseText = streamReader.ReadToEnd();

                            responseText = _contentPreProcessor.PreProcess(responseText);

                            using (var stringReader = new StringReader(responseText))
                            {
                                using (var reader = XmlReader.Create(stringReader))
                                {
                                    var feed = SyndicationFeed.Load(reader);

                                    _log.Info("Returning new content for remote source '{0}' with {1} items", feedInfo.Url, feed.Items.Count());

                                    return(new RemoteContentResponse(true, etag, feed, response.StatusCode));
                                }
                            }
                        }
                    }
                    else
                    {
                        string message = string.Format("No new content for remote source '{0}', response status is {1}", feedInfo.Url, response.StatusCode);

                        if (response.StatusCode == HttpStatusCode.NotModified)
                        {
                            _log.Debug(message);
                        }
                        else
                        {
                            _log.Warn(message);
                        }

                        return(RemoteContentResponse.NotModified(response.StatusCode));
                    }
                }
            }
            catch (Exception exc)
            {
                _log.ErrorException(string.Format("Failed to read remote source '{0}'", feedInfo.Url), exc);

                throw new CannotReachRemoteSourceException(feedInfo.Url, exc);
            }
        }