public Task <IEnumerable <Post> > GetPagedItems(Subreddit subreddit, Subreddit.Sort sortCriteria = Subreddit.Sort.Hot, uint itemsPerPage = 10) { return(Task.Run <IEnumerable <Post> >(() => { var postList = subreddit.GetPosts(max: 25); List <Post> retVal = new List <Post>(); postList.ForEach((x) => { retVal.Add(x); }); return retVal; })); }
internal Tuple <SkillResponse, CurrentSession> previousPost(CurrentSession cs) { SkillResponse response; if (cs == null) { response = Function.MakeSkillResponse($"{NO_SUBREDDIT_SELECTED} {NO_SUBREDDIT_SELECTED_REPROMPT}", false, NO_SUBREDDIT_SELECTED_REPROMPT); } else { var backSubredditTask = reddit.SearchSubreddits(cs.subreddit).First(); backSubredditTask.Wait(); Subreddit backSubreddit = backSubredditTask.Result; log.LogLine($"BackSubreddit Selected: {backSubreddit}"); String backIntro; if (cs.postNumber > 1) { cs.postNumber--; backIntro = "Previous Post."; } else { cs.postNumber = 1; backIntro = "Can't go back any further."; } Task <Post> backPostTask = backSubreddit.GetPosts(cs.postNumber).Last(); Post backPost = backPostTask.Result; log.LogLine($"Post retrieved: {backPost}"); cs.selfText = backPost.SelfText; cs.title = backPost.Title; cs.inTitleMode = true; cs.url = backPost.Url.ToString(); List <IDirective> directives = getImageResponseIfUrlLeadsToImage(cs.url, cs.title); String speechResponse = $"{backIntro} {cs.title}."; if (directives != null) { response = Function.MakeSkillResponseWithDirectives(speechResponse, false, directives, BROWSING_REPROMPT_TEXT); } else { response = Function.MakeSkillResponse(speechResponse, false, BROWSING_REPROMPT_TEXT); } } return(new Tuple <SkillResponse, CurrentSession>(response, cs)); }
public async Task PostDankMeme(EduardoContext context) { Subreddit subreddit = await _reddit.GetSubredditAsync("dankmemes"); Listing <Post> postListing = subreddit.GetPosts(Subreddit.Sort.Hot, 100); List <Post> posts = new List <Post>(100); await postListing.ForEachAsync(post => { posts.Add(post); }); Post randomPost = posts[_rng.Next(0, posts.Count)]; using Stream stream = await NetworkHelper.GetStreamAsync(randomPost.Url.AbsoluteUri); await context.Channel.SendFileAsync(stream, $"{randomPost.Title}.png"); }
internal Tuple <SkillResponse, CurrentSession> nextPost(CurrentSession cs) { SkillResponse response; if (cs == null) { response = Function.MakeSkillResponse($"{NO_SUBREDDIT_SELECTED} {NO_SUBREDDIT_SELECTED_REPROMPT}", false, NO_SUBREDDIT_SELECTED_REPROMPT); } else { var contSubredditTask = reddit.SearchSubreddits(cs.subreddit).First(); contSubredditTask.Wait(); Subreddit contSubreddit = contSubredditTask.Result; log.LogLine($"ContSubreddit Selected: {contSubreddit}"); cs.postNumber++; Task <Post> contPostTask = contSubreddit.GetPosts(cs.postNumber).Last(); Post contPost = contPostTask.Result; log.LogLine($"Post retrieved: {contPost}"); cs.selfText = contPost.SelfText; cs.title = contPost.Title; cs.inTitleMode = true; cs.url = contPost.Url.ToString(); List <IDirective> directives = getImageResponseIfUrlLeadsToImage(cs.url, cs.title); String speechResponse = $"Next Post. {cs.title}."; if (directives != null) { response = Function.MakeSkillResponseWithDirectives(speechResponse, false, directives, BROWSING_REPROMPT_TEXT); } else { response = Function.MakeSkillResponse(speechResponse, false, BROWSING_REPROMPT_TEXT); } } return(new Tuple <SkillResponse, CurrentSession>(response, cs)); }
async Task test() { applicationStart = DateTime.UtcNow; using (StreamReader sr = new StreamReader(new FileStream("data//Config.json", FileMode.Open))) Config = JsonConvert.DeserializeObject <Dictionary <string, string> >(sr.ReadToEnd()); using (StreamReader sr = new StreamReader(new FileStream("data//FlairConfig.json", FileMode.Open))) FlairConfig = JsonConvert.DeserializeObject <Dictionary <string, int> >(sr.ReadToEnd()); var webAgent = new BotWebAgent(Config["botAcc"], Config["botPw"], Config["clientId"], Config["clientSecret"], Config["redirectURI"]); reddit = new Reddit(webAgent, false); await reddit.InitOrUpdateUserAsync(); subreddit = await reddit.GetSubredditAsync(Config["subreddit"]); CancellationTokenSource source = new CancellationTokenSource(); CancellationToken token = source.Token; ListingStream <Post> postStream = subreddit.GetPosts(Subreddit.Sort.New).Stream(); postStream.Subscribe(async post => await newPost(post)); ListingStream <Comment> commentStream = subreddit.GetComments().Stream(); commentStream.Subscribe(async comment => await newComment(comment)); ListingStream <VotableThing> removedStream = subreddit.GetRemoved().Stream(); removedStream.Subscribe(async thing => await removedThing(thing)); await Task.WhenAll(new Task[] { postStream.Enumerate(token), commentStream.Enumerate(token), removedStream.Enumerate(token) }); }
public async Task <List <Post> > GetPosts(Subreddit subreddit) { return(await subreddit.GetPosts(100).ToList()); }
private async Task Go() { Config = Cfg.Config; bool d1 = bool.Parse(Config["Delete"]); bool o1 = bool.Parse(Config["Overwrite"]); if (d1 && !o1) { Console.WriteLine("Fix your configuration file. If you delete without also overwriting, then the original comment will remain in the reddit database even though the comment is deleted, according to Reddit admins."); return; } rand = new Random(BitConverter.ToInt32(new Guid().ToByteArray().Take(4).ToArray())); AuthenticatedFixture authFixture = new AuthenticatedFixture(); Reddit reddit = new Reddit(authFixture.WebAgent, true); IEnumerable <string> srs = Config.GetSection("Places:SubReddits").GetChildren().ToList().Select(x => x.Value); foreach (string srn in srs) { Subreddit sr = await reddit.GetSubredditAsync(srn, true); //by comment Console.WriteLine($"comments from {srn} follow"); List <InterestingComment> interstingComments = new List <InterestingComment>(); int junk = 0; IAsyncEnumerator <Comment> comments = sr.GetComments().GetEnumerator(50, -1, false); while (await comments.MoveNext(CancellationToken.None)) { Comment comment = comments.Current; if (comment.Vote == VotableThing.VoteType.Upvote && bool.Parse(Config["UnUpVote"])) { try { await comment.ClearVote(); InterestingLog("un-up-vote +1 => 0 | " + comment.AsString()); } catch (Exception ex) { //can't check for archived without walking up the heirarchy... //InterestingLog($"failed to un-up-vote because {ex.Message} | {comment.AsString()}"); } } else if (comment.Vote == VotableThing.VoteType.Downvote && bool.Parse(Config["UnDownVote"])) { try { await comment.ClearVote(); InterestingLog("un-down-vote -1 => 0 | " + comment.AsString()); } catch (Exception ex) { //can't check for archived without walking up the heirarchy... //InterestingLog($"failed to un-down-vote because {ex.Message} | {comment.AsString()}"); } } if (IsInteresting(comment)) { if (UserIsAuthor(comment)) { try { InterestingComment inter = new InterestingComment() { Comment = comment }; InterestingLog(inter); interstingComments.Add(inter); if (bool.Parse(Config["Overwrite"])) { await comment.EditTextAsync(Dust()); await Task.Delay(1000); // just in case... inter.Overwritten = true; InterestingLog(inter); } if (bool.Parse(Config["Delete"])) { await comment.DelAsync(); inter.Deleted = true; await Task.Delay(1000); // just in case... InterestingLog(inter); } } catch (Exception ex) { Debugger.Break(); } } else { InterestingLog(new InterestingComment() { Comment = comment }); } } else { JunkLog(new JunkComment() { Comment = comment }); junk++; } } Console.WriteLine($"done with {srn} comments, interesting: {interstingComments.Count}, junk: {junk}"); if (interstingComments.Count > 0) { Console.WriteLine(); Console.WriteLine($"Interesting comments from {srn} follow:"); foreach (InterestingComment inter in interstingComments) { Console.WriteLine(inter); } Console.WriteLine(); } //by post Console.WriteLine($"posts from {srn} follow"); List <InterestingPost> interstingPosts = new List <InterestingPost>(); junk = 0; IAsyncEnumerator <Post> posts = sr.GetPosts().GetEnumerator(50, -1, false); while (await posts.MoveNext(CancellationToken.None)) { Post post = posts.Current; if (IsInteresting(post)) { if (UserIsAuthor(post)) { try { InterestingPost inter = new InterestingPost() { Post = post }; InterestingLog(inter); interstingPosts.Add(inter); if (bool.Parse(Config["Overwrite"])) { await post.EditTextAsync(Dust()); await Task.Delay(1000); // just in case... inter.Overwritten = true; InterestingLog(inter); } if (bool.Parse(Config["Delete"])) { await post.DelAsync(); inter.Deleted = true; await Task.Delay(1000); // just in case... InterestingLog(inter); } } catch (Exception ex) { Debugger.Break(); } } else { InterestingLog(new InterestingPost() { Post = post }); } } else { JunkLog(new JunkPost() { Post = post }); junk++; } } Console.WriteLine($"done with {srn} posts, interesting: {interstingPosts.Count}, junk: {junk}"); if (interstingPosts.Count > 0) { Console.WriteLine(); Console.WriteLine($"Interesting posts from {srn} follow:"); foreach (InterestingPost inter in interstingPosts) { Console.WriteLine(inter); } Console.WriteLine(); } } if (bool.Parse(Config["UnDownVote"]) || bool.Parse(Config["UnUpVote"])) { RedditUser user = await RedditUser.GetUserAsync(authFixture.WebAgent, authFixture.UserName); if (bool.Parse(Config["UnDownVote"])) { IAsyncEnumerator <Post> enumDisliked = user.GetDislikedPosts().GetEnumerator(); while (await enumDisliked.MoveNext(CancellationToken.None)) { Post disliked = enumDisliked.Current; if (!disliked.IsArchived) { try { await disliked.SetVoteAsync(VotableThing.VoteType.None); InterestingLog("un-down-vote -1 => 0 | " + disliked.AsString()); } catch (Exception ex) { InterestingLog($"failed to un-down-vote because {ex.Message} | {disliked.AsString()}"); } } } } if (bool.Parse(Config["UnUpVote"])) { IAsyncEnumerator <Post> enumLiked = user.GetLikedPosts().GetEnumerator(); while (await enumLiked.MoveNext(CancellationToken.None)) { Post liked = enumLiked.Current; if (!liked.IsArchived) { try { await liked.SetVoteAsync(VotableThing.VoteType.None); } catch (Exception ex) { InterestingLog($"failed to un-up-vote because {ex.Message} | {liked.AsString()}"); } InterestingLog("un-up-vote +1 => 0 | " + liked.AsString()); } } } } }
public async Task <List <Post> > GetPosts(Subreddit subreddit, int amount = 100) { return(await subreddit.GetPosts(Subreddit.Sort.New, amount).ToListAsync()); }