private List <Post> GetPosts( RedditClient reddit, string after, int limit, string query, int count) { var searchInput = new SearchGetSearchInput( q: query, after: after, limit: limit, count: count); return(reddit.Search(searchInput)); }
private static List <Post> GetPosts( string query, string after = null, string before = null, int limit = 25, int count = 0) { var reddit = new RedditClient( appId: "Mx2Rp1J2roDMdg", appSecret: "eDT3-0no1WHyTuBWTLoNDQNUqWA", refreshToken: "291925913345-DFbyOHX5f6zz-__Dqbr41jCOoPs"); var searchInput = new SearchGetSearchInput( q: query, after: after, before: before, limit: limit, count: count); return(reddit.Search(searchInput)); }
private static async Task TestRedditAsync() { var reddit = new RedditClient( appId: "Mx2Rp1J2roDMdg", appSecret: "eDT3-0no1WHyTuBWTLoNDQNUqWA", refreshToken: "291925913345-DFbyOHX5f6zz-__Dqbr41jCOoPs"); var indices = Enumerable.Range(0, 100); // ei33zr // ei37wh string after = null; var limit = 50; //var before = "ei2mja"; DateTime?before = null; var query = "university OR study OR studying OR college NOT football"; // Since we only need the posts, there's no need to call .About() on this one. --Kris int total = 0; //string MaxStr(string a, string b) //{ // var cmp = StringComparer.Create(CultureInfo.InvariantCulture, false); // if (cmp.Compare(a, b) > 0) // { // return a; // } // return b; //} DateTime Max(DateTime a, DateTime?b) { if (!b.HasValue) { return(a); } if (a < b.Value) { return(b.Value); } return(a); } List <Post> get( RedditClient reddit, string after, int limit, string query, int count) { var searchInput = new SearchGetSearchInput( q: query, after: after, // before:before, limit: limit, count: count); return(reddit.Search(searchInput)); } var subs = new ConcurrentDictionary <string, int>(); while (true) { var maxBefore = before; var count = 0; var postListing = get(reddit, after, limit, query, count); var outDated = false; while (postListing.Count > 0) { var children = postListing; foreach (var item in children) { if (item.Created <= before) { outDated = true; Console.WriteLine("Outdated encountered"); break; } count++; subs.AddOrUpdate(item.Subreddit, 1, (k, v) => v + 1); maxBefore = Max(item.Created, maxBefore); var title = item.Title; var text = item.Listing.SelfText; var subLength = 20000; if (text.Length > subLength) { text = text.Substring(0, subLength); } Console.WriteLine($"{item.Fullname} {item.Listing.CreatedUTC}"); Console.WriteLine($"\t{title}"); Console.WriteLine($"\t{text}"); var comments = item.Comments.GetTop(100); foreach (var(i, c) in indices.Zip(comments)) { Console.WriteLine($"C-{$"{i:00}"}:\t{c.Body}"); } Console.WriteLine(string.Concat(Enumerable.Range(0, 80).Select(r => "*"))); } PrintDict(subs, total + count); if (outDated) { Console.WriteLine("outdated"); break; } after = postListing.Count > 0 ? postListing.Last().Fullname : after; Console.WriteLine($"after:{after}"); postListing = get(reddit, after, limit, query, count); } before = maxBefore; Console.WriteLine($"waiting: before; {before} after: {after}, c:{count} "); total += count; PrintDict(subs, total); after = null; count = 0; await Task.Delay(TimeSpan.FromSeconds(10)); } }
public async Task <Dictionary <string, PostAnalysisResults> > Analyze(List <Post> posts) { var toReturn = new Dictionary <string, PostAnalysisResults>(); foreach (var post in posts) //TODO error handling { var youTubePosts = new Dictionary <string, List <Post> >(); toReturn.Add(post.Id, new PostAnalysisResults(post, ModuleEnum)); string postYTID = YouTubeHelpers.ExtractVideoId(post.Url.ToString()); Task <Logging.UserPostingHistory> hist; if (!string.IsNullOrEmpty(postYTID)) { //It's a YouTube vid so we can kick off the analysis and get cookin hist = Logging.UserPostingHistory.GetUserPostingHistory(post.AuthorName); if (!youTubePosts.ContainsKey(postYTID)) { youTubePosts.Add(postYTID, new List <Post>()); } youTubePosts[postYTID].Add(post); } else { //not a YouTube post, so bail out continue; } bool success = false; int nonYTPosts = 0; int tries = 0; while (!success && tries < 3) { success = true; try { var recentPosts = RedditClient.Search <RedditSharp.Things.Post>($"author:{post.AuthorName} self:no", RedditSharp.Sorting.New).GetListing(100, 100); foreach (var recentPost in recentPosts) { string ytID = YouTubeHelpers.ExtractVideoId(recentPost.Url.ToString()); if (!string.IsNullOrEmpty(ytID)) { if (!youTubePosts.ContainsKey(ytID)) { youTubePosts.Add(ytID, new List <Post>()); } youTubePosts[ytID].Add(post); } else { nonYTPosts++; } } } catch (Exception ex) { success = false; tries++; if (tries > 3) { Console.WriteLine($"Failed to get search results: {ex.Message}"); processedCache.Remove(post.Id); break; } await Task.Delay(100); } } if (tries > 3) { continue; } var yt = new YouTubeService(new BaseClientService.Initializer { ApiKey = YouTubeAPIKey }); Dictionary <string, List <string> > postHistory = ( await hist ).PostingHistory; string postChannelID = ""; string postChannelName = ""; var req = yt.Videos.List("snippet"); for (var i = 0; i < youTubePosts.Keys.Count; i += 50) { req.Id = string.Join(",", youTubePosts.Keys.Skip(i).Take(50)); var response = await req.ExecuteAsync(); foreach (var vid in response.Items) { foreach (var ytPost in youTubePosts[vid.Id]) { if (!postHistory.ContainsKey(vid.Snippet.ChannelId)) { postHistory.Add(vid.Snippet.ChannelId, new List <string>()); } //check to see if it already exists (aka wasnt deleted and showed up in search results) if (!postHistory[vid.Snippet.ChannelId].Contains(ytPost.Id)) { postHistory[vid.Snippet.ChannelId].Add(ytPost.Id); } if (vid.Id == postYTID) { postChannelID = vid.Snippet.ChannelId; postChannelName = vid.Snippet.ChannelTitle; } } } } if (string.IsNullOrEmpty(postChannelID)) { //shouldn't ever happen, but might if the video is deleted or the channel deleted or something Console.WriteLine($"Channel for post {post.Id} by {post.AuthorName} couldn't be found"); continue; } int totalPosts = postHistory.Sum(ph => ph.Value.Count) + nonYTPosts; int channelPosts = postHistory[postChannelID].Count; if (!IncludePostInPercentage) { totalPosts--; channelPosts--; postHistory[postChannelID].Remove(post.Id); } double percent = ((double)channelPosts / totalPosts) * 100; if (percent > PercentageThreshold && channelPosts > GracePeriod) { var score = new AnalysisScore(); score.ModuleName = "SelfPromotionCombustor"; score.ReportReason = $"SelfPromo: {Math.Round( percent, 2 )}%"; score.Reason = $"Self Promotion for channel '{postChannelName}' with a posting percentage of {Math.Round( percent, 2 )}. Found PostIDs: {string.Join( ", ", postHistory[postChannelID] )}"; score.Score = OVER_PERCENT_SCORE * Settings.ScoreMultiplier; score.RemovalFlair = RemovalFlair; toReturn[post.Id].Scores.Add(score); } } return(toReturn); }