public Post Transform(ResponsePost x)
        {
            var updated = DateTimes.UnixTimeStampToDateTime(x.CreatedUtc);
            var possibleMediaPreviewUrl = GetMediaPreviewUrl(x);

            return(new Post
            {
                Id = ids.IdFromUrl(x.Url),
                Author = x.Author.Truncate(100),
                Content = x.Selftext,
                Score = x.Score,
                Controversiality = null,
                Gilded = x.Gilded,
                Title = x.Title.Truncate(200),
                Subreddit = ids.ExtractSubredditIdFromUrl(x.Url) ?? string.Empty,
                PublishedMonthPrecision = updated.MonthPrecision(),
                PublishedWeekPrecision = updated.DayOfWeekPrecision(),
                PublishedDayPrecision = updated.DayPrecision(),
                PublishedHourPrecision = updated.HourPrecision(),
                PublishedMinutePrecision = updated.MinutePrecision(),
                PublishedTimestamp = updated,
                IngestedTimestamp = DateTime.UtcNow,
                Url = x.Url,
                MediaPreviewUrl = possibleMediaPreviewUrl,
            });
        }
        public Comment Transform(ResponseComment x)
        {
            var updated        = DateTimes.UnixTimeStampToDateTime(x.CreatedUtc);
            var url            = $"https://www.reddit.com{x.Permalink}";
            var parentRedditId = ids.ExtractIdFromTypeAndId(x.ParentId);
            var parentUrl      = ids.ParentUrlFromChildDetails(
                childUrl: url,
                childId: x.Id,
                parentId: parentRedditId
                );
            var generatedParentId = ids.IdFromUrl(parentUrl);
            var postUrl           = ids.PostUrlFromChildDetails(
                childUrl: url,
                childId: x.Id
                );
            var generatedPostId = ids.IdFromUrl(postUrl);

            var id = ids.IdFromUrl(url);

            if (new string[] { parentRedditId, parentUrl, generatedParentId, postUrl, generatedPostId }.Any(
                    string.IsNullOrEmpty))
            {
                log.Error(
                    $"One or more of the essential structure providing details was null or empty: [parentRedditId={parentRedditId}], [parentUrl={parentUrl}], [generatedParentId={generatedParentId}], [postUrl={postUrl}], [generatedPostId={generatedPostId}]");
                log.Error("This entry will not be saved to the database!");

                return(new InvalidComment()
                {
                    Id = id
                });
            }
            else
            {
                return(new Comment
                {
                    Id = id,
                    Author = x.Author.Truncate(100),
                    Content = x.Body,
                    Score = x.Score,
                    Controversiality = x.Controversiality,
                    Gilded = x.Gilded,
                    Subreddit = ids.ExtractSubredditIdFromUrl(url) ?? string.Empty,
                    PublishedMonthPrecision = updated.MonthPrecision(),
                    PublishedWeekPrecision = updated.DayOfWeekPrecision(),
                    PublishedDayPrecision = updated.DayPrecision(),
                    PublishedHourPrecision = updated.HourPrecision(),
                    PublishedMinutePrecision = updated.MinutePrecision(),
                    PublishedTimestamp = updated,
                    IngestedTimestamp = DateTime.UtcNow,
                    Url = url,
                    ParentId = generatedParentId,
                    PostId = generatedPostId,
                    ParentUrl = parentUrl,
                    PostUrl = postUrl,
                });
            }
        }
Exemplo n.º 3
0
        public void SubredditIdFromUrl_url_noWWW()
        {
            const string url =
                "https://reddit.com/r/Hawaii/comments/6z7mcz/eli5_molokai_ranch_which_covers_about_35_of_the/";

            logger.Setup(x => x.Info(It.IsAny <string>(), It.IsAny <string>()));
            ids = new Ids(logger.Object);
            var result = ids.ExtractSubredditIdFromUrl(url);

            Assert.AreEqual("Hawaii", result);
        }
Exemplo n.º 4
0
        public void SubredditIdFromUrl_url_is_empty()
        {
            var url = string.Empty;

            logger.Setup(x => x.Info(It.IsAny <string>(), It.IsAny <string>()));

            ids = new Ids(logger.Object);
            var result = ids.ExtractSubredditIdFromUrl(url);

            Assert.IsNull(result);

            logger.Verify(
                x => x.Info(
                    It.Is <string>(message => message == "URL is null or empty.  No subreddit ID found."),
                    It.IsAny <string>()
                    ),
                Times.Once
                );
        }
Exemplo n.º 5
0
        public void SubredditIdFromUrl_url_is_not_reddit()
        {
            const string url =
                "https://www.example.com/stuff";

            logger.Setup(x => x.Info(It.IsAny <string>(), It.IsAny <string>()));

            ids = new Ids(logger.Object);
            var result = ids.ExtractSubredditIdFromUrl(url);

            Assert.IsNull(result);

            logger.Verify(
                x => x.Info(
                    It.Is <string>(message => message == $"No subreddit found in URL {url}"),
                    It.IsAny <string>()
                    ),
                Times.Once
                );
        }