reddit

package module
v0.0.0-...-9b7cc90 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2014 License: BSD-3-Clause Imports: 11 Imported by: 0

README

reddit

a convenient abstraction for the reddit.com API

Checkout the godoc page

warning

this is a work in progress!

example

package main

import (
	"fmt"

	"github.com/jzelinskie/reddit"
)

// Please don't handle errors this way.
func main() {
	// Login to reddit
	session, _ := reddit.NewLoginSession(
		"novelty_account",
		"password",
		"golang reddit example",
	)

	// Get reddit's default frontpage
	submissions, _ := session.DefaultFrontpage()

	// Get our own personal frontpage
	submissions, _ = session.Frontpage()

	// Upvote the first post
	session.Vote(submissions[0], reddit.UpVote)
}

Documentation

Overview

Package reddit implements an abstraction for the reddit.com API.

Index

Constants

View Source
const (
	UpVote     vote = "1"
	DownVote        = "-1"
	RemoveVote      = "0"
)
View Source
const (
	DefaultPopularity        popularitySort = ""
	HotSubmissions                          = "hot"
	NewSubmissions                          = "new"
	RisingSubmissions                       = "rising"
	TopSubmissions                          = "top"
	ControversialSubmissions                = "controversial"
)
View Source
const (
	DefaultAge ageSort = ""
	ThisHour           = "hour"
	ThisMonth          = "month"
	ThisYear           = "year"
	AllTime            = "all"
)

Variables

This section is empty.

Functions

func NewLinkSubmission

func NewLinkSubmission(sr, title, link string, replies bool, c *Captcha) *newSubmission

NewLinkSubmission returns a newSubmission with parameters appropriate for a link submission

func NewTextSubmission

func NewTextSubmission(sr, title, text string, replies bool, c *Captcha) *newSubmission

NewTextSubmission returns a newSubmission with parameters appropriate for a text submission

Types

type Captcha

type Captcha struct {
	Iden     string
	Response string
}

type Comment

type Comment struct {
	Author              string  //`json:"author"`
	Body                string  //`json:"body"`
	BodyHTML            string  //`json:"body_html"`
	Subreddit           string  //`json:"subreddit"`
	LinkID              string  //`json:"link_id"`
	ParentID            string  //`json:"parent_id"`
	SubredditID         string  //`json:"subreddit_id"`
	FullID              string  //`json:"name"`
	UpVotes             float64 //`json:"ups"`
	DownVotes           float64 //`json:"downs"`
	Created             float64 //`json:"created_utc"`
	Edited              bool    //`json:"edited"`
	BannedBy            *string //`json:"banned_by"`
	ApprovedBy          *string //`json:"approved_by"`
	AuthorFlairTxt      *string //`json:"author_flair_text"`
	AuthorFlairCSSClass *string //`json:"author_flair_css_class"`
	NumReports          *int    //`json:"num_reports"`
	Likes               *int    //`json:"likes"`
	Replies             []*Comment
}

Comment represents a reddit comment.

func (Comment) String

func (c Comment) String() string

type Deleter

type Deleter interface {
	// contains filtered or unexported methods
}

Deleter represents something that can be deleted on reddit.com.

type LoginSession

type LoginSession struct {
	Session
	// contains filtered or unexported fields
}

LoginSession represents an HTTP session with reddit.com -- all authenticated API calls are methods bound to this type.

func NewLoginSession

func NewLoginSession(username, password, useragent string) (*LoginSession, error)

NewLoginSession creates a new session for those who want to log into a reddit account.

func (LoginSession) Clear

func (s LoginSession) Clear() error

Clear clears all session cookies and updates the current session with a new one.

func (LoginSession) Delete

func (s LoginSession) Delete(d Deleter) error

Delete deletes a Submission or Comment.

func (LoginSession) Frontpage

func (s LoginSession) Frontpage() ([]*Submission, error)

Frontpage returns the submissions on the logged-in user's personal frontpage.

func (LoginSession) Me

func (s LoginSession) Me() (*Redditor, error)

Me returns an up-to-date redditor object of the logged-in user.

func (LoginSession) NeedsCaptcha

func (s LoginSession) NeedsCaptcha() (bool, error)

NeedsCaptcha returns true if captcha is required, false if it isn't

func (LoginSession) NewCaptchaIden

func (s LoginSession) NewCaptchaIden() (string, error)

NewCaptchaIden gets a new captcha iden from reddit

func (LoginSession) Reply

func (s LoginSession) Reply(r Replier, comment string) error

Reply posts a comment as a response to a Submission or Comment.

func (LoginSession) Submit

func (s LoginSession) Submit(ns *newSubmission) error

func (LoginSession) Vote

func (s LoginSession) Vote(v Voter, vote vote) error

Vote either votes or rescinds a vote for a Submission or Comment.

type Redditor

type Redditor struct {
	ID           string  `json:"id"`
	Name         string  `json:"name"`
	LinkKarma    int     `json:"link_karma"`
	CommentKarma int     `json:"comment_karma"`
	Created      float32 `json:"created_utc"`
	Gold         bool    `json:"is_gold"`
	Mod          bool    `json:"is_mod"`
	Mail         *bool   `json:"has_mail"`
	ModMail      *bool   `json:"has_mod_mail"`
}

func (*Redditor) String

func (r *Redditor) String() string

String returns the string representation of a reddit user.

type Replier

type Replier interface {
	// contains filtered or unexported methods
}

Replier represents something that can be replied to on reddit.com.

type Session

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

Session represents an HTTP session with reddit.com without logging into an account.

func NewSession

func NewSession(useragent string) *Session

NewSession creates a new unauthenticated session to reddit.com.

func (Session) AboutRedditor

func (s Session) AboutRedditor(username string) (*Redditor, error)

AboutRedditor returns a Redditor for the given username.

func (Session) AboutSubreddit

func (s Session) AboutSubreddit(subreddit string) (*Subreddit, error)

AboutSubreddit returns a subreddit for the given subreddit name.

func (Session) CaptchaImage

func (s Session) CaptchaImage(iden string) (image.Image, error)

CaptchaImage gets the png corresponding to the captcha iden and decodes it

func (Session) Comments

func (s Session) Comments(h *Submission) ([]*Comment, error)

Comments returns the comments for a given Submission.

func (Session) DefaultFrontpage

func (s Session) DefaultFrontpage() ([]*Submission, error)

DefaultFrontpage returns the submissions on the default reddit frontpage.

func (Session) SortedSubmissions

func (s Session) SortedSubmissions(subreddit string, popularity popularitySort, age ageSort) ([]*Submission, error)

SortedSubmissions will return submissions from a subreddit (or homepage if "") by popularity and age TODO Review this

func (Session) SubredditSubmissions

func (s Session) SubredditSubmissions(subreddit string) ([]*Submission, error)

SubredditSubmissions returns the submissions on the given subreddit.

type Submission

type Submission struct {
	Author       string  `json:"author"`
	Title        string  `json:"title"`
	URL          string  `json:"url"`
	Domain       string  `json:"domain"`
	Subreddit    string  `json:"subreddit"`
	SubredditID  string  `json:"subreddit_id"`
	FullID       string  `json:"name"`
	ID           string  `json:"id"`
	Permalink    string  `json:"permalink"`
	Selftext     string  `json:"selftext"`
	ThumbnailURL string  `json:"thumbnail"`
	DateCreated  float32 `json:"created_utc"`
	NumComments  int     `json:"num_comments"`
	Score        int     `json:"score"`
	Ups          int     `json:"ups"`
	Downs        int     `json:"downs"`
	IsNSFW       bool    `json:"over_18"`
	IsSelf       bool    `json:"is_self"`
	WasClicked   bool    `json:"clicked"`
	IsSaved      bool    `json:"saved"`
	BannedBy     *string `json:"banned_by"`
}

Submission represents an individual post from the perspective of a subreddit. Remember to check for nil pointers before using any pointer fields.

func (h *Submission) FullPermalink() string

FullPermalink returns the full URL of a submission.

func (*Submission) String

func (h *Submission) String() string

String returns the string representation of a submission.

type Subreddit

type Subreddit struct {
	Name        string  `json:"display_name"`
	Title       string  `json:"title"`
	Desc        string  `json:"description"`
	PublicDesc  string  `json:"public_description"`
	URL         string  `json:"url"`
	FullID      string  `json:"name"`
	ID          string  `json:"id"`
	HeaderImg   string  `json:"header_img"`
	DateCreated float32 `json:"created_utc"`
	NumSubs     int     `json:"subscribers"`
	IsNSFW      bool    `json:"over18"`
}

Subreddit represents a subreddit from reddit.com.

func (*Subreddit) String

func (s *Subreddit) String() string

String returns the string representation of a subreddit.

type Voter

type Voter interface {
	// contains filtered or unexported methods
}

Voter represents something that can be voted on reddit.com.

Jump to

Keyboard shortcuts

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