2020-03-23 22:56:41 +00:00
|
|
|
package twitter
|
|
|
|
|
|
|
|
import (
|
2020-03-24 14:50:49 +00:00
|
|
|
"net/http"
|
|
|
|
|
2020-03-23 22:56:41 +00:00
|
|
|
"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
|
|
|
|
|
2020-03-24 14:50:49 +00:00
|
|
|
// Stream as in twitter.Stream
|
|
|
|
type Stream = twitter.Stream
|
|
|
|
|
2020-03-23 22:56:41 +00:00
|
|
|
// 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)
|
|
|
|
}
|
2020-03-24 14:50:49 +00:00
|
|
|
|
|
|
|
// SendTweet allows to send a string as tweet (optionally as reply to a specified tweet)
|
|
|
|
func SendTweet(msg string, replytweet *twitter.Tweet) (*twitter.Tweet, *http.Response, error) {
|
|
|
|
if replytweet == nil {
|
|
|
|
// not a reply
|
|
|
|
return Client.Statuses.Update(msg, nil)
|
|
|
|
}
|
|
|
|
// replying
|
|
|
|
p := twitter.StatusUpdateParams{
|
|
|
|
InReplyToStatusID: replytweet.ID,
|
|
|
|
}
|
|
|
|
return Client.Statuses.Update("@"+replytweet.User.ScreenName+" "+msg, &p)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetTextAndHashtags returns #tweet text and hashtags for short and extended tweets
|
|
|
|
func GetTextAndHashtags(tweet *twitter.Tweet) (string, []twitter.HashtagEntity) {
|
|
|
|
if tweet.ExtendedTweet == nil {
|
|
|
|
return tweet.Text, tweet.Entities.Hashtags
|
|
|
|
}
|
|
|
|
return tweet.ExtendedTweet.FullText, tweet.ExtendedTweet.Entities.Hashtags
|
|
|
|
}
|