Ejemplo n.º 1
0
        public static CommandResponse Google(CommandEventArgs args)
        {
            if (String.IsNullOrEmpty(args.Parameters))
            {
                return(new CommandResponse(""));
            }
            //var data = new WebClient().DownloadString($"https://www.google.com/search?q={args.Parameters}");
            //var matches = Reg.Matches(data);
            try
            {
                var searchResults = new Dictionary <string, string>();
                var web           = new HtmlWeb();
                //search
                var url     = $"https://www.google.com/search?q={args.Parameters}";
                var doc     = web.Load(url);
                var results = doc.DocumentNode.SelectNodes("//div[@class='g']/div/h3/a");

                var inline = args.Message == null;
                if (!inline)
                {
                    foreach (var result in results)
                    {
                        searchResults.Add(result.InnerText.Replace("<span>", "").Replace("</span>", ""), result.Attributes["href"].Value);
                        //Console.WriteLine(result.InnerHtml);
                    }

                    //foreach (Match result in matches)
                    //{
                    //    if (result.Value.Contains("a class=\"sla\"")) continue;
                    //    if (result.Value.Contains("<a href=\"/search?q=")) continue;
                    //    var start = result.Value.Replace("<h3 class=\"r\"><a href=\"/url?q=", "");
                    //    var url = start.Substring(0, start.IndexOf("&amp;sa=U"));
                    //    var title = start.Substring(start.IndexOf(">") + 1);
                    //    title = title.Replace("<b>", "").Replace("</b>", "").Replace("</a></h3>", "");
                    //    title = System.Web.HttpUtility.HtmlDecode(title);
                    //    searchResults.Add(title, url);
                    //    if (searchResults.Count >= 5) break;
                    //}
                    var menu = new Menu();
                    foreach (var result in searchResults)
                    {
                        menu.Buttons.Add(new InlineButton(result.Key, url: result.Value));
                    }
                    return(new CommandResponse("Here are the results for " + args.Parameters, menu: menu));
                }
                var response = $"I googled that for you:\n\n{results[0].InnerText.Replace("<span>", "").Replace("</span>", "")}\n{results[0].Attributes["href"].Value}";
                return(new CommandResponse(response));
            }
            catch (Exception e)
            {
                return(new CommandResponse("Sorry, I wasn't able to pull the results.  Send this to Para: " + e.Message));
            }
        }
Ejemplo n.º 2
0
        public static CommandResponse Google(CommandEventArgs args)
        {
            if (String.IsNullOrEmpty(args.Parameters))
            {
                return(new CommandResponse(""));
            }
            var data    = new WebClient().DownloadString($"https://www.google.com/search?q={args.Parameters}");
            var matches = Reg.Matches(data);

            try
            {
                var searchResults = new Dictionary <string, string>();
                foreach (Match result in matches)
                {
                    if (result.Value.Contains("a class=\"sla\""))
                    {
                        continue;
                    }
                    if (result.Value.Contains("<a href=\"/search?q="))
                    {
                        continue;
                    }
                    var start = result.Value.Replace("<h3 class=\"r\"><a href=\"/url?q=", "");
                    var url   = start.Substring(0, start.IndexOf("&amp;sa=U"));
                    var title = start.Substring(start.IndexOf(">") + 1);
                    title = title.Replace("<b>", "").Replace("</b>", "").Replace("</a></h3>", "");
                    title = System.Web.HttpUtility.HtmlDecode(title);
                    searchResults.Add(title, url);
                    if (searchResults.Count >= 5)
                    {
                        break;
                    }
                }
                var menu = new Menu();
                foreach (var result in searchResults)
                {
                    menu.Buttons.Add(new InlineButton(result.Key, url: result.Value));
                }
                return(new CommandResponse("Here are the results for " + args.Parameters, menu: menu));
            }
            catch (Exception e)
            {
                return(new CommandResponse("Sorry, I wasn't able to pull the results.  Send this to Para: " + e.Message));
            }
        }
Ejemplo n.º 3
0
        public static CommandResponse Google(CommandEventArgs args)
        {
            if (String.IsNullOrEmpty(args.Parameters))
            {
                return(new CommandResponse(""));
            }
            try
            {
                var searchResults = new List <(string Text, string Url)>();
                var web           = new HtmlWeb
                {
                    UserAgent = @"Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36"
                };

                //search
                var url = $"https://www.google.com/search?q={args.Parameters}";
                var doc = web.Load(url);

                string quickanswer = "";
                var    answerbox   = doc.DocumentNode.SelectSingleNode("//div[@role='heading']/div[not(descendant::a)]");
                if (answerbox != null)
                {
                    quickanswer = "Quick answer: ";
                    if (answerbox.ChildNodes.Any(x => x.Name == "div"))
                    {
                        var divs = answerbox.SelectNodes("div");
                        quickanswer += divs[0].InnerText;
                        for (int i = 1; i < divs.Count; i++)
                        {
                            quickanswer += "\n" + divs[i].InnerText;
                        }
                    }
                    else
                    {
                        quickanswer += answerbox.InnerText;
                    }
                    quickanswer += "\n\n";
                }

                var results = doc.DocumentNode.SelectNodes("//div[@class='rc']/div/a[descendant::h3]");

                var inline = args.Message == null;
                if (!inline)
                {
                    foreach (var result in results)
                    {
                        var link = result.Attributes["href"].Value;
                        var text = result.SelectSingleNode("h3").InnerText;
                        searchResults.Add((text, link));
                    }

                    var menu = new Menu();
                    foreach (var(Text, Url) in searchResults)
                    {
                        menu.Buttons.Add(new InlineButton(Text, url: Url));
                    }
                    var answer = quickanswer + "Here are the results for " + args.Parameters;
                    return(new CommandResponse(answer, menu: menu));
                }
                var response = $"I googled that for you:\n{args.Parameters}\n\n{quickanswer}{results[0].SelectSingleNode("h3").InnerText}\n{results[0].Attributes["href"].Value}";
                return(new CommandResponse(response));
            }
            catch (Exception e)
            {
                return(new CommandResponse("Sorry, I wasn't able to pull the results.  Send this to Para: " + e.Message));
            }
        }