コード例 #1
0
        private async Task LoadPageInternal(LoadPageEvent uri)
        {
            const string logMessage = "Recieve URL to load:";

            log?.WriteLine(logMessage + uri.Url);

            if (await CheckCache(uri, logMessage.Length))
            {
                return;
            }

            HttpClient          client = new HttpClient();
            HttpResponseMessage result;

            if (uri.Method == HttpMethod.Get)
            {
                result = await client.GetAsync(uri.Url);
            }
            else
            {
                result = await client.PostAsync(uri.Url, new FormUrlEncodedContent(uri.FormData.ToDictionary()));
            }
            result.EnsureSuccessStatusCode();
            var content = await result.Content.ReadAsStringAsync();

            lastLoaded = DateTime.Now;
            log?.WriteLine("URL loaded: ".PadRight(logMessage.Length) + uri.Url);
            queue.Publish(new ContentLoadedEvent(uri, content, lastLoaded));
        }
コード例 #2
0
        private async void LoadPage(LoadPageEvent uri)
        {
            await syncObject.WaitAsync();

            try {
                await Throttle();
                await LoadPageInternal(uri);
            }
            catch (Exception ex) {
                log?.WriteError("Error during load: " + uri.Url, ex);
            }
            finally {
                syncObject.Release();
            }
        }
コード例 #3
0
ファイル: Cache.cs プロジェクト: podkolzzzin/Movien.Crawler
        public async Task <ContentLoadedEvent?> Retrieve(LoadPageEvent evt)
        {
            var method     = evt.Method.ToString();
            var candidates = await repository.Find(f => f.Url.Equals(evt.Url) && f.HttpMethod == method);

            var parameters = evt.FormData.ToDictionary();
            var headers    = new Dictionary <string, string>();

            var page = candidates.FirstOrDefault(f => IsCandidateSuits(f, parameters, headers));

            if (page == null)
            {
                return(null);
            }

            return(new ContentLoadedEvent(evt, page.Content, page.LoadedAt));
        }
コード例 #4
0
        private async Task <bool> CheckCache(LoadPageEvent request, int logMessageLength)
        {
            if (cache != null)
            {
                var cachedItem = await cache?.Retrieve(request);

                if (cachedItem != null)
                {
                    log?.WriteLine("Item loaded from cache. HTTPRequest skipped".PadRight(logMessageLength));
                    queue.Publish(cachedItem.Value);
                    return(true);
                }
                else
                {
                    log?.WriteLine("Cache does not contain suitable item".PadRight(logMessageLength));
                    return(false);
                }
            }
            return(false);
        }