Ejemplo n.º 1
0
        private void OnExecuteSearchCommand()
        {
            SearchResults.Clear();
            Metas.Clear();
            Task.Factory.StartNew(() =>
            {
                var result = new HtmlWeb().Load(string.Format(Url, QueryString));
                //var nodes = result.DocumentNode.SelectNodes("//html//body//div[@class='g']").Select(r => r.InnerHtml).Take(10);
                //SearchResults.Clear();
                //foreach (var node in nodes)
                //{
                //	SearchResults.Add(node);
                //}

                // extract links

                var searchResultCount = 0;
                foreach (var link in result.DocumentNode.SelectNodes("//a[@href]"))
                {
                    var hrefValue = link.GetAttributeValue("href", string.Empty);
                    if (hrefValue.ToUpper().Contains("GOOGLE") || !hrefValue.Contains("/url?q=") ||
                        !hrefValue.ToUpper().Contains("HTTP://")) continue;
                    var index = hrefValue.IndexOf("&", StringComparison.Ordinal);
                    if (index <= 0) continue;
                    hrefValue = hrefValue.Substring(0, index);
                    var searchResultUrl = hrefValue.Replace("/url?q=", "");
                    if (searchResultCount == 20) continue;
                    searchResultCount++;
                   _dispatcher.Invoke(() =>
                    {
                        SearchResults.Add(searchResultUrl);
                    }, DispatcherPriority.Normal);
                }

                Parallel.ForEach(SearchResults, (c) =>
                {
                    var meta = LoadMeta(c);
                    HtmlTool.FetchOg(c);

                    _dispatcher.Invoke(() =>
                    {
                        Metas.Add(meta);

                    });
                });
            });
        }