Exemplo n.º 1
0
        public IDictionary <VashmagazinPhone, int> GetPhones()
        {
            ConcurrentDictionary <VashmagazinPhone, int> agents = new ConcurrentDictionary <VashmagazinPhone, int>();

            IEnumerable <Rubrics> rubrics = Enum.GetValues(typeof(Rubrics)).Cast <Rubrics>();

            foreach (Rubrics rubric in rubrics)
            {
                bool hasRubricError = false;
                IEnumerable <Subrubrics> subrubrics = Enum.GetValues(typeof(Subrubrics)).Cast <Subrubrics>();
                foreach (Subrubrics subrubric in subrubrics)
                {
                    int  page         = 1;
                    bool hasPageError = false;
                    do
                    {
                        string url      = _pageLoader.GetUrl(rubric, subrubric, page);
                        string pageHtml = _pageLoader.LoadPageHtml(url);
                        ICollection <VashmagazinPhone> phones = parseHtmlPageForGetPhone(pageHtml, rubric, out hasPageError);
                        if (page == 1)
                        {
                            hasRubricError = hasPageError;
                        }

                        if (!hasPageError)
                        {
                            foreach (VashmagazinPhone phone in phones)
                            {
                                int amount;
                                if (agents.TryGetValue(phone, out amount))
                                {
                                    agents.TryUpdate(phone, amount + 1, amount);
                                }
                                else
                                {
                                    agents.TryAdd(phone, 1);
                                }
                            }

                            _loger.Log(url);
                            _loger.Log("Total phones: " + agents.Count);
                            page++;
                        }
                    } while (!hasPageError);
                    if (hasRubricError)
                    {
                        break;
                    }
                }
            }
            return(agents);
        }