added nsfw results for search functions
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"os"
|
||||
|
||||
"github.com/vartanbeno/go-reddit/v2/reddit"
|
||||
)
|
||||
|
||||
type Post struct {
|
||||
reddit.Post
|
||||
}
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
func main() {
|
||||
handler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelDebug})
|
||||
logger := slog.New(handler)
|
||||
slog.SetDefault(logger)
|
||||
if err := run(logger); err != nil {
|
||||
logger.Error("Failed", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func run(logger *slog.Logger) error {
|
||||
client, err := reddit.NewReadonlyClient()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
subs, _, err := client.Subreddit.Search(ctx, "hentai", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("Searching subreddits successful", "subreddits", subs)
|
||||
posts, _, err := client.Subreddit.SearchPosts(ctx, "psylocke", "hentai", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("Searching posts successful", "posts", posts, "count", len(posts))
|
||||
data, err := json.Marshal(posts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var new_posts []*Post
|
||||
err = json.Unmarshal(data, &new_posts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("Unmarshalled post subclass", "posts", new_posts)
|
||||
return nil
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"reflect"
|
||||
@@ -348,6 +349,7 @@ func parseRate(r *http.Response) Rate {
|
||||
// pointed to by v, or returned as an error if an API error has occurred. If v implements the io.Writer interface,
|
||||
// the raw response will be written to v, without attempting to decode it.
|
||||
func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*Response, error) {
|
||||
slog.Debug("Making API request to reddit", "url", req.URL.String(), "method", req.Method)
|
||||
if err := c.checkRateLimitBeforeDo(req); err != nil {
|
||||
return &Response{
|
||||
Response: err.Response,
|
||||
|
||||
+2
-2
@@ -499,7 +499,7 @@ func (s *SubredditService) Unfavorite(ctx context.Context, subreddit string) (*R
|
||||
|
||||
// Search for subreddits.
|
||||
func (s *SubredditService) Search(ctx context.Context, query string, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) {
|
||||
path := fmt.Sprintf("subreddits/search?q=%s", query)
|
||||
path := fmt.Sprintf("subreddits/search?q=%s&include_over_18=on", query)
|
||||
l, resp, err := s.client.getListing(ctx, path, opts)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
@@ -533,7 +533,7 @@ func (s *SubredditService) SearchPosts(ctx context.Context, query string, subred
|
||||
subreddit = "all"
|
||||
}
|
||||
|
||||
path := fmt.Sprintf("r/%s/search", subreddit)
|
||||
path := fmt.Sprintf("r/%s/search?include_over_18=on", subreddit)
|
||||
path, err := addOptions(path, opts)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
|
||||
Reference in New Issue
Block a user