-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsolution.go
246 lines (211 loc) · 5.64 KB
/
solution.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"net/url"
"strconv"
"strings"
"sync"
"time"
"github.com/garyburd/go-oauth/oauth"
"github.com/machinebox/sdk-go/textbox"
)
// Tweet is a single tweet.
type Tweet struct {
Text string
Terms []string
}
// TweetReader includes the info we need to access Twitter.
type TweetReader struct {
ConsumerKey, ConsumerSecret, AccessToken, AccessSecret string
}
// NewTweetReader creates a new TweetReader with the given credentials.
func NewTweetReader(consumerKey, consumerSecret, accessToken, accessSecret string) *TweetReader {
return &TweetReader{
ConsumerKey: consumerKey,
ConsumerSecret: consumerSecret,
AccessToken: accessToken,
AccessSecret: accessSecret,
}
}
// Stats stores aggregated stats about
// tweets collected over time
type Stats struct {
SentimentAverage float64
Counts map[string]int
Mux sync.Mutex
}
// IncrementCount increments the count of tweets.
func (s *Stats) IncrementCount(sentiment float64) {
// Get the appropriate counter.
var key string
switch {
case sentiment > 0.80:
key = "positive"
case sentiment < 0.50:
key = "negative"
default:
key = "neutral"
}
// Update the counts.
s.Mux.Lock()
s.Counts[key]++
s.Counts["total"]++
s.Mux.Unlock()
}
// UpdateSentiment updates the tweet stream sentiment.
func (s *Stats) UpdateSentiment(newSentiment float64) {
// Lock so only the current goroutine can access the sentiment.
s.Mux.Lock()
// Get the current count of tweets.
total, ok := s.Counts["total"]
if !ok {
fmt.Println("Could not get key value \"total\"")
return
}
// Update the value.
s.SentimentAverage = (newSentiment + s.SentimentAverage*float64(total)) / (float64(total) + 1.0)
// Unlock the data.
s.Mux.Unlock()
}
// tweetWorker processes tweets off a buffered channel.
func tweetWorker(ctx context.Context, myStats *Stats, mbClient *textbox.Client, tweets chan Tweet) {
for {
select {
// Stop the goroutine.
case <-ctx.Done():
return
// Print the tweets.
case t := <-tweets:
// Analyze the tweet.
analysis, err := mbClient.Check(strings.NewReader(t.Text))
if err != nil {
fmt.Println("MachineBox error:", err)
continue
}
// Get the sentiment.
sentimentTotal := 0.0
for _, sentence := range analysis.Sentences {
sentimentTotal += sentence.Sentiment
}
sentimentTotal = sentimentTotal / float64(len(analysis.Sentences))
// Update the stats.
myStats.UpdateSentiment(sentimentTotal)
myStats.IncrementCount(sentimentTotal)
}
}
}
func main() {
// Create a new HTTP client.
var connLock sync.Mutex
var conn net.Conn
client := &http.Client{
Transport: &http.Transport{
Dial: func(netw, addr string) (net.Conn, error) {
connLock.Lock()
defer connLock.Unlock()
if conn != nil {
conn.Close()
conn = nil
}
netc, err := net.DialTimeout(netw, addr, 5*time.Second)
if err != nil {
return nil, err
}
conn = netc
return netc, nil
},
},
}
// Create a new Tweet Reader.
consumerKey := ""
consumerSecret := ""
accessToken := ""
accessSecret := ""
r := NewTweetReader(consumerKey, consumerSecret, accessToken, accessSecret)
// Create oauth Credentials.
creds := &oauth.Credentials{
Token: r.AccessToken,
Secret: r.AccessSecret,
}
// Create an oauth Client.
authClient := &oauth.Client{
Credentials: oauth.Credentials{
Token: r.ConsumerKey,
Secret: r.ConsumerSecret,
},
}
// Create the MachineBox client.
machBoxIP := "http://localhost:8080"
mbClient := textbox.New(machBoxIP)
// Initialize the stats.
myStats := Stats{
SentimentAverage: 0.0,
Counts: map[string]int{
"positive": 0,
"negative": 0,
"neutral": 0,
"total": 0,
},
Mux: sync.Mutex{},
}
// Setup the values we need for the context and filtering.
ctx, _ := context.WithTimeout(context.Background(), 10*time.Second)
tweets := make(chan Tweet)
terms := []string{"Trump", "Russia"}
fmt.Println("Start tweet workers...")
for w := 1; w <= 3; w++ {
go tweetWorker(ctx, &myStats, mbClient, tweets)
}
fmt.Println("Start another goroutine to collect tweets...")
go func() {
// Prepare the query.
form := url.Values{"track": terms}
formEnc := form.Encode()
u, err := url.Parse("https://stream.twitter.com/1.1/statuses/filter.json")
if err != nil {
fmt.Println("Error parsing URL:", err)
}
// Prepare the request.
req, err := http.NewRequest("POST", u.String(), strings.NewReader(formEnc))
if err != nil {
fmt.Println("creating filter request failed:", err)
}
req.Header.Set("Authorization", authClient.AuthorizationHeader(creds, "POST", u, form))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Content-Length", strconv.Itoa(len(formEnc)))
// Execute the request.
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error getting response:", err)
}
if resp.StatusCode != http.StatusOK {
fmt.Println("Unexpected HTTP status code:", resp.StatusCode)
}
// Decode the results.
decoder := json.NewDecoder(resp.Body)
for {
var t Tweet
if err := decoder.Decode(&t); err != nil {
break
}
tweets <- t
}
resp.Body.Close()
}()
// Check on our stats.
for i := 0; i < 10; i++ {
fmt.Println("")
time.Sleep(time.Second)
myStats.Mux.Lock()
fmt.Printf("Sentiment: %0.2f\n", myStats.SentimentAverage)
fmt.Printf("Total tweets analyzed: %d\n", myStats.Counts["total"])
fmt.Printf("Total positive tweets: %d\n", myStats.Counts["positive"])
fmt.Printf("Total negative tweets: %d\n", myStats.Counts["negative"])
fmt.Printf("Total neutral tweets: %d\n", myStats.Counts["neutral"])
myStats.Mux.Unlock()
}
}