private async Task StartReddit(CancellationToken cancellationToken)
        {
            _started = true;
            ListingStream <Comment>    stream   = _client.User.GetUsernameMentions(25).Stream();
            IAsyncEnumerator <Comment> listings = _client.User.GetUsernameMentions(25).GetEnumerator(25, -1, true);
            await listings.MoveNext();

            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    Comment comment = listings.Current;
                    await OnCommentReceived(comment).ConfigureAwait(false);
                }
                catch (Exception e)
                {
                    Logger.LogError(e.Message, e);
                }


                await listings.MoveNext();
            }
        }
Example #2
0
        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)
            });
        }