twbot

package module
v0.0.0-...-30734df Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2016 License: MIT Imports: 13 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

twbot package

Go Report Card

[GoDoc] (https://godoc.org/github.com/dns-gh/twbot)

Twitter Bot providing an asynchronous API to:

  • Make simple tweets
  • Make tweets with an image
  • Retweet messages with a user defined pattern
  • Auto like tweets/retweets with a user-defined pattern
  • Auto follow the followers of a user
  • Auto unfollow friends with a user-defined pattern
  • Add user-defined randomness to avoid, in a way, being caught as a bot

Still more to do, feel free to join my efforts!

Motivation

Used in the Nasa Space Rocks Bot project https://github.com/dns-gh/nasa-space-rocks-bot and for potential futur projects with Twitter for me or others :)

Installation

  • It requires Go language of course. You can set it up by downloading it here: https://golang.org/dl/
  • Install it here C:/Go.
  • Set your GOPATH, GOROOT and PATH environment variables:
export GOROOT=C:/Go
export GOPATH=WORKING_DIR
export PATH=C:/Go/bin:${PATH}
  • Download and install the package:
@working_dir $ go get github.com/dns-gh/twbot/...
@working_dir $ go install github.com/dns-gh/twbot

Example

See the https://github.com/dns-gh/nasa-space-rocks-bot

Tests

TODO

LICENSE

See included LICENSE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SleepPolicy

type SleepPolicy struct {
	// maxRand randomly sleeps after each request from '0' to 'maxRand' seconds
	MaxRand int
	// maybeSleep parameters enable the user to create a conditional sleep  after each requests
	// of 'maybeSleepChance' over 'maybeSleepTotalChance' chances. The sleep will last at least
	// 'maybeSleepMin' seconds and a maximum of 'maybeSleepMax' seconds.
	// For example, parameters 1, 10, 30, 60 design a random sleep between 30 to 60 seconds
	// that will occur with a chance of 1 over 10 after each requests.
	MaybeSleepChance      int
	MaybeSleepTotalChance int
	MaybeSleepMin         int
	MaybeSleepMax         int
}

SleepPolicy represents the sleeping behavior of the bot between requests to the twitter API. Their use is highly recommanded especially when you automatically follow and unfollow users. It will allow you to hide your bot face from twitter statistics analysis.

type TwitterBot

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

TwitterBot represents the twitter bot.

func MakeTwitterBot

func MakeTwitterBot(followersPath, friendsPath, tweetsPath string, debug bool) *TwitterBot

MakeTwitterBot creates a twitter bot. The database is made of 3 files: followers, friends and tweets. They are here to ensure to:

  • not add a friend as friend
  • not remove friendship from a non friend
  • not retweet a tweet already retweeted

You have to set up 4 environment variables:

TWITTER_CONSUMER_KEY,
TWITTER_CONSUMER_SECRET,
TWITTER_ACCESS_TOKEN,
TWITTER_ACCESS_SECRET.

They can be found here by creating a twitter app: https://apps.twitter.com/.

The 'debug' mode creates more logs and remove all sleeps between API twitter calls.

func MakeTwitterBotWithCredentials

func MakeTwitterBotWithCredentials(followersPath, friendsPath, tweetsPath, consumerKey, consumerSecret, accessToken, accessSecret string, debug bool) *TwitterBot

MakeTwitterBotWithCredentials creates a twitter bot. Same as MakeTwitterBot but the twitter keys are given as input.

func (*TwitterBot) AutoFollowFollowers

func (t *TwitterBot) AutoFollowFollowers(query string, maxPage int, sleepPolicy SleepPolicy)

AutoFollowFollowers automatically follows the followers of the first user fecthed using the given 'query'. The 'maxPage' parameter indicates the number of page of followers (5000 users max by page) we want to fetch. The sleep policy controls the type of sleep you want between requests.

func (*TwitterBot) AutoFollowFollowersAsync

func (t *TwitterBot) AutoFollowFollowersAsync(query string, maxPage int, sleepPolicy *SleepPolicy)

AutoFollowFollowersAsync automatically asynchronously follows the followers of the first user fecthed using the given 'query'. The 'maxPage' parameter indicates the number of page of followers (5000 users max by page) we want to fetch. The sleep policy controls the type of sleep you want between requests.

func (*TwitterBot) AutoUnfollowFriendsAsync

func (t *TwitterBot) AutoUnfollowFriendsAsync(sleepPolicy *SleepPolicy)

AutoUnfollowFriendsAsync automatically asynchronously unfollows friends from database that were added at least a day ago by default. The sleep policy controls the type of sleep you want between requests.

func (*TwitterBot) Close

func (t *TwitterBot) Close()

Close closes the twitter client

func (*TwitterBot) RetweetOnce

func (t *TwitterBot) RetweetOnce(queries, bannedQueries []string) error

RetweetOnce retweets randomly, with a maximum of 'retweetPolicy.maxTry' tries, a tweet matching one element of the input queries slice. It returns an error if the loading of tweets in database failed or if the retweet itself failed.

func (*TwitterBot) RetweetOnceAsync

func (t *TwitterBot) RetweetOnceAsync(searchQueries, bannedQueries []string)

RetweetOnceAsync retweets asynchronously and randomly, with a maximum of 'retweetPolicy.maxTry' tries, a tweet matching one element of the input queries slice. It logs errors if the loading of tweets in database failed or if the retweets itself failed.

func (*TwitterBot) RetweetPeriodically

func (t *TwitterBot) RetweetPeriodically(searchQueries, bannedQueries []string, freq time.Duration)

RetweetPeriodically retweets periodically and randomly, with a maximum of 'retweetPolicy.maxTry' tries, a tweet matching one element of the input queries slice. The retweet frequencies is set up by the given 'freq' input parameter. It logs errors if the loading of tweets in database failed or if the retweets itself failed.

func (*TwitterBot) RetweetPeriodicallyAsync

func (t *TwitterBot) RetweetPeriodicallyAsync(searchQueries, bannedQueries []string, freq time.Duration)

RetweetPeriodicallyAsync retweets asynchronously, periodically and randomly, with a maximum of 'retweetPolicy.maxTry' tries, a tweet matching one element of the input queries slice. The retweet frequencies is set up by the given 'freq' input parameter. It logs errors if the loading of tweets in database failed or if the retweets itself failed.

func (*TwitterBot) SetLikePolicy

func (t *TwitterBot) SetLikePolicy(auto bool, threshold int)

SetLikePolicy sets the like policy that allows to automatically likes tweets that are already liked above a threshold.

func (*TwitterBot) SetRetweetPolicy

func (t *TwitterBot) SetRetweetPolicy(maxTry int, like bool)

SetRetweetPolicy sets the retweet policy that allows to try to retweet 'maxTry' times when looping through a list of tweets to retweet. The 'like' parameter controls the ability to like the tweet or the retweet using the like policy.

func (*TwitterBot) TweetImageOnce

func (t *TwitterBot) TweetImageOnce(msg, archiveURL, img string) error

TweetImageOnce tweets the given 'msg', 'archiveURL' and img' data provided as strings. Note: internally, the 'img' data will be encoded to base 64 in order to be properly tweeted via the twitter API.

func (*TwitterBot) TweetImagePeriodically

func (t *TwitterBot) TweetImagePeriodically(fetch func() (string, string, string, error), freq time.Duration)

TweetImagePeriodically tweets periodically the message and image returned by the 'fetch' callback. The tweet frequencies is set up by the given 'freq' input parameter. It only logs the error if the 'fetch' call failed or if the tweet itself failed.

func (*TwitterBot) TweetImagePeriodicallyAsync

func (t *TwitterBot) TweetImagePeriodicallyAsync(fetch func() (string, string, string, error), freq time.Duration)

TweetImagePeriodicallyAsync tweets asynchronously and periodically the message and image returned by the 'fetch' callback. The tweet frequencies is set up by the given 'freq' input parameter. It only logs the error if the 'fetch' call failed or if the tweet itself failed.

func (*TwitterBot) TweetOnce

func (t *TwitterBot) TweetOnce(fetch func() (string, error)) error

TweetOnce tweets the message returned by the 'fetch' callback. It returns an error if the 'fetch' call failed or if the tweet itself failed.

func (*TwitterBot) TweetOnceAsync

func (t *TwitterBot) TweetOnceAsync(fetch func() (string, error))

TweetOnceAsync tweets asynchronously the message returned by the 'fetch' callback. It only logs the error if the 'fetch' call failed or if the tweet itself failed.

func (*TwitterBot) TweetPeriodically

func (t *TwitterBot) TweetPeriodically(fetch func() (string, error), freq time.Duration)

TweetPeriodically tweets periodically the message returned by the 'fetch' callback. The tweet frequencies is set up by the given 'freq' input parameter. It only logs the error if the 'fetch' call failed or if the tweet itself failed.

func (*TwitterBot) TweetPeriodicallyAsync

func (t *TwitterBot) TweetPeriodicallyAsync(fetch func() (string, error), freq time.Duration)

TweetPeriodicallyAsync tweets asynchronously and periodically the message returned by the 'fetch' callback. The tweet frequencies is set up by the given 'freq' input parameter. It only logs the error if the 'fetch' call failed or if the tweet itself failed.

func (*TwitterBot) TweetSliceOnce

func (t *TwitterBot) TweetSliceOnce(fetch func() ([]string, error)) error

TweetSliceOnce tweets the slice returned by the given 'fetch' callback. It returns an error is the 'fetch' calls fails and only logs errors for each failed tweet tentative.

func (*TwitterBot) TweetSliceOnceAsync

func (t *TwitterBot) TweetSliceOnceAsync(fetch func() ([]string, error))

TweetSliceOnceAsync tweets asynchronously the slice returned by the given 'fetch' callback. It logs errors for each failed tweet tentative.

func (*TwitterBot) TweetSlicePeriodically

func (t *TwitterBot) TweetSlicePeriodically(fetch func() ([]string, error), freq time.Duration)

TweetSlicePeriodically tweets periodically the slice returned by the given 'fetch' callback. The slice tweet frequencies is set up by the given 'freq' input parameter. It logs errors for each failed tweet tentative.

func (*TwitterBot) TweetSlicePeriodicallyAsync

func (t *TwitterBot) TweetSlicePeriodicallyAsync(fetch func() ([]string, error), freq time.Duration)

TweetSlicePeriodicallyAsync tweets asynchronously and periodically the slice returned by the given 'fetch' callback. The slice tweet frequencies is set up by the given 'freq' input parameter. It logs errors for each failed tweet tentative.

func (*TwitterBot) UpdateProfileBanner

func (t *TwitterBot) UpdateProfileBanner(img string, width, height, offsetLeft, offsetTop int) error

UpdateProfileBanner updates the profile banner of the authenticated user with the given encoded image data. Other parameters are optionals and usable coinjointly only if they all are strictly positive. For more details, see: https://dev.twitter.com/rest/reference/post/account/update_profile_banner

func (*TwitterBot) Wait

func (t *TwitterBot) Wait()

Wait waits for all the asynchronous calls to return

Jump to

Keyboard shortcuts

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