Пример #1
0
        public void Search()
        {
            if (RangeSearch)
            {
                logger.Log("Starting range search...");

                List <BingResult> rangeResults = new List <BingResult>();
                for (int i = 0; i < Range; i++)
                {
                    Page = (i * 10);
                    Thread tRange = new Thread(SearchMethod);
                    tRange.SetApartmentState(ApartmentState.STA);
                    tRange.Start();
                    tRange.Join();
                    rangeResults.Add(BingResult);
                }

                BingResult = BingResult.ConsolidateResults(rangeResults, GetUrl());

                return;
            }

            logger.Log($"Search for {SearchTerm} has begun...", logger.STATUS_INFO);

            Thread t = new Thread(SearchMethod);

            t.SetApartmentState(ApartmentState.STA);
            t.Start();

            t.Join();

            logger.Log("Search completed", logger.STATUS_OKAY);
        }
Пример #2
0
        public BingResult ConsolidateResults(List <BingResult> results, string url)
        {
            List <Result> res = new List <Result>();

            foreach (BingResult r in results)
            {
                res.AddRange(r.SearchResults);
            }

            BingResult newResult = new BingResult(results[0].SearchResultsCount, res.Count, res, url);

            return(newResult);
        }
Пример #3
0
        private void SearchMethod()
        {
            int           resCount = 0;
            int           fetched  = 0;
            List <Result> results  = new List <Result>();

            string url = GetUrl();

            logger.Log("Url: " + url, logger.STATUS_INFO);
            logger.Log("Start From: " + Page + "\n", logger.STATUS_INFO);
            HtmlWeb web = new HtmlWeb();

            HtmlDocument document = web.LoadFromBrowser(url, o =>
            {
                WebBrowser webBrowser = (WebBrowser)o;
                return(!string.IsNullOrEmpty(webBrowser.Document.GetElementById("b_results").InnerHtml));
            });

            var allResults = new List <HtmlNode>();

            try
            {
                allResults = document.DocumentNode.SelectNodes("//ol/li/h2").ToList();
            }
            catch (Exception e)
            {
                allResults = new List <HtmlNode> {
                    HtmlNode.CreateNode("<h2>No results</h2>")
                };
            }

            List <string> resultsList = new List <string>();

            foreach (var listItem in allResults)
            {
                resultsList.Add(listItem.InnerHtml);
            }

            foreach (string res in resultsList)
            {
                if (res == "No results")
                {
                    Result r = new Result("No results", "No results");
                    results.Add(r);
                    continue;
                }

                string resName = res.Substring(res.IndexOf('>') + 1);
                resName = resName.Replace(" - Pastebin.com", "");
                resName = resName.Split('<')[0];

                string resUrl = res.Substring(res.IndexOf('=') + 2);
                resUrl = resUrl.Split('"')[0];

                Result result = new Result(resName, resUrl);
                results.Add(result);
            }

            var possibleResCount = document.DocumentNode.SelectNodes("//div/span").ToList();

            foreach (var poss in possibleResCount)
            {
                if (poss.InnerText.Contains(" results"))
                {
                    string asText = poss.InnerText;
                    asText = asText.Replace(" results", "");
                    asText = asText.Replace(",", "");

                    if (asText.Contains("of"))
                    {
                        asText = asText.Split(' ')[2];
                    }

                    resCount = int.Parse(asText);
                }
            }

            fetched = results.Count;

            BingResult bingResult = new BingResult(resCount, fetched, results, GetUrl());

            BingResult = bingResult;
        }