public async Task UpdateOrCreateComment(Reddit reddit, ChapterDictionary dict) { Console.WriteLine("Updating comment for: {0} ({1})", Id, PostTitle); var text = GetStickyText(dict); List <Chapter> res = new List <Chapter>(); res.Add(this); if (CommentId == null) { Console.Write("Creating new comment for {0}... ", Id); var post = await reddit.GetPostAsync(new Uri(RedditLink)); var comment = await post.CommentAsync(text); await comment.DistinguishAsync(DistinguishType.Moderator, true); CommentId = comment.Id; Console.WriteLine("Done! CommendId is {0}", CommentId); } else { var post = await reddit.GetPostAsync(new Uri(RedditLink)); var comments = await post.GetCommentsAsync(5, CommentSort.Best); var myComment = comments.SingleOrDefault(a => a.Id == CommentId); if (myComment == null) { Console.WriteLine("Couldn't find our comment {0} for {1}, recreating.", CommentId, Id); CommentId = null; await UpdateOrCreateComment(reddit, dict); return; } if (myComment.Distinguished != DistinguishType.Moderator) { await myComment.DistinguishAsync(DistinguishType.Moderator, true); } if (myComment.Body != text) { Console.WriteLine("Actually updating comment {0} for post {1}", CommentId, Id); await(myComment).EditTextAsync(text); } else { Console.WriteLine("Comment {0} for post {1} has not changed - ignoring :)", CommentId, Id); } } IsPoisoned = false; }
private static async Task <IEnumerable <RedditCrossViewPost> > GetPosts() { var webAgent = new BotWebAgent(Configuration.Instance["reddit-username"], Configuration.Instance["reddit-password"], Configuration.Instance["reddit-token"], Configuration.Instance["reddit-secret"], Configuration.Instance["reddit-url"]); //This will check if the access token is about to expire before each request and automatically request a new one for you //"false" means that it will NOT load the logged in user profile so reddit.User will be null var reddit = new Reddit(webAgent, false); await reddit.InitOrUpdateUserAsync(); var authenticated = reddit.User != null; if (!authenticated) { Console.WriteLine("Invalid token"); } var subreddit = await reddit.GetSubredditAsync("/r/crossview"); var posts = new List <RedditCrossViewPost>(); //await subreddit.GetTop(RedditSharp.Things.FromTime.All).Where(n => n.CreatedUTC.Year == 2016).Take(50).ForEachAsync(post => { //await subreddit.GetTop(RedditSharp.Things.FromTime.Month).Take(50).ForEachAsync(post => { await reddit.GetPostAsync(new Uri(@"https://www.reddit.com/r/CrossView/comments/1ujpnj/some_1920s_stereograms/?ref=share&ref_source=link")).ToAsyncEnumerable().ForEachAsync(post => { var data = new RedditCrossViewPost(post.Url, null, post.Title, post.Shortlink, post.Score, post.CreatedUTC); Console.WriteLine(post.Title); posts.Add(data); }); return(posts); }
public async Task UpdateAsyc(Reddit reddit, ChapterDictionary dict) { var myPost = await reddit.GetPostAsync(new Uri(RedditLink)); var parentId = GetFirstChapterId(myPost.SelfText); if (ParentId != null && ParentId != parentId) { if (dict.Chapters.TryGetValue(ParentId, out var oldParent)) { oldParent.PoisonSelf(dict); oldParent.Children.Remove(Id); } } ParentId = parentId; if (parentId != null && dict.Chapters.TryGetValue(ParentId, out var newParent)) { if (newParent.Children.Add(Id)) { newParent.PoisonSelf(dict); } } if (CommentId == null) { PoisonSelf(dict); } LastUpdate = DateTime.Now; }
public async Task <Post> GetPost(string postId) { return(await redditService.GetPostAsync(new Uri($"{REDDITPREFIX}/comments/{postId}"))); }