39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
|
package twitter
|
||
|
|
||
|
import (
|
||
|
"git.1750studios.com/ToddShepard/DB640/internal/config"
|
||
|
"github.com/dghubble/go-twitter/twitter"
|
||
|
"github.com/dghubble/oauth1"
|
||
|
)
|
||
|
|
||
|
// Client holds the authenticated twitter client
|
||
|
var Client *twitter.Client
|
||
|
|
||
|
// Tweet as in twitter.Tweet
|
||
|
type Tweet = twitter.Tweet
|
||
|
|
||
|
// Init initzializes the twitter client
|
||
|
func Init() {
|
||
|
conf := oauth1.NewConfig(config.C.Twitter.ConsumerKey, config.C.Twitter.ConsumerSecret)
|
||
|
token := oauth1.NewToken(config.C.Twitter.AccessKey, config.C.Twitter.AccessSecret)
|
||
|
httpClient := conf.Client(oauth1.NoContext, token)
|
||
|
|
||
|
Client = twitter.NewClient(httpClient)
|
||
|
}
|
||
|
|
||
|
// GetStreamForTag returns a stream object for a specified hashtag
|
||
|
func GetStreamForTag(hashtag string) (*twitter.Stream, error) {
|
||
|
params := &twitter.StreamFilterParams{
|
||
|
Track: []string{hashtag},
|
||
|
StallWarnings: twitter.Bool(true),
|
||
|
}
|
||
|
return Client.Streams.Filter(params)
|
||
|
}
|
||
|
|
||
|
// StreamDemux sets callback for incoming messages
|
||
|
func StreamDemux(stream *twitter.Stream, cb func(*Tweet)) {
|
||
|
demux := twitter.NewSwitchDemux()
|
||
|
demux.Tweet = cb
|
||
|
demux.HandleChan(stream.Messages)
|
||
|
}
|