コード例 #1
0
ファイル: Scraper.cs プロジェクト: long20p/hackernewsscraper
        /// <summary>
        /// Gets story by ID
        /// </summary>
        /// <param name="rankedStoryId">Object containing story ID and its rank</param>
        /// <returns>A story</returns>
        public async Task <Story> GetStory(RankedStoryId rankedStoryId)
        {
            var item = await dataFetcher.GetPostItem(rankedStoryId.StoryId);

            if (!Uri.TryCreate(item.Url, UriKind.Absolute, out var uri) || !uri.IsWellFormedOriginalString() || !AllowedUriSchemes.Contains(uri.Scheme))
            {
                throw new InvalidPostItemPropertyException("Post URL is not valid");
            }

            ValidatePostItemText(item.Title, "Title");
            ValidatePostItemText(item.By, "Author name");
            ValidatePostItemNumber(item.Score, "Story points");
            ValidatePostItemNumber(item.Descendants, "Comment count");

            var story = new Story
            {
                Title    = item.Title,
                Uri      = item.Url,
                Author   = item.By,
                Points   = item.Score,
                Comments = item.Descendants,
                Rank     = rankedStoryId.Rank
            };

            return(story);
        }