added nsfw flag to search functions

This commit is contained in:
2026-05-26 01:50:49 -05:00
parent 68b11ef35c
commit c959c91d0d
3 changed files with 10 additions and 17 deletions
+4 -15
View File
@@ -2,7 +2,6 @@ package main
import ( import (
"context" "context"
"encoding/json"
"log/slog" "log/slog"
"os" "os"
@@ -29,25 +28,15 @@ func run(logger *slog.Logger) error {
if err != nil { if err != nil {
return err return err
} }
subs, _, err := client.Subreddit.Search(ctx, "hentai", nil) subs, _, err := client.Subreddit.Search(ctx, "hentai", &reddit.ListSubredditOptions{IncludeNsfw: true})
if err != nil { if err != nil {
return err return err
} }
logger.Info("Searching subreddits successful", "subreddits", subs) logger.Info("Searching subreddits successful", "count", len(subs))
posts, _, err := client.Subreddit.SearchPosts(ctx, "psylocke", "hentai", nil) posts, _, err := client.Subreddit.SearchPosts(ctx, "psylocke", "hentai", &reddit.ListPostSearchOptions{IncludeNsfw: true})
if err != nil { if err != nil {
return err return err
} }
logger.Info("Searching posts successful", "posts", posts, "count", len(posts)) logger.Info("Searching posts successful", "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 return nil
} }
+4
View File
@@ -558,6 +558,8 @@ type ListSubredditOptions struct {
ListOptions ListOptions
// One of: relevance, activity. // One of: relevance, activity.
Sort string `url:"sort,omitempty"` Sort string `url:"sort,omitempty"`
IncludeNsfw bool `url:"include_over_18,omitempty"`
} }
// ListPostOptions defines possible options used when getting posts from a subreddit. // ListPostOptions defines possible options used when getting posts from a subreddit.
@@ -572,6 +574,8 @@ type ListPostSearchOptions struct {
ListPostOptions ListPostOptions
// One of: relevance, hot, top, new, comments. // One of: relevance, hot, top, new, comments.
Sort string `url:"sort,omitempty"` Sort string `url:"sort,omitempty"`
IncludeNsfw bool `url:"include_over_18,omitempty"`
} }
// ListUserOverviewOptions defines possible options used when getting a user's post and/or comments. // ListUserOverviewOptions defines possible options used when getting a user's post and/or comments.
+2 -2
View File
@@ -499,7 +499,7 @@ func (s *SubredditService) Unfavorite(ctx context.Context, subreddit string) (*R
// Search for subreddits. // Search for subreddits.
func (s *SubredditService) Search(ctx context.Context, query string, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) { func (s *SubredditService) Search(ctx context.Context, query string, opts *ListSubredditOptions) ([]*Subreddit, *Response, error) {
path := fmt.Sprintf("subreddits/search?q=%s&include_over_18=on", query) path := fmt.Sprintf("subreddits/search?q=%s", query)
l, resp, err := s.client.getListing(ctx, path, opts) l, resp, err := s.client.getListing(ctx, path, opts)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err
@@ -533,7 +533,7 @@ func (s *SubredditService) SearchPosts(ctx context.Context, query string, subred
subreddit = "all" subreddit = "all"
} }
path := fmt.Sprintf("r/%s/search?include_over_18=on", subreddit) path := fmt.Sprintf("r/%s/search", subreddit)
path, err := addOptions(path, opts) path, err := addOptions(path, opts)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err