Esempio n. 1
0
        public async Task TestGetSubreddit()
        {
            RedditApi api       = new RedditApi();
            Subreddit subreddit = await api.GetSubredditAsync(TestSubredditName);

            Assert.Equal(TestSubredditName, subreddit.Name);
        }
Esempio n. 2
0
        public void CheckChannels(ITextChannel channel)
        {
            try
            {
                foreach (Subreddit sr in reddits)
                {
                    var sub   = api.GetSubredditAsync(sr.Name).Result;
                    var links = sub.GetNewLinksAsync(new ListingRequest {
                        Limit = 1
                    }).Result;
                    foreach (Link link in links.OfType <Link>())
                    {
                        Console.WriteLine("__" + link.Title + " " + link.Url + " " + link.Permalink);

                        if (sr.LastLink != null)
                        {
                            Console.WriteLine("__" + sr.LastLink);
                        }
                        if (sr.LastLink == null)
                        {
                            sr.LastLink = link.Url;
                        }
                        else if (!link.Url.ToLower().Equals(sr.LastLink.ToLower()))
                        {
                            DiscordUtil.SendMessage(channel, "New post in /r/" + sr.Name + "! https://www.reddit.com" + link.Permalink);
                            sr.LastLink = link.Url;
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
        }
Esempio n. 3
0
        public static async Task <List <Link> > HotNews(int limit, string after = null)
        {
            var redditService = new RedditApi();
            var subreddit     = await redditService.GetSubredditAsync("news");

            var listings =
                await subreddit.GetHotLinksAsync(new ListingRequest { Limit = limit, After = after });

            return(listings.Select((t, i) => (Link)listings.Children[i]).ToList());
        }
Esempio n. 4
0
        public async Task TestGetNextListing()
        {
            RedditApi api       = new RedditApi();
            Subreddit subreddit = await api.GetSubredditAsync(TestSubredditName);

            Listing listing = await subreddit.GetNewLinksAsync(new ListingRequest { Limit = 1 });

            Listing next = await listing.GetNextListingAsync(1);

            Assert.Equal(1, next.Count);
        }
Esempio n. 5
0
        public async Task Dankmeme()
        {
            try {
                RedditApi redditService = new RedditApi();
                Subreddit subreddit     = await redditService.GetSubredditAsync("dankmemes");

                RedditNet.Things.Link link = await subreddit.GetRandomLinkAsync();
                await ReplyAsync(link.Title + " - " + link.Url);
            } catch {
                await ReplyAsync("Sorry, I couldn't find a dank enough post!");
            }
        }
Esempio n. 6
0
        public async Task TestSubredditSearch()
        {
            RedditApi api       = new RedditApi();
            Subreddit subreddit = await api.GetSubredditAsync(TestSubredditName);

            Listing listing = await subreddit.SearchAsync(new SearchRequest
            {
                Sort  = SearchSort.Hot,
                Limit = 10
            });

            Assert.Equal(10, listing.Count);
        }
Esempio n. 7
0
        public async Task TestGetSubredditLinks()
        {
            RedditApi api       = new RedditApi();
            Subreddit subreddit = await api.GetSubredditAsync(TestSubredditName);

            Listing listing = await subreddit.GetNewLinksAsync(new ListingRequest { Limit = 5 });

            var result = listing.ToList();

            foreach (Thing thing in result)
            {
                Assert.IsAssignableFrom(typeof(Link), thing);
            }
        }
Esempio n. 8
0
        public async Task RandomPost([Summary("The subreddit to look for a random post")] string rsubreddit)
        {
            try {
                string filtered = Regex.Replace(rsubreddit, "/?r/", "");

                RedditApi redditService = new RedditApi();
                Subreddit subreddit     = await redditService.GetSubredditAsync(filtered);

                RedditNet.Things.Link link = await subreddit.GetRandomLinkAsync();
                await ReplyAsync(link.Title + " - " + link.Url);
            } catch {
                await ReplyAsync("Sorry, I couldn't find a random post in that subreddit!");
            }
        }
Esempio n. 9
0
        public async Task <string> PostUrl(string title, string url, string subreddit)
        {
            PrepareClient();

            Subreddit sub = await RedditApi.GetSubredditAsync(subreddit);

            try
            {
                Post post = await sub.SubmitPostAsync(title, url);

                return(new Uri(new Uri("https://reddit.com/"), post.Permalink).ToString());
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine("reddit post fail: " + e.ToString());
                throw e;
            }
        }
Esempio n. 10
0
        public async Task Dankmeme()
        {
            try
            {
                var redditService = new RedditApi();
                var subreddit     = await redditService.GetSubredditAsync("dankmemes");

                var link = await subreddit.GetRandomLinkAsync();
                await ReplyAsync(link.Title + " - " + link.Url);

                ConsoleHelper.Log($"{Helper.GetName(Context.User)} requested a dank meme.", LogSeverity.Info);
            } catch (Exception ex)
            {
                ConsoleHelper.Log($"Error getting random meme from /r/dankmemes! ({ex.Message})",
                                  LogSeverity.Error);
                await ReplyAsync("Sorry, I couldn't find a dank enough post!");
            }
        }
Esempio n. 11
0
        public async Task RandomPost([Summary("The subreddit to look for a random post")]
                                     string rsubreddit)
        {
            try
            {
                string filtered = Regex.Replace(rsubreddit, "/?r/", "");

                var redditService = new RedditApi();
                var subreddit     = await redditService.GetSubredditAsync(filtered);

                var link = await subreddit.GetRandomLinkAsync();
                await ReplyAsync(link.Title + " - " + link.Url);

                ConsoleHelper.Log($"{Helper.GetName(Context.User)} requested a random link from /r/{filtered}.",
                                  LogSeverity.Info);
            } catch (Exception ex)
            {
                ConsoleHelper.Log($"Error getting random link from {rsubreddit}! ({ex.Message})",
                                  LogSeverity.Error);
                await ReplyAsync("Sorry, I couldn't find a random post in that subreddit!");
            }
        }
Esempio n. 12
0
        static void Main(string[] args)
        {
            var api       = new RedditApi();
            var subreddit = api.GetSubredditAsync("noveltranslations").GetAwaiter().GetResult();
            var links     = subreddit.GetHotLinksAsync(new ListingRequest {
                Limit = 2
            }).Result;

            foreach (Link link in links.OfType <Link>())
            {
                Console.WriteLine($"Link - {link.Title}");

                var comments = link.GetCommentsAsync(new GetCommentsRequest {
                    Limit = 5
                }).GetAwaiter().GetResult();
                foreach (Comment comment in comments.OfType <Comment>())
                {
                    Console.WriteLine($"- Comment - \"{comment.Body}\" by {comment.Author}");
                }

                More more = comments.LastOrDefault() as More;

                var someMoreComments = more?.Children.Take(2) ?? new List <string>();
                foreach (string anotherComment in someMoreComments)
                {
                    Comment aComment = subreddit.GetCommentAsync(new GetCommentRequest {
                        LinkId = link.Id, CommentId = anotherComment
                    })
                                       .GetAwaiter().GetResult();

                    Console.WriteLine($"-- Another comment {aComment.Body}");
                }
            }

            Console.WriteLine();
            Console.WriteLine("Done!");
            Console.ReadLine();
        }