graw

package module
v0.0.0-...-e9c4214 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2021 License: MIT Imports: 6 Imported by: 1

Documentation

Overview

Package graw is a high level, easy to use, Reddit bot library.

graw will take a low level handle from the graw/reddit package and manage everything for you. You just specify in a config what events you want to listen to on Reddit, and graw will take care of maintaining the event stream and calling the handler methods of your bot whenever new events occur.

Announcing new posts in /r/self is as simple as:

type announcer struct{}

func (a *announcer) Post(post *reddit.Post) error {
  fmt.Printf(`%s posted "%s"\n`, post.Author, post.Title)
  return nil
}

.....

// Get an api handle to reddit for a logged out (script) program,
// which forwards this user agent on all requests and issues a request at
// most every 5 seconds.
apiHandle := reddit.NewScript("your user agent", 5 * time.Second)

// Create a configuration specifying what event sources on Reddit graw
// should connect to the bot.
cfg := graw.Config{Subreddits: []string{"self"}}

// launch a graw scan in a goroutine using the bot, handle, and config. The
// returned "stop" and "wait" are functions. "stop" will stop the graw run
// at any time, and "wait" will block until it finishes.
stop, wait, err := graw.Scan(&announcer{}, apiHandle, cfg)

// This time, let's block so the bot will announce (ideally) forever.
if err := wait(); err != nil {
  fmt.Printf("graw run encountered an error: %v\n", err)
}

graw can handle many event sources on Reddit; see the Config struct for the complete set of offerings.

graw has one other function that behaves like Scan(), which is Run(). Scan() is for logged-out bots (what Reddit calls "scripts"). Run() handles logged in bots, which can subscribe to logged-in event sources in the bot's account inbox like mentions and private messages.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(handler interface{}, bot reddit.Bot, cfg Config) (
	func(),
	func() error,
	error,
)

Run connects a handler to any requested event sources and makes requests with the given bot api handle. It launches a goroutine for the run. It returns two functions, a stop() function to terminate the graw run at any time, and a wait() function to block until the graw run fails.

func Scan

func Scan(handler interface{}, script reddit.Script, cfg Config) (
	func(),
	func() error,
	error,
)

Scan connects any requested logged-out event sources to the given handler, making requests with the given script handle. It launches a goroutine for the scan. It returns two functions: a stop() function to stop the scan at any time, and a wait() function to block until the scan fails.

Types

type Config

type Config struct {
	// New posts in all subreddits named here will be forwarded to the bot's
	// PostHandler.
	Subreddits []string
	// New comments in all subreddits named here will be forwarded to the
	// bot's CommentHandler.
	SubredditComments []string
	// New posts and comments made by all users named here will be forwarded
	// to the bot's UserHandler. Note that since a separate monitor must be
	// construced for every user, unlike subreddits, subscribing to the
	// actions of many users can delay updates from other event sources.
	Users []string
	// When true, replies to posts made by the bot's account will be
	// forwarded to the bot's PostReplyHandler.
	PostReplies bool
	// When true, replies to comments made by the bot's account will be
	// forwarded to the bot's CommentReplyHandler.
	CommentReplies bool
	// When true, mentions of the bot's username  will be forwarded to the
	// bot's MentionHandler.
	Mentions bool
	// When true, messages sent to the bot's inbox will be forwarded to the
	// bot's MessageHandler.
	Messages bool
	// If set, internal messages will be logged here. This is a spammy log
	// used for debugging graw.
	Logger *log.Logger
}

Config configures a graw run or scan by specifying event sources. Each event type has a corresponding handler defined in graw/botfaces. The bot must be able to handle requested event types.

Directories

Path Synopsis
Package botfaces defines interfaces graw uses to connect bots to event streams on Reddit.
Package botfaces defines interfaces graw uses to connect bots to event streams on Reddit.
cmd
act command
Package act is a utility to operating a Reddit account from cli using the reddit package.
Package act is a utility to operating a Reddit account from cli using the reddit package.
feed command
Package feed is an example grawbot that announces the feed of a given subreddit.
Package feed is an example grawbot that announces the feed of a given subreddit.
Package reddit is a small wrapper around the Reddit API.
Package reddit is a small wrapper around the Reddit API.
Package streams provides robust event streams from Reddit.
Package streams provides robust event streams from Reddit.
internal/monitor
Package monitor tracks a listing feed on Reddit.
Package monitor tracks a listing feed on Reddit.
internal/rsort
Package rsort provides tools for sorting Reddit elements.
Package rsort provides tools for sorting Reddit elements.

Jump to

Keyboard shortcuts

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