Esempio n. 1
0
        public IHtmlParser CreateParserFromSourceUrl(string url)
        {
            var htmlParser = default(IHtmlParser);

            //if the url has already been downloaded once we store it in memory for this instance of the default web services.
            //helps avoid multiple downloads of a file that likely hasn't changed in this short time span.
            if (!_cachedUrls.ContainsKey(url))
            {
                try
                {
                    string pageSource = _pageDownloader.DownloadPageSource(new Uri(url), _webClientProvider);
                    _cachedUrls[url] = new CacheableUrlResponse
                    {
                        PageSource = pageSource
                    };
                    htmlParser = _htmlParserProvider.Create(_cachedUrls[url].PageSource);
                }
                catch (PageNotFoundException ex)
                {
                    var cacheableResponse = new CacheableUrlResponse
                    {
                        PageSource        = string.Empty,
                        ResponseException = ex
                    };
                    _cachedUrls[url] = cacheableResponse;
                    throw _cachedUrls[url].ResponseException;
                }
            }
            else
            {
                var cachedValue = _cachedUrls[url];
                if (cachedValue.ResponseException != null)
                {
                    throw cachedValue.ResponseException;
                }
                else
                {
                    htmlParser = _htmlParserProvider.Create(cachedValue.PageSource);
                }
            }

            return(htmlParser);
        }