Beispiel #1
0
        public async Task <IDictionary <NewsThread, List <Post> > > GetPosts(string board, IEnumerable <NewsThread> threads)
        {
            using (var op = Begin("Get posts for {0} threads for board {1}", threads.Count(), board))
            {
                var source   = $"r.{board}";
                var webAgent = new BotWebAgent(Config("Reddit:User"), Config("Reddit:Pass"), Config("Reddit:ClientId"), Config("Reddit:ClientSecret"), "https://github.com/allisterb/Canaan");
                webAgent.UserAgent = "Canaan/0.1";
                var reddit      = new RedditSharp.Reddit(webAgent);
                var threadPosts = new Dictionary <NewsThread, List <Post> >();
                var redditPosts = new Dictionary <RedditSharp.Things.Post, List <Post> >();
                var r           = await reddit.GetSubredditAsync(board);

                await r.GetPosts(Subreddit.Sort.Top, 400).ForEachAsync(async(post, p) =>
                {
                    var thread = threads.SingleOrDefault(t => t.Id == post.Id + "-" + YY);
                    if (thread != null)
                    {
                        var comments = await post.GetCommentsAsync();
                        var posts    = comments.Select((c, cp) => GetPostsFromComment(board, cp, thread.Id, c, null)).SelectMany(x => x).ToList();
                        threadPosts.Add(thread, posts);
                    }
                });

                return(threadPosts);
            }
        }
Beispiel #2
0
        public async Task <IEnumerable <NewsThread> > GetThreads(string board)
        {
            using (var op = Begin("Get threads for board {0}", board))
            {
                var source   = $"r.{board}";
                var webAgent = new BotWebAgent(Config("Reddit:User"), Config("Reddit:Pass"), Config("Reddit:ClientId"), Config("Reddit:ClientSecret"), "https://github.com/allisterb/Canaan");
                webAgent.UserAgent = "Canaan/0.1";
                var reddit  = new RedditSharp.Reddit(webAgent);
                var threads = new List <NewsThread>();
                var r       = await reddit.GetSubredditAsync(board);

                await r.GetPosts(Subreddit.Sort.Top, 400).ForEachAsync((post, p) =>
                {
                    var text          = post.IsSelfPost ? post.SelfText : string.Empty;
                    var html          = post.IsSelfPost ? post.SelfTextHtml : null;
                    NewsThread thread = new NewsThread()
                    {
                        Id            = post.Id + "-" + YY,
                        Source        = source,
                        Position      = p + 1,
                        Subject       = post.Title,
                        DatePublished = post.CreatedUTC,
                        User          = post.AuthorName,
                        Text          = text,
                        Links         = post.IsSelfPost ? WebScraper.ExtractLinksFromHtmlFrag(html) : new Link[] { new Link()
                                                                                                                   {
                                                                                                                       Uri = post.Url
                                                                                                                   } }
                    };
                    threads.Add(thread);
                });

                return(threads);
            }
        }
        /// <inheritdoc />
        protected override async Task GatherAsync(IDownloaderClient client, List <IPost> list, CancellationToken token)
        {
            using var valid = new CancellationTokenSource();

            var subreddit = await _Reddit.GetSubredditAsync(Subreddit).CAF();

            try
            {
                await subreddit.GetPosts(RedditSharp.Things.Subreddit.Sort.New, int.MaxValue).ForEachAsync(post =>
                {
                    valid.Token.ThrowIfCancellationRequested();
                    token.ThrowIfCancellationRequested();
                    if (post.CreatedUTC < OldestAllowed)
                    {
                        valid.Cancel();
                    }
                    else if (post.IsStickied || post.IsSelfPost || post.Score < MinScore)
                    {
                        return;
                    }
                    else if (!Add(list, new Model(post)))
                    {
                        valid.Cancel();
                    }
                }, valid.Token).CAF();
            }
            catch (OperationCanceledException) when(!token.IsCancellationRequested)
            {
            }
        }
        public void Register(IModularContainer container)
        {
            var appConfiguration = new AppConfiguration();

            // Register the logger for the application immediately
            container.RegisterLogger(appConfiguration.LogFilename);

            // Actual login is performed here.
            var botWebAgent = new BotWebAgent
                              (
                appConfiguration.DB4Username,
                appConfiguration.DB4Password,
                appConfiguration.DB4ClientId,
                appConfiguration.DB4ClientSecret,
                "http://localhost"
                              );

            var reddit    = new RedditSharp.Reddit(botWebAgent, true);
            var subreddit = reddit.GetSubredditAsync($"/r/{appConfiguration.SubredditName}").Result;

            // Register core / shared classes
            container.RegisterSingleton(appConfiguration);
            container.RegisterSingleton <AutoRestartManager>();
            container.RegisterSingleton(botWebAgent);
            container.RegisterSingleton(reddit);
            container.RegisterSingleton(subreddit);
            container.RegisterSingleton <RedditState>();

            // Register shared services
            container.Register <IDB4Queue, DB4MemoryQueue>();

            // Register persistence services
            container.Register <IDB4Repository, DB4Repository>();

            // Register Reddit Services
            container.Register <IActivityDispatcher, RedditSharpActivityDispatcher>();
            container.Register <IActivityMonitor, RedditSharpActivityMonitor>();
            container.Register <IRedditService, RedditSharpRedditService>();
            container.Register <ISubredditService, RedditSharpSubredditService>();

            // Register functionality implementations
            container.Register <IDB4QueueDispatcher, DB4QueueDispatcher>();
            container.Register <ICommentProcessor, CommentProcessor>();
            container.Register <ICommentBuilder, CommentBuilder>();
            container.Register <ICommentDetector, CommentDetector>();
            container.Register <ICommentValidator, CommentValidator>();
            container.Register <ICommentReplier, CommentReplier>();
            container.Register <IDeltaAwarder, DeltaAwarder>();
            container.Register <IDeltaboardEditor, DeltaboardEditor>();
            container.Register <IDeltaLogEditor, DeltaLogEditor>();
            container.Register <IHealthPinger, HealthPinger>();
            container.Register <IPostBuilder, PostBuilder>();
            container.Register <IPrivateMessageProcessor, PrivateMessageProcessor>();
            container.Register <IPrivateMessageHandlerFactory, PrivateMessageHandlerFactory>();
            container.Register <IStickyCommentEditor, StickyCommentEditor>();
            container.Register <IUserWikiEditor, UserWikiEditor>();
        }
        public DB4Thing Post(string title, string text, string subredditName = "")
        {
            var subReddit = _subreddit;

            // Since DB4 posts to another subreddit for DeltaLogs and registering multiple interfaces
            // against the DI container is a bit of a pain, default to main subreddit, but switch
            // to DeltaLog subreddit if passed in
            if (!string.IsNullOrEmpty(subredditName))
            {
                subReddit = _reddit.GetSubredditAsync($"/r/{subredditName}").Result;
            }

            // Submit post
            var post = subReddit.SubmitTextPostAsync(title, text).Result;

            // The returned post has basically nothing on it - need to retrieve a post with GetPost
            var oAuthUrl = UrlHelper.ConvertToOAuth(post.Url.AbsoluteUri);
            var fullPost = _reddit.GetPostAsync(new Uri(oAuthUrl)).Result;

            return(RedditThingConverter.Convert(fullPost));
        }