goreddit

package module
v0.0.0-...-7de8563 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2013 License: GPL-3.0 Imports: 9 Imported by: 0

README

GoReddit

Reddit API client for Go
LICENSE

Currently, the following things are implemented:

  • Logging in
  • Getting submissions from a subreddit
  • Getting comments for a thing
  • Voting

GoReddit makes every effort to mirror the JSON output it receives from the Reddit API into native golang types and structs.

Usage examples

Creating a client
import "goreddit" // https://github.com/kz26/GoReddit

client := goreddit.NewClient("MyBot/1.0")

Each client instance is thread-safe (can be called by multiple goroutines) and respects Reddit's guideline of no more than one request every 2 seconds.

Getting submissions from a subreddit

listing, err := rc.GetSubreddit("news", "new", 25) // Get new submissions from r/news, limit 25
for _, link := range listing {
	fmt.Println(link.Data.Title)
}
Voting
err := rc.Vote("t3_XXXXXX", 1) // Upvote the thing with the given ID
if err == nil {
	fmt.Println("Upvoted")
} else {
	fmt.Println("Error")
}

Documentation

Index

Constants

View Source
const DELAY_S = 2 * time.Second
View Source
const REDDIT_HTTPS_URL = "https://ssl.reddit.com"
View Source
const REDDIT_URL = "http://www.reddit.com"

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client represents a custom Reddit client that respects the Reddit API rate limit guidelines

func NewClient

func NewClient(userAgent string) *Client

Create a new client with the given user agent

func (*Client) GetComments

func (c *Client) GetComments(id string, sort string, limit int) ([]Comment, error)

Get comments for a specific thing ID TODO: implement sorting

func (*Client) GetSubreddit

func (c *Client) GetSubreddit(sr string, sort string, limit int) ([]Link, error)

Get posts in a subreddit TODO: implement sorting

func (*Client) Login

func (c *Client) Login(user string, passwd string) error

Perform a login using the given username and password

func (*Client) Vote

func (c *Client) Vote(id string, dir int) error

Vote on a thing id: thing id, dir: 1, 0, -1 for upvote, null vote, and downvote, respectively

type Comment

type Comment struct {
	Kind string
	Data struct {
		Votable
		Created

		Author    string
		Body      string
		Body_html string
		Id        string
		Name      string
		Permalink string
		Replies   json.RawMessage
		Score     int
		Title     string
	}
}

A comment

func GetCommentsFlat

func GetCommentsFlat(comList []Comment) []Comment

Returns a flattened list of comments and replies

func (*Comment) GetReplies

func (com *Comment) GetReplies() ([]Comment, error)

type CommentListing

type CommentListing struct {
	Data struct {
		Modhash  string
		Children []Comment
		After    string
		Before   string
	}
	Kind string
}

A listing of comments

type Created

type Created struct {
	Created     float64
	Created_utc float64
}

Fields for things that implement Created

type Link struct {
	Kind string
	Data struct {
		Votable
		Created

		Domain       string
		Hidden       bool
		Id           string
		Is_self      bool
		Name         string
		Num_comments int
		Over_18      bool
		Permalink    string
		Score        int
		Title        string
	}
}

A link

type LinkListing

type LinkListing struct {
	Data struct {
		Modhash  string
		Children []Link
		After    string
		Before   string
	}
	Kind string
}

A listing of links

type Votable

type Votable struct {
	Downs int
	Ups   int
	Likes json.RawMessage // can't use bool here since Reddit also uses null; just check the raw string value if you need this field
}

Fields for things that implement Votable

Jump to

Keyboard shortcuts

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