geddit

package
v0.0.0-...-998b5eb Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2015 License: BSD-3-Clause, CC0-1.0 Imports: 12 Imported by: 0

README

geddit

a convenient abstraction for the reddit.com API in Go

Checkout the godoc page

warning

this is a work in progress!

example

package main

import (
	"fmt"

	"github.com/jzelinskie/geddit"
)

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

	// Set listing options
	subOpts := geddit.ListingOptions{
		Limit: 10,
	}

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

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

	// Get specific subreddit submissions, sorted by new
	submissions, _ = session.SubredditSubmissions("hockey", geddit.NewSubmissions, subOpts)

	// Print title and author of each submission
	for _, s := range submissions {
		fmt.Printf("Title: %s\nAuthor: %s\n\n", s.Title, s.Author)
	}

	// Upvote the first post
	session.Vote(submissions[0], geddit.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"
	ThisDay            = "day"
	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 ListingOptions

type ListingOptions struct {
	Time    string `url:"t,omitempty"`
	Limit   int    `url:"limit,omitempty"`
	After   string `url:"after,omitempty"`
	Before  string `url:"before,omitempty"`
	Count   int    `url:"count,omitempty"`
	Show    string `url:"show,omitempty"`
	Article string `url:"article,omitempty"`
}

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(sort popularitySort, params ListingOptions) ([]*Submission, error)

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

func (LoginSession) Listing

func (s LoginSession) Listing(username, listing string, sort popularitySort, after string) ([]*Submission, error)

Listing returns a listing for an user

func (LoginSession) Me

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

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

func (LoginSession) MyComments

func (s LoginSession) MyComments(sort popularitySort, after string) ([]*Submission, error)

Fetch the Comments listing for the logged-in user

func (LoginSession) MyDisliked

func (s LoginSession) MyDisliked(sort popularitySort, after string) ([]*Submission, error)

Fetch the Disliked listing for the logged-in user

func (LoginSession) MyGilded

func (s LoginSession) MyGilded(sort popularitySort, after string) ([]*Submission, error)

Fetch the Gilded listing for the logged-in user

func (LoginSession) MyHidden

func (s LoginSession) MyHidden(sort popularitySort, after string) ([]*Submission, error)

Fetch the Hidden listing for the logged-in user

func (LoginSession) MyLiked

func (s LoginSession) MyLiked(sort popularitySort, after string) ([]*Submission, error)

Fetch the Liked listing for the logged-in user

func (LoginSession) MyOverview

func (s LoginSession) MyOverview(sort popularitySort, after string) ([]*Submission, error)

Fetch the Overview listing for the logged-in user

func (LoginSession) MySaved

func (s LoginSession) MySaved(sort popularitySort, after string) ([]*Submission, error)

Fetch the Saved listing for the logged-in user

func (LoginSession) MySubmitted

func (s LoginSession) MySubmitted(sort popularitySort, after string) ([]*Submission, error)

Fetch the Submitted listing for 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(sort popularitySort, params ListingOptions) ([]*Submission, error)

DefaultFrontpage returns the submissions on the default reddit frontpage.

func (Session) SubredditSubmissions

func (s Session) SubredditSubmissions(subreddit string, sort popularitySort, params ListingOptions) ([]*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