/// <summary>Get the top posts from hackernews.com in json string format</summary> /// <param name="numberOfPosts">Number of posts you want to get (0 > posts < 101)</param> /// <returns>a string containing the number of posts specified in a json format</returns> public async Task <string> GetXNumberOfTopHackerNewsPosts(int numberOfPosts) { string storyInfo = ""; if (numberOfPosts > 0 && numberOfPosts < 101) { var data = await HackerNewsAPI.GetTopHackerNewsStoryIds(); List <int> storyIds = parseListOfStoryIdsFromJson(data, numberOfPosts); storyInfo = await GetStoriesBasedOnStoryIds(storyIds); } else { storyInfo = "Number of posts specified is out of bounds. Please enter a value between 0 and 100"; } return(storyInfo); }
/// <summary>Get a single story from hacker news based on the id</summary> /// <param name="storyIds">hacker news post Id</param> /// <param name="rank">the rank at which the story is on hacker news</param> /// <returns>A StoryInfo object with fields containing info about the story</returns> static async Task <StoryInfo> GetStoryBasedOnStoryIdAndRank(int storyId, int rank) { //Get the story information string json = await HackerNewsAPI.GetHackerNewsStoriesById(storyId); JObject storyInfo = (JObject)JsonConvert.DeserializeObject(json); //Title StoryInfo hnInfo = new StoryInfo(); if (storyInfo.GetValue("title") != null) { string title = storyInfo.GetValue("title").ToString(); //Check if title is non-empty string if (!String.IsNullOrEmpty(title) && title.Length < 257) { hnInfo.Title = title; } else if (title.Length > 256) { hnInfo.Title = "Title too long"; } else { hnInfo.Title = "No Title Available"; } } //Uri if (storyInfo.GetValue("url") != null) { hnInfo.Uri = storyInfo.GetValue("url").ToString(); //Check if URL is valid Uri uriResult; bool result = Uri.TryCreate(hnInfo.Uri, UriKind.Absolute, out uriResult) && (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps); if (!result) { hnInfo.Uri = hnInfo.Uri + " (Invalid Uri)"; } } //Author if (storyInfo.GetValue("by") != null) { string author = storyInfo.GetValue("by").ToString(); //Check if Author is non-empty string if (!String.IsNullOrEmpty(author) && author.Length < 257) { hnInfo.Author = author; } else if (author.Length > 257) { hnInfo.Author = "Author name too long"; } else { hnInfo.Author = "No Author Available"; } } //Points if (storyInfo.GetValue("score") != null) { string score = storyInfo.GetValue("score").ToString(); //Check if greater or equal to zero bool isInt = int.TryParse(score, out int intScore); if (isInt && intScore >= 0) { hnInfo.Points = score; } else { hnInfo.Points = "No score available"; } } //Comments if (storyInfo.GetValue("descendants") != null) { string comments = storyInfo.GetValue("descendants").ToString(); //Check if greater or equal to zero bool isInt = int.TryParse(comments, out int intComments); if (isInt && intComments >= 0) { hnInfo.comments = comments; } else { hnInfo.comments = "No comment info available"; } } //Rank //Check if greater or equal to zero if (rank >= 0) { hnInfo.rank = rank.ToString(); } else { hnInfo.rank = "No Rank Info Available"; } // Convert DtoryInfo object to JOSN string format //string jsonData = JsonConvert.SerializeObject(hnInfo, Formatting.Indented); return(hnInfo); }