reddit

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 26, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CountComments added in v1.0.1

func CountComments(comments []*Comment) int

CountComments counts all non-"more" comments in a tree.

func EstimateTokens added in v1.0.1

func EstimateTokens(comments []*Comment) int

EstimateTokens estimates token count for a comment tree (~4 chars per token).

func TestCredentials added in v1.0.1

func TestCredentials(clientID, clientSecret string) error

TestCredentials verifies that client ID and secret can obtain an OAuth token.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client handles all Reddit API communication.

func NewClient

func NewClient() *Client

NewClient creates a new Reddit API client. Automatically loads OAuth credentials if available (~/.config/lurk/credentials.json).

func (*Client) ExpandThread added in v1.0.1

func (c *Client) ExpandThread(thread *Thread, noCache bool)

ExpandThread recursively expands "more" placeholders (up to 10 passes).

func (*Client) Fetch

func (c *Client) Fetch(path string, noCache bool) ([]byte, error)

Fetch makes a GET request to the Reddit JSON API with retry, rate limiting, and caching.

func (*Client) FetchMoreChildren

func (c *Client) FetchMoreChildren(linkID string, childrenIDs []string, noCache bool) ([]*Comment, error)

FetchMoreChildren fetches collapsed comment threads using Reddit's /api/morechildren endpoint. The response format differs from normal comments: it returns contentText, parent, and author/score embedded in HTML content rather than structured JSON fields.

func (*Client) FetchPost

func (c *Client) FetchPost(path string, formData url.Values, noCache bool) ([]byte, error)

FetchPost makes a POST request to the Reddit JSON API with retry, rate limiting, and caching.

func (*Client) GetSubreddit

func (c *Client) GetSubreddit(name string, sort SortOrder, limit int, timeFilter TimeFilter, after string, noCache bool) ([]*Post, string, error)

GetSubreddit fetches posts from a subreddit with sorting and pagination. Returns the list of posts, the "after" token for the next page, and any error.

func (*Client) GetSubredditInfo

func (c *Client) GetSubredditInfo(name string, noCache bool) (*SubredditInfo, error)

GetSubredditInfo fetches metadata about a subreddit.

func (*Client) GetThread

func (c *Client) GetThread(permalink string, noCache bool) (*Thread, error)

GetThread fetches a Reddit thread (post + comments) by permalink. Accepts full URLs like https://www.reddit.com/r/sub/comments/id/title/ or just the path.

func (*Client) GetThreadShallow added in v1.0.1

func (c *Client) GetThreadShallow(permalink string, noCache bool) (*Thread, error)

GetThreadShallow fetches a thread without expanding collapsed comments.

func (*Client) GetUser

func (c *Client) GetUser(username string, limit int, noCache bool) (*UserInfo, []*Post, []*Comment, error)

GetUser fetches a user's profile info and recent activity (posts + comments).

func (*Client) IsAuthenticated added in v1.0.1

func (c *Client) IsAuthenticated() bool

IsAuthenticated returns true if OAuth credentials are configured.

func (*Client) Search

func (c *Client) Search(query string, sub string, sort SortOrder, limit int, timeFilter TimeFilter, after string, noCache bool) ([]*Post, string, error)

Search performs a Reddit search, optionally scoped to a subreddit. Supports comma-separated subreddit names for parallel multi-sub search.

type Comment

type Comment struct {
	ID        string
	ParentID  string // parent comment/post ID (used by morechildren insertion)
	Author    string
	Body      string
	Score     int
	Created   time.Time
	Depth     int
	Stickied  bool
	Replies   []*Comment
	IsMore    bool     // true if this represents a "load more" placeholder
	MoreIDs   []string // IDs of collapsed comments
	MoreCount int
}

Comment represents a parsed Reddit comment.

func TopCommentsByScore added in v1.0.1

func TopCommentsByScore(comments []*Comment, limit int) []*Comment

TopCommentsByScore flattens the comment tree and returns the top N by score.

type Credentials added in v1.0.1

type Credentials struct {
	ClientID     string `json:"client_id"`
	ClientSecret string `json:"client_secret"`
}

Credentials holds Reddit OAuth app credentials.

func LoadCredentials added in v1.0.1

func LoadCredentials() (Credentials, error)

LoadCredentials reads OAuth credentials. Priority: env vars (LURK_CLIENT_ID/LURK_CLIENT_SECRET) > config file (~/.config/lurk/credentials.json).

type Listing

type Listing struct {
	Kind string      `json:"kind"`
	Data ListingData `json:"data"`
}

Listing is the Reddit API envelope for paginated results.

func ParseListing

func ParseListing(data []byte) (*Listing, error)

ParseListing decodes a JSON response into a Listing.

type ListingData

type ListingData struct {
	After    string  `json:"after"`
	Before   string  `json:"before"`
	Children []Thing `json:"children"`
}

type Post

type Post struct {
	ID              string
	Subreddit       string
	Author          string
	Title           string
	SelfText        string
	Score           int
	UpvoteRatio     float64
	NumComments     int
	URL             string
	Permalink       string
	Domain          string
	Created         time.Time
	IsVideo         bool
	IsSelf          bool
	Over18          bool
	Stickied        bool
	LinkFlairText   string
	CrosspostParent string
	MediaURL        string
}

Post represents a parsed Reddit post.

func ParsePost

func ParsePost(data map[string]any) *Post

ParsePost extracts a Post from a Thing's data map.

type SearchResult

type SearchResult struct {
	Post *Post
}

SearchResult wraps a post found via search with its source subreddit.

type SortOrder

type SortOrder string

SortOrder for subreddit/search listings.

const (
	SortHot           SortOrder = "hot"
	SortNew           SortOrder = "new"
	SortTop           SortOrder = "top"
	SortRising        SortOrder = "rising"
	SortControversial SortOrder = "controversial"
	SortRelevance     SortOrder = "relevance"
	SortComments      SortOrder = "comments"
)

type SubredditInfo

type SubredditInfo struct {
	Name          string
	Title         string
	Description   string
	Subscribers   int
	ActiveUsers   int
	Created       time.Time
	Over18        bool
	SubredditType string // public, private, restricted
}

SubredditInfo holds metadata about a subreddit.

type Thing

type Thing struct {
	Kind string         `json:"kind"`
	Data map[string]any `json:"data"`
}

Thing wraps every Reddit object (post, comment, more, etc.)

type Thread

type Thread struct {
	Post     *Post
	Comments []*Comment
}

Thread is a post with its full comment tree.

type TimeFilter

type TimeFilter string

TimeFilter for top/controversial sorting.

const (
	TimeHour  TimeFilter = "hour"
	TimeDay   TimeFilter = "day"
	TimeWeek  TimeFilter = "week"
	TimeMonth TimeFilter = "month"
	TimeYear  TimeFilter = "year"
	TimeAll   TimeFilter = "all"
)

type UserInfo

type UserInfo struct {
	Name         string
	Created      time.Time
	LinkKarma    int
	CommentKarma int
	TotalKarma   int
	IsSuspended  bool
}

UserInfo holds metadata about a user.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL