grape

package module
v0.0.0-...-59a85a6 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2013 License: MIT Imports: 14 Imported by: 0

README

grape

grape is a Go wrapper for the Reddit API. It will provide functionality for all aspects of the API for use in the creation of bots.

The current functionality is limited to the following:

  • Logging in
  • Retrieving information about any user
  • Retrieving information about the currently logged in user
  • Retrieving a subreddit (limited to front page only)
  • Retrieving sorted subreddits (by new, hot and top)
  • Retrieving comments from a link
  • Submitting a link from the currently logged in user to a subreddit
  • Submitting comments
  • Getting all mail (inbox as well as unread)
  • Cached responses from reddit and a priority system in preparation for prefetching of data

Immediate TODO:

  • Prefetching of extra information
  • Replying to private messages
  • Develop a way of incorporating OAuth

Example Code:

package main 

import (
  "github.com/Stringy/grape"
  "fmt"
)

func main() {
  user, err := grape.Login("username", "password", true)
  if err != nil {
    // handle error
  } 
  sub, err := grape.GetSubreddit("learnprogramming")
  if err != nil {
    // handle error
  }
  for i, item := range sub.Items {
    fmt.Println(item.Submission)
  }
}

Documentation

Overview

Package grape (Go Reddit Api PackagE) is an all encompassing Reddit API wrapper

This library is currently under active development and the functionality is growing rapidly.

Index

Constants

View Source
const (
	CommentPrefix   = "t1_"
	AccountPrefix   = "t2_"
	LinkPrefix      = "t3_"
	MessagePrefix   = "t4_"
	SubredditPrefix = "t5_"
	AwardPrefix     = "t6_"
	PromoCampaign   = "t8_"
)

reddit Id prefixes

View Source
const (
	Hot         sort = "hot"
	Top         sort = "top"
	New         sort = "new"
	Cont        sort = "controversial"
	Conf        sort = "confidence"
	DefaultSort sort = "subreddit"
)

Listing sort constants

View Source
const (
	Hour          period = "hour"
	Day           period = "day"
	Week          period = "week"
	Month         period = "month"
	Year          period = "year"
	All           period = "all"
	DefaultPeriod period = ""
)

time period constants

View Source
const (
	Banuser               = "banuser"
	Unbanuser             = "unbanuser"
	Removelink            = "removelink"
	Approvelink           = "approvelink"
	Removecomment         = "removecomment"
	Approvecomment        = "approvecomment"
	Addmoderator          = "addmoderator"
	Invitemoderator       = "invitemoderator"
	Uninvitemoderator     = "uninvitemoderator"
	Acceptmoderatorinvite = "acceptmoderatorinvite"
	Removemoderator       = "removemoderator"
	Addcontributor        = "addcontributor"
	Removecontributor     = "removecontributor"
	Editsettings          = "editsettings"
	Editflair             = "editflair"
	Distinguish           = "distinguish"
	Marknsfw              = "marknsfw"
	Wikibanned            = "wikibanned"
	Wikicontributor       = "wikicontributor"
	Wikiunbanned          = "wikiunbanned"
	Removewikicontributor = "removewikicontributor"
	Wikirevise            = "wikirevise"
	Wikipermlevel         = "wikipermlevel"
	Ignorereports         = "ignorereports"
	Unignorereports       = "unignorereports"
	Setpermissions        = "setpermissions"
	Sticky                = "sticky"
	Unsticky              = "unsticky"
)

Moderator Actions

Variables

View Source
var Config = new(cfg)

Configuration structures

Functions

This section is empty.

Types

type Comment

type Comment struct {
	Author          string
	Body            string
	ScoreHidden     bool
	Ups             int
	Downs           int
	ApprovedBy      string `json:"approved_by"`
	AuthorFlairCSS  string `json:"author_flair_css_class"`
	AuthorFlairText string `json:"author_flair_text"`
	BannedBy        string `json:"banned_by"`
	Edited          time.Time
	Gilded          int
	Likes           bool
	LinkId          string `json:"link_id"`
	LinkTitle       string `json:"link_title"`
	NumReports      int    `json:"num_reports"`
	ParentId        string `json:"parent_id"`
	Sub             string `json:"subreddit"`
	SubId           string `json:"subreddit_id"`
	Distinguished   string
	Replies         []Comment
	*Thing
}

func (*Comment) Edit

func (c *Comment) Edit(user *Redditor, body string) error

func (*Comment) MoreChildren

func (c *Comment) MoreChildren() ([]*Comment, error)

func (*Comment) Reply

func (c *Comment) Reply(user *Redditor, body string) error

func (*Comment) Vote

func (c *Comment) Vote(vote bool) error

true for up, false for down

type Message

type Message struct {
	WasComment bool
	Body       string
	Subject    string
	Subreddit  string // if comment
	ParentId   string `json:"parent_id"`
	New        bool
	Author     string
	Recipient  string `json:"dest"`
	Context    string
	Likes      bool
	LinkTitle  string `json:"link_title"` // if comment
	Replies    string
	*Thing
}

func (*Message) Block

func (m *Message) Block(user *Redditor) error

Block blocks the author of the message for the user

func (*Message) MarkAsRead

func (m *Message) MarkAsRead(user *Redditor) error

MarkAsRead marks the messages as read for the user

func (*Message) MarkAsUnread

func (m *Message) MarkAsUnread(user *Redditor) error

MarkAsUnread marks the message as unread for the user

type Redditor

type Redditor struct {
	LKarma        int  `json:"link_karma"`
	CKarma        int  `json:"comment_karma"`
	IsFriend      bool `json:"is_friend"`
	HasMail       bool `json:"has_mail"`
	IsOver18      bool `json:"over_18"`
	IsGold        bool `json:"is_gold"`
	IsMod         bool `json:"is_mod"`
	Created       int
	CreatedUTC    time.Time `json:"created_utc"`
	HasModMail    bool      `json:"has_mod_mail"`
	VerifiedEmail bool      `json:"has_verified_email"`
	ModHash       string
	*Thing
}

func GetRedditor

func GetRedditor(user string) (*Redditor, error)

GetRedditor returns a redditor object containing all information relevant to that reddit user

func Login

func Login(user, pass string, rem bool) (*Redditor, error)

Login logs a user into reddit through the api login page returns the same errors recieved from reddit, if applicable otherwise returns a redditor with populated modhash TODO(Stringy): add support for ssl.reddit.com/login

func NewRedditor

func NewRedditor() *Redditor

func (*Redditor) ClearSessions

func (r *Redditor) ClearSessions(pass string) error

ClearSessions clears the current user's reddit sessions This will result in immediate logging out of the user

func (*Redditor) ComposeMessage

func (r *Redditor) ComposeMessage() error

func (*Redditor) DeleteAccount

func (r *Redditor) DeleteAccount(passwd string) error

DeleteAccount deletes the user from reddit

func (*Redditor) GetFrontpage

func (r *Redditor) GetFrontpage() (*Subreddit, error)

GetFrontpage returns the frontpage for the user, including all subscribed subreddits

func (*Redditor) GetFrontpageN

func (r *Redditor) GetFrontpageN(n int) (*Subreddit, error)

GetFrontpageN returns the first n submissions from their frontpage similar semantics to GetSubredditN

func (*Redditor) GetInbox

func (r *Redditor) GetInbox(limit int) ([]Message, error)

GetInbox gets all mail from the user's mail doesn't require modhash for reading

func (*Redditor) GetUnreadMail

func (r *Redditor) GetUnreadMail(limit int) ([]Message, error)

GetUnreadMail gets the unread mail for the user doesn't require modhash for reading messages

func (*Redditor) IsLoggedIn

func (r *Redditor) IsLoggedIn() bool

IsLoggedIn returns true if the user is currently logged into reddit

func (*Redditor) Me

func (r *Redditor) Me() error

Me populates the redditor with their information

func (*Redditor) PostComment

func (r *Redditor) PostComment(parent *Submission, body string) error

PostComment posts a top level comment to a reddit submission

func (*Redditor) ReplyToComment

func (r *Redditor) ReplyToComment(parent *Comment, body string) error

ReplyToComment replies to a reddit comment on behalf of the user

func (r *Redditor) SubmitLink(subreddit, title, link string, resubmit bool) error

SubmitLink submits a link to the subreddit

func (*Redditor) SubmitSelf

func (r *Redditor) SubmitSelf(subreddit, title, body string) error

SubmitSelf submits a self (non-link) submission to the subreddit of choice

func (*Redditor) UpdateEmailAndPass

func (r *Redditor) UpdateEmailAndPass(email, newpass, curpass string) error

UpdateEmailAndPass updates the user's email and password. The user must provide their current password for this to succeed.

type Submission

type Submission struct {
	Title           string
	Url             string
	NumComments     int `json:"num_comments"`
	Author          string
	IsSelf          bool `json:"is_self"`
	IsNSFW          bool `json:"over_18"`
	SelfText        string
	Created         float64 `json:"created_utc"`
	Score           int
	Ups             int
	Downs           int
	Sub             string `json:"subreddit"`
	AuthorFlairCSS  string `json:"author_flair_css_class"`
	AuthorFlairText string `json:"author_flair_text"`
	LinkFlairCSS    string `json:"link_flair_css_class"`
	LinkFlairText   string `json:"link_flair_text"`
	Clicked         bool
	Domain          string
	Hidden          bool
	Likes           bool
	Permalink       string
	Saved           bool
	Edited          int
	Distinguished   string
	Stickied        bool
	*Thing
}

func (*Submission) GetComments

func (r *Submission) GetComments(s sort, t period) []Comment

func (*Submission) GetUrl

func (r *Submission) GetUrl() string

func (*Submission) PostComment

func (r *Submission) PostComment(user *Redditor, body string) error

func (*Submission) String

func (r *Submission) String() string

type Subreddit

type Subreddit struct {
	AccountsActive int `json:"accounts_active"`
	HideDuration   int `json:"comment_score_hide_mins"`
	Description    string
	DisplayName    string `json:"display_name"`
	HeaderImage    string `json:"header_img"`
	HeaderSize     []int  `json:"header_size"`
	HeaderTitle    string `json:"header_title"`
	Over18         bool   `json:"over_18"`
	PublicDesc     string `json:"public_description"`
	PublicTraffic  bool   `json:"public_traffic"`
	Subscribers    int
	SubmissionType string `json:"submission_type"`
	SubredditType  string `json:"subreddit_type"`
	Title          string
	Url            string // relative e.g. /r/pics
	UserIsBanned   bool   `json:"user_is_banned"`
	UserIsContrib  bool   `json:"user_is_contributor"`
	UserIsMod      bool   `json:"user_is_moderator"`
	UserIsSub      bool   `json:"user_is_subscriber"`
	Items          []struct {
		Submission `json:"data"`
	} `json:"children"`
	*sync.RWMutex
	*Thing
}

func GetFrontPage

func GetFrontPage(user *Redditor) (*Subreddit, error)

GetFrontPage currently gets the front page of *default* reddit TODO(Stringy): apply this to currently logged in user

func GetSubreddit

func GetSubreddit(sub string) (*Subreddit, error)

GetSubreddit gets the front page of a named subreddit.

sub is the name of a valid subreddit

func NewSubreddit

func NewSubreddit() *Subreddit

func (*Subreddit) GetControversial

func (s *Subreddit) GetControversial(limit int) ([]Submission, error)

func (*Subreddit) GetHot

func (s *Subreddit) GetHot(limit int) ([]Submission, error)

GetHot returns a list of "hot" submissions from the subreddit

func (*Subreddit) GetNew

func (s *Subreddit) GetNew(limit int) ([]Submission, error)

GetNew returns a list of "new" submissions from the subreddit

func (*Subreddit) GetTop

func (s *Subreddit) GetTop(t period, limit int) ([]Submission, error)

GetTop returns a list of "top" submissions for a particular time period (hour, day, month, year, all)

func (*Subreddit) GetUrl

func (s *Subreddit) GetUrl() string

type Thing

type Thing struct {
	Id   string
	Name string //fullname of a reddit thing (t1_, t2_, ...)
	Kind string //type
}

func (*Thing) Hide

func (t *Thing) Hide(user *Redditor) error

Hide hides a Thing for user, so that it won't appear on any requests returns any errors from reddit

func (*Thing) MarkNsfw

func (t *Thing) MarkNsfw(user *Redditor) error

MarkNsfw marks a reddit thing as not safe for work for user returns errors from reddit

func (*Thing) Report

func (t *Thing) Report(user *Redditor) error

Report reports a reddit Thing by user returns any errors recieved from reddit

func (*Thing) Unhide

func (t *Thing) Unhide(user *Redditor) error

Unhide undoes Hide to allow a Thing to turn up in user's requests returns any errors from reddit

func (*Thing) UnmarkNsfw

func (t *Thing) UnmarkNsfw(user *Redditor) error

UnmarkNsfw unmarks a Thing as not safe for work returns errors recieved from reddit

Jump to

Keyboard shortcuts

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