-
Notifications
You must be signed in to change notification settings - Fork 6
/
api.go
72 lines (64 loc) · 1.77 KB
/
api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"log"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
type TwitterAPIAuthentication struct {
*twitter.Client
}
func newTwitterAPIAuthentication(params OAuth1AuthenticationParameters) *TwitterAPIAuthentication {
config := oauth1.NewConfig(params.ConsumerKey, params.ConsumerSecret)
httpClient := config.Client(oauth1.NoContext, oauth1.NewToken(params.AccessKey, params.AccessSecret))
client := twitter.NewClient(httpClient)
if client == nil {
return nil
}
return &TwitterAPIAuthentication{client}
}
// Stream tweets based on terms e.g. a hashtag.
func (a *TwitterAPIAuthentication) MustStream(terms []string) <-chan *twitter.Tweet {
params := &twitter.StreamFilterParams{Track: terms}
ch, err := a.Streams.Filter(params)
if err != nil {
panic(err)
}
out := make(chan *twitter.Tweet)
go func() {
defer func() { ch.Stop(); close(out) }()
for msg := range ch.Messages {
tweet, ok := msg.(*twitter.Tweet)
if !ok {
log.Printf("(error) msg.(*twitter.Tweet): tweet=%+v", tweet)
continue
} else if tweet.RetweetedStatus != nil { // Ignore RTs
continue
}
out <- tweet
}
}()
return out
}
func (t *TwitterAPIAuthentication) Retweet(tweet *twitter.Tweet) error {
if tweet.Retweeted {
return nil
}
_, _, err := t.Statuses.Retweet(tweet.ID, nil)
return err
}
func (a *TwitterAPIAuthentication) Like(tweet *twitter.Tweet) error {
if tweet.Favorited {
return nil
}
params := &twitter.FavoriteCreateParams{ID: tweet.ID}
_, _, err := a.Favorites.Create(params)
return err
}
func (a *TwitterAPIAuthentication) Follow(tweet *twitter.Tweet) error {
if tweet.User.Following {
return nil
}
params := &twitter.FriendshipCreateParams{UserID: tweet.User.ID}
_, _, err := a.Friendships.Create(params)
return err
}