forked from hoisie/twitterstream
/
twitterstream.go
219 lines (183 loc) · 4.92 KB
/
twitterstream.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
package twitterstream
import (
"bufio"
"bytes"
"encoding/base64"
"http"
"io"
"json"
"os"
"net"
"strconv"
"sync"
"time"
)
var followUrl, _ = http.ParseURL("http://stream.twitter.com/1/statuses/filter.json")
var trackUrl, _ = http.ParseURL("http://stream.twitter.com/1/statuses/filter.json")
var sampleUrl, _ = http.ParseURL("http://stream.twitter.com/1/statuses/sample.json")
var retryTimeout int64 = 5e9
type streamConn struct {
clientConn *http.ClientConn
url *http.URL
stream chan Tweet
authData string
postData string
stale bool
}
func (conn *streamConn) Close() {
// Just mark the connection as stale, and let the connect() handler close after a read
conn.stale = true
}
func (conn *streamConn) connect() (*http.Response, os.Error) {
if conn.stale {
return nil, os.NewError("Stale connection")
}
tcpConn, err := net.Dial("tcp", "", conn.url.Host+":80")
if err != nil {
return nil, err
}
conn.clientConn = http.NewClientConn(tcpConn, nil)
var req http.Request
req.URL = conn.url
req.Method = "GET"
req.Header = map[string]string{}
req.Header["Authorization"] = "Basic " + conn.authData
if conn.postData != "" {
req.Method = "POST"
req.Body = nopCloser{bytes.NewBufferString(conn.postData)}
req.ContentLength = int64(len(conn.postData))
req.Header["Content-Type"] = "application/x-www-form-urlencoded"
}
err = conn.clientConn.Write(&req)
if err != nil {
return nil, err
}
resp, err := conn.clientConn.Read()
if err != nil {
return nil, err
}
return resp, nil
}
func (conn *streamConn) readStream(resp *http.Response) {
var reader *bufio.Reader
reader = bufio.NewReader(resp.Body)
for {
//we've been closed
if conn.stale {
tcpConn, _ := conn.clientConn.Close()
if tcpConn != nil {
tcpConn.Close()
}
break
}
line, err := reader.ReadBytes('\n')
if err != nil {
if conn.stale {
continue
}
resp, err := conn.connect()
if err != nil {
println(err.String())
time.Sleep(retryTimeout)
continue
}
if resp.StatusCode != 200 {
continue
}
reader = bufio.NewReader(resp.Body)
continue
}
line = bytes.TrimSpace(line)
if len(line) == 0 {
continue
}
var tweet Tweet
json.Unmarshal(line, &tweet)
conn.stream <- tweet
}
}
type Client struct {
Username string
Password string
Stream chan Tweet
conn *streamConn
connLock *sync.Mutex
}
func NewClient(username, password string) *Client {
return &Client{username, password, make(chan Tweet), nil, new(sync.Mutex)}
}
func encodedAuth(user, pwd string) string {
var buf bytes.Buffer
encoder := base64.NewEncoder(base64.StdEncoding, &buf)
encoder.Write([]byte(user + ":" + pwd))
encoder.Close()
return buf.String()
}
type nopCloser struct {
io.Reader
}
func (nopCloser) Close() os.Error { return nil }
func (c *Client) connect(url *http.URL, body string) (err os.Error) {
if c.Username == "" || c.Password == "" {
return os.NewError("The username or password is invalid")
}
c.connLock.Lock()
var resp *http.Response
//initialize the new stream
var sc streamConn
sc.authData = encodedAuth(c.Username, c.Password)
sc.postData = body
sc.url = url
resp, err = sc.connect()
if err != nil {
goto Return
}
if resp.StatusCode != 200 {
err = os.NewError("Twitterstream HTTP Error" + resp.Status)
goto Return
}
//close the current connection
if c.conn != nil {
c.conn.Close()
}
c.conn = &sc
sc.stream = c.Stream
go sc.readStream(resp)
Return:
c.connLock.Unlock()
return
}
// Follow a list of user ids
func (c *Client) Follow(ids []int64) os.Error {
var body bytes.Buffer
body.WriteString("follow=")
for i, id := range ids {
body.WriteString(strconv.Itoa64(id))
if i != len(ids)-1 {
body.WriteString(",")
}
}
return c.connect(followUrl, body.String())
}
// Track a list of topics
func (c *Client) Track(topics []string) os.Error {
var body bytes.Buffer
body.WriteString("track=")
for i, topic := range topics {
body.WriteString(topic)
if i != len(topics)-1 {
body.WriteString(",")
}
}
return c.connect(trackUrl, body.String())
}
// Filter a list of user ids
func (c *Client) Sample() os.Error { return c.connect(sampleUrl, "") }
// Close the client
func (c *Client) Close() {
//has it already been closed?
if c.conn.stale {
return
}
c.conn.Close()
}