コード例 #1
0
        private List <string> LoadQuotesFromUrl(string quotesUrl)
        {
            List <string> quotesFromUrl            = new List <string>();
            string        htmlFromQuotesPage       = WebRequestUtility.ReadHtmlPageFromUrl(quotesUrl);
            bool          alreadySkippedFirstEntry = false;

            foreach (string pageFragment in htmlFromQuotesPage.Split(new[] { "title=\"view quote\">" },
                                                                     StringSplitOptions.RemoveEmptyEntries))
            {
                if (!alreadySkippedFirstEntry)
                {
                    alreadySkippedFirstEntry = true;
                    continue;
                }
                int indexOfAnchorCloseTag = pageFragment.IndexOf("</a>", StringComparison.Ordinal);
                if (indexOfAnchorCloseTag < 0)
                {
                    continue;
                }
                string quoteAsString = pageFragment.Substring(0, indexOfAnchorCloseTag);
                quoteAsString = HttpUtility.HtmlDecode(quoteAsString);
                //Console.WriteLine(HttpUtility.HtmlDecode(quoteAsString));
                if (MAX_QUOTES < quotesFromUrl.Count)
                {
                    break;
                }
                quotesFromUrl.Add(quoteAsString);
            }
            return(quotesFromUrl);
        }
コード例 #2
0
        public List <Person> FindPeopleForDate(int month, int date)
        {
            string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month).ToLower();
            string url       = BRAINY_QUOTE_DOMAIN + $@"/birthdays/{monthName}_{date}";
            string page      = WebRequestUtility.ReadHtmlPageFromUrl(url);
            bool   alreadySkippedFirstOne = false;
            var    findPeopleForDate      = new List <Person>();

            foreach (var pageFragment in page.Split(new[] { @"<td><a href=""" }, StringSplitOptions.RemoveEmptyEntries))
            {
                if (!alreadySkippedFirstOne)
                {
                    alreadySkippedFirstOne = true;
                    continue;
                }

                int year;
                if (!GetNameAndUrlFromFragment(pageFragment, out var currentName, out var quotesUrl, out year))
                {
                    continue;
                }
                //Console.WriteLine(quotesUrl);

                findPeopleForDate.Add(
                    new Person()
                {
                    Name   = currentName,
                    Year   = year,
                    Quotes = LoadQuotesFromUrl(quotesUrl),
                });
            }

            findPeopleForDate.Sort(SortPeopleByYear);
            return(findPeopleForDate);
        }
コード例 #3
0
        public List <string> FindAnagram(string letters)
        {
            string url = string.Format($@"https://new.wordsmith.org/anagram/anagram.cgi?anagram={letters}&t=500&a=n");

            string response = WebRequestUtility.ReadHtmlPageFromUrl(url);

            return(ParseResponse(response));
        }
コード例 #4
0
        public List <Dictionary <int, string> > ExecuteQuery(string query, string sheetName = null)
        {
            string encodedQuery = EncodeQuery(query);
            string url          =
                @"https://spreadsheets.google.com/tq?tqx=out:json&tq=" + encodedQuery +
                @"&key=" + GoogleSheetKey;

            if (!string.IsNullOrWhiteSpace(sheetName))
            {
                url += $"&sheet={EncodeQuery(sheetName)}";
            }

            string response = WebRequestUtility.ReadHtmlPageFromUrl(url, IgnoreCache);

            var results = ReadGenericDictionaryFromResponse(response);

            return(results);
        }
コード例 #5
0
        public List <PotentialTheme> FindPotentialThemes(string word)
        {
            List <PotentialTheme> themes = new List <PotentialTheme>();
            string html = WebRequestUtility.ReadHtmlPageFromUrl($"https://www.wordnik.com/fragments/lists/{word}");
            int    indexOfStartOfOtherLists = html.IndexOf(@"<ul class=""other_lists"">", StringComparison.Ordinal);

            if (indexOfStartOfOtherLists < 0)
            {
                return(themes);
            }
            string remainingHtml = html.Substring(indexOfStartOfOtherLists);

            foreach (string listFragment in remainingHtml.Split(new[] { @"<a href=""/lists/" }, StringSplitOptions.RemoveEmptyEntries))
            {
                int    indexOfQuote = listFragment.IndexOf('"');
                string listName     = listFragment.Substring(0, indexOfQuote);
                int    indexOfCount = listFragment.IndexOf(@"<span class=""count"">", StringComparison.Ordinal);
                if (indexOfCount < 0)
                {
                    //Console.WriteLine(listFragment);
                    continue;
                }
                string countAsString = listFragment.Substring(indexOfCount + @"<span class=""count"">".Length);
                int    indexOfSpace  = countAsString.IndexOf(" ", StringComparison.Ordinal);
                countAsString = countAsString.Substring(0, indexOfSpace);
                int count;
                if (int.TryParse(countAsString, out count))
                {
                    if (count <= 300)
                    {
                        themes.Add(new PotentialTheme()
                        {
                            Count = count, Name = listName
                        });
                    }
                }
            }

            themes.Sort((a, b) => a.Count - b.Count);
            return(themes);
        }
コード例 #6
0
        public WordCategory CategorizeWord(string word)
        {
            string html = WebRequestUtility.ReadHtmlPageFromUrl($"https://kids.wordsmyth.net/we/?ent={word}");

            if (html.Contains("<span class=\"dictionary\">Advanced Dictionary</span>"))
            {
                return(WordCategory.AdvancedWord);
            }

            if (html.Contains("Did you mean this word?"))
            {
                return(WordCategory.NotAWord);
            }

            if (html.Contains("<tr class=\"definition\">"))
            {
                return(WordCategory.BasicWord);
            }

            return(WordCategory.AdvancedWord);
        }
コード例 #7
0
        public List <string> FindWordsInList(string listName)
        {
            List <string> words = new List <string>();
            string        html  = WebRequestUtility.ReadHtmlPageFromUrl($"https://www.wordnik.com/lists/{listName}");

            foreach (string fragment in html.Split(new[] { @"<li class=""word""><a href=""/words/" },
                                                   StringSplitOptions.RemoveEmptyEntries))
            {
                int    indexOfQuote  = fragment.IndexOf('"');
                string wordToInclude = fragment.Substring(0, indexOfQuote);
                if (wordToInclude.Contains(" "))
                {
                    continue;
                }
                if (wordToInclude.Contains("-"))
                {
                    continue;
                }
                words.Add(wordToInclude);
            }

            return(words);
        }