Ejemplo n.º 1
0
        private async void FetchResults(IScrapable <IMetacriticData> item)
        {
            List <UrlResponsePair> urlResponsePairs = item.Scrape();
            var tasks = urlResponsePairs.Where(p => !string.IsNullOrEmpty(p.Response)).
                        Select(pairs => Task.Run(() => item.Parse(pairs)));

            RequestTrackerItem tItem;

            lock (m_requestTrackerLock)
            {
                tItem = m_requestTracker.FirstOrDefault(i => i.RequestId == item.RequestId);
                if (!EqualityComparer <RequestTrackerItem> .Default.Equals(tItem, default(RequestTrackerItem)))
                {
                    m_requestTracker.Remove(tItem);
                }
            }

            if (!EqualityComparer <RequestTrackerItem> .Default.Equals(tItem, default(RequestTrackerItem)))
            {
                try
                {
                    IMetacriticData[] htmlResp = await Task.WhenAll(tasks);

                    if (htmlResp != null && htmlResp.Length > 0)
                    {
                        PublishResult(tItem.RequestId, htmlResp);
                    }
                    else
                    {
                        throw new Errors.EmptyResponseException("No matching data found.");
                    }
                }
                catch (EmptyResponseException)
                {
                    Logger.Info("No response received");
                    Error[] error = new Error[1];
                    Error   err   = new Error("No matching item found!");
                    error[0] = err;
                    PublishResult(tItem.RequestId, error);
                    RequestTrackerItem reqItem = m_requestTracker.FirstOrDefault(r => r.RequestId == tItem.RequestId);
                    m_requestTracker.Remove(reqItem);
                }
                catch (Exception)
                {
                    var exceptions = tasks.Where(t => t.Exception != null).Select(t => t.Exception);
                    Logger.Error("Encountered exception while parsing. Exceptions: ");
                    foreach (Exception ex in exceptions)
                    {
                        Logger.Error("-- {0}", ex.ToString());
                    }
                }
            }
            else
            {
                Logger.Warn("Item not found in request tracker");
            }
        }
Ejemplo n.º 2
0
        private void ProcessAutoSearch(RequestItem request)
        {
            Task <bool> task = request.AutoSearch();

            try
            {
                if (task.Result)
                {
                    if (request.FilterValidUrls())
                    {
                        request.RetrieveImagePath();
                        m_dataFetchQueue.Enqueue(request);
                    }
                    else if (request.ForceUrl())
                    {
                        Logger.Info("No valid urls from autosearch. Forcing the website url");
                        m_dataFetchQueue.Enqueue(request);
                    }
                    else
                    {
                        Logger.Info("No valid urls matching the request");
                        throw new Errors.EmptyResponseException("No matching data found.");
                    }
                }
                else
                {
                    Logger.Info("No valid matches.");
                    throw new Errors.EmptyResponseException("No matching data found.");
                }
            }
            catch (EmptyResponseException)
            {
                Logger.Info("No response received");
                Error[] error = new Error[1];
                Error   err   = new Error("No matching item found!");
                error[0] = err;
                PublishResult(request.RequestId, error);
                RequestTrackerItem item = m_requestTracker.FirstOrDefault(r => r.RequestId == request.RequestId);
                m_requestTracker.Remove(item);
            }
            catch (Exception)
            {
                Logger.Error("Encountered exception while parsing: {0}", task.Exception.InnerException.ToString());
                Error[] error = new Error[1];
                Error   err   = new Error(task.Exception.InnerException);
                error[0] = err;
                PublishResult(request.RequestId, error);
                RequestTrackerItem item = m_requestTracker.FirstOrDefault(r => r.RequestId == request.RequestId);
                m_requestTracker.Remove(item);
            }
        }