twitter

package
v0.0.0-...-a4f4c6b Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientCreate

func ClientCreate(config Config) error

Types

type Binding

type Binding struct {
	Request                     func(client *ClientWrapper, options *BindingOptions, args ...any) (any, error)
	RequestRateLimit            RequestRateLimit
	MinResourcesPerRequest      int
	MaxResourcesPerRequest      int
	Type                        BindingType
	ResourceType                BindingResourceType
	SetOptionsForNextRequest    func(previousResult BindingResult, args ...any) ([]any, error)
	SetOptionsForCurrentRequest func(resources int, args ...any) ([]any, error)
	ClampMaxResults             func(binding *Binding, args ...any) (int, []any)
}

Binding represents a binding for the ClientWrapper and wraps a Request function, the RequestRateLimit for the action, and the BindingType of the Binding.

type BindingError

type BindingError interface {
	error
	Errors() []*twitter.ErrorObj
}

BindingError wraps an array of twitter.ErrorObj in a Go error.

type BindingOptions

type BindingOptions struct {
	Total int
}

BindingOptions are the options available for the execution of a Binding.

type BindingResourceType

type BindingResourceType int

BindingResourceType represents the resource type that is returned by a Binding.

const (
	// Tweet is returned by Bindings that fetch tweets.
	Tweet BindingResourceType = iota
	User
)

func (BindingResourceType) String

func (brt BindingResourceType) String() string

String returns the name of the BindingResourceType.

type BindingResult

type BindingResult interface {
	Raw() any
	Meta() BindingResultMeta
	RateLimit() *twitter.RateLimit
	MergeNext(nextBindingResult BindingResult, binding *Binding) (err error)
}

BindingResult is an interface for the fields that can be found in many of the responses from the twitter.Client.

type BindingResultMeta

type BindingResultMeta interface {
	// NewestID returns the newest ID in the response.
	NewestID() string
	// OldestID returns the oldest ID in the response.
	OldestID() string
	// ResultCount returns the result count in the response.
	ResultCount() int
	// NextToken returns the next token in the response for pagination.
	NextToken() string
}

BindingResultMeta is an interface for the Meta field that can be found in many of the responses from twitter.Client.

type BindingType

type BindingType int

BindingType represents a binding for the ClientWrapper.

const (
	// RecentSearch binds to the twitter.Client.TweetRecentSearch method.
	RecentSearch BindingType = iota
	// UserRetrieve binds to the twitter.Client.UserLookup method.
	UserRetrieve
	// UserNameRetrieve binds to the twitter.Client.UserNameLookup method.
	UserNameRetrieve
)

func (BindingType) Binding

func (bt BindingType) Binding() *Binding

Binding returns the Binding for the BindingType using the clientBindings.

func (BindingType) String

func (bt BindingType) String() string

String returns the name of the BindingType.

func (BindingType) WrapResponse

func (bt BindingType) WrapResponse(client *ClientWrapper, response any) (result BindingResult, err error)

WrapResponse wraps the given response from the given ClientWrapper in a prototype that implements the BindingResult interface.

type ClientWrapper

type ClientWrapper struct {
	Client        *twitter.Client
	Config        Config
	TweetCapMutex sync.RWMutex
	// RateLimits stores the twitter.RateLimit for each BindingType that has been executed. Actual map type:
	//  map[BindingType]*twitter.RateLimit
	RateLimits sync.Map
	TweetCap   *TweetCap
}
var Client *ClientWrapper

func (*ClientWrapper) CheckRateLimit

func (w *ClientWrapper) CheckRateLimit(binding *Binding, totalResources int) (err error)

CheckRateLimit will return an appropriate error if the given number of resources exceeds either the tweet cap or the given RequestRateLimit.

func (*ClientWrapper) ExecuteBinding

func (w *ClientWrapper) ExecuteBinding(bindingType BindingType, options *BindingOptions, args ...any) (bindingResult BindingResult, err error)

ExecuteBinding executes the given BindingType with the given BindingOptions and returns a prototype that implements the BindingResult. It will check if the rate limit has been/will be exceeded using the ClientWrapper.CheckRateLimit method before executing the Binding.Request.

func (*ClientWrapper) GetTweetCap

func (w *ClientWrapper) GetTweetCap() (err error)

GetTweetCap will first check if the TweetCap cache file exists. If so, then it will read the TweetCap from there. Otherwise, it will log in to the Twitter developer portal using browser.Browser and parse the details on the dashboard to a TweetCap instance.

func (*ClientWrapper) RateLimit

func (w *ClientWrapper) RateLimit(bindingType BindingType) (*twitter.RateLimit, bool)

RateLimit returns the latest twitter.RateLimit for the given BindingType. The bool represents whether there is a twitter.RateLimit for the given BindingType, if this is false then the twitter.RateLimit returned will be nil.

func (*ClientWrapper) SetTweetCap

func (w *ClientWrapper) SetTweetCap(used int, remaining int, total int, resets time.Time, lastFetched time.Time) (err error)

SetTweetCap will set the TweetCap field of the given ClientWrapper but also save the TweetCap to a cache JSON file to be read later on.

func (*ClientWrapper) WriteTweetCap

func (w *ClientWrapper) WriteTweetCap() (err error)

WriteTweetCap will write the TweetCap to a JSON cache file located at DefaultTweetCapLocation.

type Config

type Config interface {
	TwitterAPIKey() string
	TwitterAPIKeySecret() string
	TwitterBearerToken() string
	TwitterUsername() string
	TwitterPassword() string
	TwitterHashtags() []string
	TwitterQuery() string
	TwitterHeadless() bool
	TwitterRateLimits() RateLimits
	TwitterTweetCapLocation() string
	TwitterCreatedAtFormat() string
	TwitterIgnoredErrorTypes() []string
}

type ContextAnnotation

type ContextAnnotation struct {
	Domain ContextAnnotationDomain `json:"domain"`
	Entity twitter.TweetContextObj `json:"entity"`
}

ContextAnnotation represents a context annotation for a Tweet returned when executing a Binding.

func (*ContextAnnotation) FromTweetContextAnnotationObj

func (ca *ContextAnnotation) FromTweetContextAnnotationObj(obj *twitter.TweetContextAnnotationObj)

FromTweetContextAnnotationObj sets the fields of the referred to ContextAnnotation using a twitter.TweetContextAnnotationObj.

func (*ContextAnnotation) ToSet

ToSet creates a mapset.Set for ContextAnnotation and pre-populates it with the referred to ContextAnnotation.

type ContextAnnotationDomain

type ContextAnnotationDomain int

ContextAnnotationDomain represents a domain that is used by the context annotations returned by the Twitter API for a tweet. This is defined so that we can weight each individual domain separately. For instance, VideoGame has a higher weighting in db.DeveloperSnapshot than VideoGamePublisher (as we are looking for unpublished games).

const (
	TvShows                     ContextAnnotationDomain = 3
	TvEpisodes                  ContextAnnotationDomain = 4
	SportsEvents                ContextAnnotationDomain = 6
	Person                      ContextAnnotationDomain = 10
	Sport                       ContextAnnotationDomain = 11
	SportsTeam                  ContextAnnotationDomain = 12
	Place                       ContextAnnotationDomain = 13
	TvGenres                    ContextAnnotationDomain = 22
	TvChannels                  ContextAnnotationDomain = 23
	SportsLeague                ContextAnnotationDomain = 26
	AmericanFootballGame        ContextAnnotationDomain = 27
	NflFootballGame             ContextAnnotationDomain = 28
	Events                      ContextAnnotationDomain = 29
	Community                   ContextAnnotationDomain = 31
	Politicians                 ContextAnnotationDomain = 35
	PoliticalRace               ContextAnnotationDomain = 38
	BasketballGame              ContextAnnotationDomain = 39
	SportsSeries                ContextAnnotationDomain = 40
	SoccerMatch                 ContextAnnotationDomain = 43
	BaseballGame                ContextAnnotationDomain = 44
	BrandVertical               ContextAnnotationDomain = 45
	BrandCategory               ContextAnnotationDomain = 46
	Brand                       ContextAnnotationDomain = 47
	Product                     ContextAnnotationDomain = 48
	Musician                    ContextAnnotationDomain = 54
	MusicGenre                  ContextAnnotationDomain = 55
	Actor                       ContextAnnotationDomain = 56
	EntertainmentPersonality    ContextAnnotationDomain = 58
	Athlete                     ContextAnnotationDomain = 60
	InterestsAndHobbiesVertical ContextAnnotationDomain = 65
	InterestsAndHobbiesCategory ContextAnnotationDomain = 66
	InterestsAndHobbies         ContextAnnotationDomain = 67
	HockeyGame                  ContextAnnotationDomain = 68
	VideoGame                   ContextAnnotationDomain = 71
	VideoGamePublisher          ContextAnnotationDomain = 78
	VideoGameHardware           ContextAnnotationDomain = 79
	CricketMatch                ContextAnnotationDomain = 83
	Book                        ContextAnnotationDomain = 84
	BookGenre                   ContextAnnotationDomain = 85
	Movie                       ContextAnnotationDomain = 86
	MovieGenre                  ContextAnnotationDomain = 87
	PoliticalBody               ContextAnnotationDomain = 88
	MusicAlbum                  ContextAnnotationDomain = 89
	RadioStation                ContextAnnotationDomain = 90
	Podcast                     ContextAnnotationDomain = 91
	SportsPersonality           ContextAnnotationDomain = 92
	Coach                       ContextAnnotationDomain = 93
	Journalist                  ContextAnnotationDomain = 94
	TvChannelEntityService      ContextAnnotationDomain = 95
	ReoccurringTrends           ContextAnnotationDomain = 109
	ViralAccounts               ContextAnnotationDomain = 110
	Concert                     ContextAnnotationDomain = 114
	VideoGameConference         ContextAnnotationDomain = 115
	VideoGameTournament         ContextAnnotationDomain = 116
	MovieFestival               ContextAnnotationDomain = 117
	AwardShow                   ContextAnnotationDomain = 118
	Holiday                     ContextAnnotationDomain = 119
	DigitalCreator              ContextAnnotationDomain = 120
	FictionalCharacter          ContextAnnotationDomain = 122
	MultimediaFranchise         ContextAnnotationDomain = 130
	UnifiedTwitterTaxonomy      ContextAnnotationDomain = 131
	VideoGamePersonality        ContextAnnotationDomain = 136
	ESportsTeam                 ContextAnnotationDomain = 137
	ESportsPlayer               ContextAnnotationDomain = 138
	FanCommunity                ContextAnnotationDomain = 139
	ESportsLeague               ContextAnnotationDomain = 149
	Food                        ContextAnnotationDomain = 152
	Weather                     ContextAnnotationDomain = 155
	Cities                      ContextAnnotationDomain = 156
	CollegesUniversities        ContextAnnotationDomain = 157
	PointsOfInterest            ContextAnnotationDomain = 158
	States                      ContextAnnotationDomain = 159
	Countries                   ContextAnnotationDomain = 160
	ExerciseFitness             ContextAnnotationDomain = 162
	Travel                      ContextAnnotationDomain = 163
	FieldsOfStudy               ContextAnnotationDomain = 164
	Technology                  ContextAnnotationDomain = 165
	Stocks                      ContextAnnotationDomain = 166
	Animals                     ContextAnnotationDomain = 167
	LocalNews                   ContextAnnotationDomain = 171
	GlobalTvShow                ContextAnnotationDomain = 172
	GoogleProductTaxonomy       ContextAnnotationDomain = 173
	DigitalAssetsCrypto         ContextAnnotationDomain = 174
	EmergencyEvents             ContextAnnotationDomain = 175
)

func (ContextAnnotationDomain) String

func (cam ContextAnnotationDomain) String() string

String returns the name of the ContextAnnotationDomain. This is the same as the name value in the domain value of a context annotation returned by the Twitter API for a tweet.

func (ContextAnnotationDomain) Value

func (cam ContextAnnotationDomain) Value() float64

Value returns the value of each domain in respect to their appropriateness towards Indie game posts on Twitter.

type ContextAnnotationSet

type ContextAnnotationSet struct {
	mapset.Set[ContextAnnotation]
}

ContextAnnotationSet represents a set of ContextAnnotation.

func NewContextAnnotationSet

func NewContextAnnotationSet(objs ...*twitter.TweetContextAnnotationObj) *ContextAnnotationSet

NewContextAnnotationSet creates a ContextAnnotationSet. If twitter.TweetContextAnnotationObj objects are provided, these will be converted to ContextAnnotation using ContextAnnotation.FromTweetContextAnnotationObj, then added to the ContextAnnotationSet.

func (ContextAnnotationSet) GormDBDataType

func (ContextAnnotationSet) GormDBDataType(db *gorm.DB, field *schema.Field) string

func (ContextAnnotationSet) GormDataType

func (ContextAnnotationSet) GormDataType() string

func (*ContextAnnotationSet) Scan

func (cas *ContextAnnotationSet) Scan(value any) (err error)

Scan will first unmarshal the JSON bytes in the DB into an array of ContextAnnotation, from here a ContextAnnotationSet will be created, and the elements of the array will be added to this set.

func (ContextAnnotationSet) ToSlice

func (cas ContextAnnotationSet) ToSlice() []ContextAnnotation

ToSlice creates a slice of ContextAnnotation from the Context

func (ContextAnnotationSet) Value

func (cas ContextAnnotationSet) Value() (driver.Value, error)

Value marshals the ContextAnnotationSet to a JSON array of objects.

type RateLimitError

type RateLimitError struct {
	// Config is a copy of the RateLimits config from the ClientWrapper.Config.
	Config RateLimits
	// RateLimit is a copy of the rate limit from a Twitter API action response.
	RateLimit *twitter.RateLimit
	// TweetCap is a copy of the TweetCap from the ClientWrapper.
	TweetCap *TweetCap
	// RequestRateLimit is a copy of the RequestRateLimit struct from a Binding.
	RequestRateLimit *RequestRateLimit
	// MaxResourcesPerRequest is the maximum number of resources possible to be requested from a single request of a
	// Binding.
	MaxResourcesPerRequest int
	// RequestedResources is the total number of resources that were requested.
	RequestedResources int
	// ResourceType is the BindingResourceType of the Binding that failed the ClientWrapper.CheckRateLimit check.
	ResourceType BindingResourceType
	// Happened is when the error happened.
	Happened time.Time
}

RateLimitError represents an error returned by ClientWrapper.CheckRateLimit.

func (*RateLimitError) Error

func (rle *RateLimitError) Error() string

type RateLimits

type RateLimits interface {
	LimitPerMonth() uint64
	LimitPerWeek() uint64
	LimitPerDay() uint64
	LimitPerHour() uint64
	LimitPerMinute() uint64
	LimitPerSecond() uint64
	LimitPerRequest() time.Duration
}

type RequestRateLimit

type RequestRateLimit struct {
	Requests int
	Every    time.Duration
}

RequestRateLimit represents a request rate limit for a Binding.

func (*RequestRateLimit) String

func (rrl *RequestRateLimit) String() string

String returns a string representation of the RequestRateLimit.

type TweetCap

type TweetCap struct {
	Used        int
	Remaining   int
	Total       int
	Resets      time.Time
	LastFetched time.Time
}

TweetCap represents the overall Tweet cap for a Twitter API app.

func (*TweetCap) CheckFresh

func (tc *TweetCap) CheckFresh() bool

CheckFresh will check if the TweetCap needs to be refreshed via the web (ClientWrapper.GetTweetCap).

func (*TweetCap) String

func (tc *TweetCap) String() string

String returns the string representation of the Tweet cap in the format: "<Used>/<Total> <Used/Total*100>% (<Remaining> rem.) resets: <Resets>, fetched: <LastFetched>".

Jump to

Keyboard shortcuts

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