-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitter.go
101 lines (86 loc) · 2.24 KB
/
twitter.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"net/http"
"os"
"github.com/dghubble/go-twitter/twitter"
"github.com/dghubble/oauth1"
)
// TwitterTrendsSvc accesses twitter
type TwitterTrendsSvc interface {
Trends(woeid int) ([]twitter.Trend, error)
Close()
TweetsFor(string) ([]string, error)
TwitterOfAProfile(string) ([]string, error)
}
type twitterTrends struct {
client *twitter.Client
}
func (tt *twitterTrends) Close() {}
func (tt *twitterTrends) Trends(woeid int) ([]twitter.Trend, error) {
ts, res, err := tt.client.Trends.Place(int64(woeid), nil)
if err != nil || res.StatusCode != http.StatusOK {
return nil, err
}
var trends []twitter.Trend
for _, xyz := range ts {
for _, trend := range xyz.Trends {
trends = append(trends, trend)
}
}
return trends, nil
}
func (tt *twitterTrends) TweetsFor(query string) ([]string, error) {
var tweets []string
search, _, err := tt.client.Search.Tweets(&twitter.SearchTweetParams{
Query: query,
Count: 100,
Lang: "pt",
})
if err != nil {
return nil, err
}
for _, tweet := range search.Statuses {
tweets = append(tweets, tweet.Text)
}
return tweets, nil
}
func (tt *twitterTrends) TwitterOfAProfile(query string) ([]string, error) {
var tweets []string
// Home Timeline
search, resp, err := tt.client.Timelines.UserTimeline(&twitter.UserTimelineParams{
ScreenName: query,
Count: 100,
})
if err != nil {
return nil, err
}
for _, tweet := range search {
tweets = append(tweets, tweet.Text)
}
return tweets, nil
print(resp, err)
return tweets, nil
}
// NewTwitterTrendsSvc creates a new TwitterTrendsSvc
func NewTwitterTrendsSvc() TwitterTrendsSvc {
return &twitterTrends{
client: newClient(),
}
}
func newClient() *twitter.Client {
consumerKey := os.Getenv("CONSUMER_KEY")
consumerSecret := os.Getenv("CONSUMER_SECRET")
accessToken := os.Getenv("ACCESS_TOKEN")
accessSecret := os.Getenv("ACCESS_SECRET")
config := oauth1.NewConfig(consumerKey, consumerSecret)
token := oauth1.NewToken(accessToken, accessSecret)
client := twitter.NewClient(config.Client(oauth1.NoContext, token))
// Tests connection and crashes app if there is bad auth data
_, _, err := client.Timelines.HomeTimeline(&twitter.HomeTimelineParams{
Count: 20,
})
if err != nil {
panic(err.Error())
}
return client
}