Exemplo n.º 1
0
        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;
        }
Exemplo n.º 2
0
        private string GetStickyText(ChapterDictionary dict)
        {
            var sb = new StringBuilder();

            sb.AppendLine("[Disclaimer](https://www.reddit.com/r/RedditWritesFanfic/comments/93ps6d/disclaimer/)");
            sb.AppendLine();

            var urlencodedLink = Uri.EscapeDataString(RedditLink);
            var link           = $"https://www.reddit.com/r/{Subreddit}/submit?selftext=true&text=[Previous%20Chapter]({urlencodedLink})%20%0A%0AWrite%20your%20story%20here!&title=Chapter%20Title";

            sb.AppendLine($"## [Continue this story!]({link})");

            if (Children.Count == 0)
            {
                return(sb.ToString());
            }

            sb.AppendLine("-----");
            sb.AppendLine();
            sb.AppendLine("## Next Chapters:");
            sb.AppendLine("");
            foreach (var child in dict.EnumerateChildren(Children))
            {
                sb.AppendFormat("* **[{0}]({1})** by /u/{2} - {3} Chapters deep!\n", child.PostTitle, child.RedditLink, child.Author, child.GetDepth(dict) + 1);
            }

            return(sb.ToString());
        }
Exemplo n.º 3
0
        private static async Task AddNewPostsAsync(ChapterDictionary dict, Reddit reddit, string subreddit)
        {
            Console.WriteLine($"Searching the subreddit { subreddit } for new posts.");

            var sub = await reddit.GetSubredditAsync(subreddit);

            var posts = await sub.GetPosts(Subreddit.Sort.New, 20).ToArray();

            foreach (var post in posts)
            {
                if (!dict.Chapters.ContainsKey(post.Id) && post.LinkFlairCssClass == "chapter")
                {
                    Console.WriteLine("Creating Chapter: {0}", post.Id);
                    var intThread = new Chapter()
                    {
                        Author     = post.AuthorName,
                        PostTitle  = post.Title,
                        Subreddit  = post.SubredditName,
                        Id         = post.Id,
                        LastUpdate = DateTime.MinValue,
                    };
                    dict.Chapters[intThread.Id] = intThread;
                }
            }
        }
Exemplo n.º 4
0
        static async Task MainAsync(string[] args)
        {
            var    login     = File.ReadAllLines(args[0]);
            string subreddit = args[1];

            var    wa     = new BotWebAgent(login[0], login[1], login[2], login[3], login[4]);
            Reddit reddit = new Reddit(wa, true);

            ChapterDictionary dict = new ChapterDictionary(subreddit + ".json");

            Console.WriteLine("Current State: ");
            dict.PrintState();

            while (true)
            {
                await AddNewPostsAsync(dict, reddit, subreddit);

                dict.Save();

                await UpdateChaptersAsync(dict, reddit, subreddit);

                dict.Save();

                await UpdatePoisonedChaptersAsync(dict, reddit, subreddit);

                dict.Save();


                Console.WriteLine("Sleeping.");
                Thread.Sleep(2000);
            }
        }
Exemplo n.º 5
0
 private void PoisonSelf(ChapterDictionary dict)
 {
     IsPoisoned = true;
     if (ParentId != null && dict.Chapters.TryGetValue(ParentId, out var parent))
     {
         parent.PoisonSelf(dict);
     }
 }
Exemplo n.º 6
0
        public int GetDepth(ChapterDictionary dict)
        {
            if (Children.Count == 0)
            {
                return(0);
            }

            return(dict.EnumerateChildren(Children).Max(a => a.GetDepth(dict)) + 1);
        }
Exemplo n.º 7
0
        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;
        }
Exemplo n.º 8
0
 private static async Task UpdateChaptersAsync(ChapterDictionary dict, Reddit reddit, string subreddit)
 {
     Console.WriteLine($"Updating chapters in { subreddit }.");
     foreach (var chapter in dict.Chapters.Values)
     {
         if (chapter.ShouldUpdate)
         {
             try
             {
                 await chapter.UpdateAsyc(reddit, dict);
             }
             catch (Exception e)
             {
                 Console.WriteLine("Error when updating chapter " + chapter.Id + ": " + e.ToString());
             }
         }
     }
 }
Exemplo n.º 9
0
        private static async Task UpdatePoisonedChaptersAsync(ChapterDictionary dict, Reddit reddit, string subreddit)
        {
            Console.WriteLine($"Updating chapters in { subreddit }.");
            foreach (var chapter in dict.Chapters.Values)
            {
                if (chapter.IsPoisoned)
                {
                    try
                    {
                        await chapter.UpdateOrCreateComment(reddit, dict);

                        dict.Save();
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine("Exception when updating comment of" + chapter.Id + ": " + e.ToString());
                    }
                }
            }
        }