Ejemplo n.º 1
0
        public string Analyse(Phrase phrase, long chatId)
        {
            if (!phrase.IsFirstWordEquals("загугли"))
            {
                return(null);
            }

            var    term = HttpUtility.UrlEncode(phrase.RemoveFirstWord().ToString());
            var    url  = $"http://www.google.com/search?num=1&q={term}";
            string html;

            using (var webClient = new WebClient())
            {
                html = webClient.DownloadString(url);
            }

            var regex   = new Regex("<div class=\"g\">(.*?)</div>");
            var matches = regex.Matches(html).Cast <Match>().ToList();

            if (!matches.Any())
            {
                return(null);
            }

            var firstMatch = matches.First().Value;

            var regex2   = new Regex("<a href=\"(.*?)&amp;(.*?)\">(.*?)a>");
            var matches2 = regex2.Matches(firstMatch).Cast <Match>().ToList();

            if (!matches2.Any())
            {
                return(null);
            }

            var requiredUrl = matches2.First().Groups[1].Value;

            if (requiredUrl.StartsWith("/url?q="))
            {
                requiredUrl = requiredUrl.Substring(7, requiredUrl.Length - 7);
            }

            requiredUrl = HttpUtility.UrlDecode(requiredUrl);
            requiredUrl = HttpUtility.UrlDecode(requiredUrl);

            return(requiredUrl.StartsWith("/search") ? "Это секретная информация!" : requiredUrl);
        }
Ejemplo n.º 2
0
        public void IsFirstWordEqualsFalseTest()
        {
            var phrase = new Phrase("загугли дич");

            phrase.IsFirstWordEquals("дич").Should().BeFalse();
        }