Add methods to get random posts and subreddits
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
This commit is contained in:
parent
5c376a1af4
commit
465e96353c
15
geddit.go
15
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)
|
||||
|
||||
|
32
post.go
32
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)
|
||||
}
|
||||
|
56
subreddit.go
56
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)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user