From 465e96353c248a433a0bb01f5b497851b77fb2b1 Mon Sep 17 00:00:00 2001 From: Vartan Benohanian Date: Mon, 20 Jul 2020 21:03:57 -0400 Subject: [PATCH] Add methods to get random posts and subreddits Signed-off-by: Vartan Benohanian --- geddit.go | 15 ++++++++++++++ post.go | 32 ++++++++++++++++++++++++++++++ subreddit.go | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ things.go | 2 +- 4 files changed, 104 insertions(+), 1 deletion(-) diff --git a/geddit.go b/geddit.go index 222c685..cb94e18 100644 --- a/geddit.go +++ b/geddit.go @@ -125,6 +125,21 @@ func newClient(httpClient *http.Client) *Client { httpClient = &http.Client{} } + // todo... + // getting a random + httpClient.CheckRedirect = func(req *http.Request, via []*http.Request) error { + redirectURL := req.URL.String() + redirectURL = strings.Replace(redirectURL, "https://www.reddit.com", defaultBaseURL, 1) + + reqURL, err := url.Parse(redirectURL) + if err != nil { + return err + } + req.URL = reqURL + + return nil + } + baseURL, _ := url.Parse(defaultBaseURL) tokenURL, _ := url.Parse(defaultTokenURL) diff --git a/post.go b/post.go index 73e67cf..a99f725 100644 --- a/post.go +++ b/post.go @@ -501,3 +501,35 @@ func addCommentToReplies(parent *Comment, comment *Comment) { addCommentToReplies(reply, comment) } } + +// RandomFromSubreddits returns a random post and its comments from the subreddits. +// If no subreddits are provided, it will run the query against your subscriptions. +func (s *PostService) RandomFromSubreddits(ctx context.Context, subreddits ...string) (*Post, []*Comment, *Response, error) { + path := "random" + if len(subreddits) > 0 { + path = fmt.Sprintf("r/%s/random", strings.Join(subreddits, "+")) + } + + req, err := s.client.NewRequest(http.MethodGet, path, nil) + if err != nil { + return nil, nil, nil, err + } + + root := new(postAndComments) + resp, err := s.client.Do(ctx, req, root) + if err != nil { + return nil, nil, resp, err + } + + return root.Post, root.Comments, resp, nil +} + +// Random returns a random post and its comments from all of Reddit. +func (s *PostService) Random(ctx context.Context) (*Post, []*Comment, *Response, error) { + return s.RandomFromSubreddits(ctx, "all") +} + +// RandomFromSubscriptions returns a random post and its comments from your subscriptions. +func (s *PostService) RandomFromSubscriptions(ctx context.Context) (*Post, []*Comment, *Response, error) { + return s.RandomFromSubreddits(ctx) +} diff --git a/subreddit.go b/subreddit.go index 18ef6fc..0767fb4 100644 --- a/subreddit.go +++ b/subreddit.go @@ -364,3 +364,59 @@ func (s *SubredditService) Moderators(ctx context.Context, subreddit string) (in return root.Data.Moderators, resp, nil } + +// todo: sr_detail's NSFW indicator is over_18 instead of over18 +func (s *SubredditService) random(ctx context.Context, nsfw bool) (*Subreddit, *Response, error) { + path := "r/random" + if nsfw { + path = "r/randnsfw" + } + + type query struct { + ExpandSubreddit bool `url:"sr_detail"` + Limit int `url:"limit,omitempty"` + } + + path, err := addOptions(path, query{true, 1}) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest(http.MethodGet, path, nil) + if err != nil { + return nil, nil, err + } + + type rootResponse struct { + Data struct { + Children []struct { + Data struct { + Subreddit *Subreddit `json:"sr_detail"` + } `json:"data"` + } `json:"children"` + } `json:"data"` + } + + root := new(rootResponse) + resp, err := s.client.Do(ctx, req, root) + if err != nil { + return nil, resp, err + } + + var sr *Subreddit + if len(root.Data.Children) > 0 { + sr = root.Data.Children[0].Data.Subreddit + } + + return sr, resp, nil +} + +// Random returns a random SFW subreddit. +func (s *SubredditService) Random(ctx context.Context) (*Subreddit, *Response, error) { + return s.random(ctx, false) +} + +// RandomNSFW returns a random NSFW subreddit. +func (s *SubredditService) RandomNSFW(ctx context.Context) (*Subreddit, *Response, error) { + return s.random(ctx, true) +} diff --git a/things.go b/things.go index c64835d..16b70ab 100644 --- a/things.go +++ b/things.go @@ -285,7 +285,7 @@ func (r *Replies) UnmarshalJSON(data []byte) error { return nil } -// todo: should we implemented json.Marshaler? +// todo: should we implement json.Marshaler? // More holds information type More struct {