private void AddTwitterPosts(string hashTag, TwitterConfiguration twitterConfig, ISocialFeedsRepository socialFeedRepo) { // bool isPreviousPost = true; int DescriptionLength = 500; var service = new TwitterService(twitterConfig.ConsumerKey, twitterConfig.ConsumerSecret); service.AuthenticateWith(twitterConfig.Token, twitterConfig.TokenSecret); TwitterSearchResult prevResults = null; TwitterSearchResult currentResults = null; SearchOptions options = new SearchOptions() { Q = hashTag, Count = 100 }; long sinceId = socialFeedRepo.GetMaxTwitterPostId(hashTag); if (sinceId > 0) { options.SinceId = sinceId; options.Resulttype = TwitterSearchResultType.Recent; } currentResults = service.Search(options); sinceId = socialFeedRepo.GetMinTwitterPostId(hashTag); if (sinceId > 0) { options.SinceId = null; options.MaxId = sinceId; prevResults = service.Search(options); } List <Post> posts = new List <Post>(); var parallerOptions = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount * 2 }; if (currentResults != null && currentResults.Statuses != null) { Parallel.ForEach(currentResults.Statuses, parallerOptions, item => { if (TwitterPostType(item) == PostType.Text && !string.IsNullOrEmpty(item.Id.ToString())) { Post post = new Post(); post.PostSource = SocialNetwork.Twitter; post.SocialNetworkPostId = Convert.ToString(item.Id); post.SocialNetworkUserId = item.User.Id; post.SocialNetworkUsername = item.User.ScreenName; post.PostType = TwitterPostType(item); post.PostUrl = TwitterPostUrl(item); post.ThumbnailUrl = TwitterThumbnailUrl(item); post.SocialNetworkUserPictureUrl = item.User.ProfileImageUrlHttps; post.Description = GetTrimmedString(item.Text, DescriptionLength); post.PostDateCreated = item.CreatedDate; post.IsVIPContent = false; post.hashTag = hashTag; post.Status = PostStatus.Pending; //if (post.PostDateCreated >= Settings.PostDateLimit) //{ posts.Add(post); //} } }); } if (prevResults != null && prevResults.Statuses != null) { Parallel.ForEach(prevResults.Statuses, parallerOptions, item => { if (TwitterPostType(item) == PostType.Text && !string.IsNullOrEmpty(item.Id.ToString())) { Post post = new Post(); post.PostSource = SocialNetwork.Twitter; post.SocialNetworkPostId = Convert.ToString(item.Id); post.SocialNetworkUserId = item.User.Id; post.SocialNetworkUsername = item.User.ScreenName; post.PostType = TwitterPostType(item); post.PostUrl = TwitterPostUrl(item); post.ThumbnailUrl = TwitterThumbnailUrl(item); post.SocialNetworkUserPictureUrl = item.User.ProfileImageUrlHttps; post.Description = GetTrimmedString(item.Text, DescriptionLength); post.PostDateCreated = item.CreatedDate; post.IsVIPContent = false; post.hashTag = hashTag; post.Status = PostStatus.Pending; //if (post.PostDateCreated >= Settings.PostDateLimit) //{ posts.Add(post); //} //else //{ isPreviousPost = false; } } }); } if (posts.Count > 0) { socialFeedRepo.SaveFeeds(posts); //if (!isPreviousPost) //{ // socialFeedRepo.UpdateFeed(hashTag); //} } posts.Clear(); }
public SocialFeedsService(ISocialFeedsRepository socialFeedsRepository) { _socialFeedsRepository = socialFeedsRepository; }