-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweets.go
262 lines (231 loc) · 8.47 KB
/
tweets.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package twitter
import (
"fmt"
"net/url"
"time"
)
// GetUserMentions returns Tweets mentioning a single user specified by the requested user ID.
// Endpoint URL: https://api.twitter.com/2/users/:id/mentions
// Official Documentation: https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/api-reference/get-users-id-mentions
// Authentication Methods: OAuth 1.0a User Context, OAuth 2.0 Bearer Token
// Rate Limit: 450/15m (app), 180/15m (user)
func (api *Twitter) GetUserMentions(id string, v url.Values, options ...QueueOption) (chan *Data, chan error) {
// create the queue to process requests
queue := NewQueue(15*time.Minute/1500, 15*time.Minute, true, make(chan *Request), make(chan *Response), options...)
// create the temp results channel
data := make(chan *Data)
errors := make(chan error)
// create the request object
request, _ := NewRquest("GET", fmt.Sprintf("%s/users/%s/mentions", api.baseURL, id), v, nil)
// start the requests channel processor
go queue.processRequests(api)
// add the 1st request to the channel
queue.requestsChannel <- request
// async process the response channel
go (func(q *Queue, d chan *Data, e chan error, req *Request) {
// on done close channels
// close data channel
defer close(d)
// close error channel
defer close(e)
// listen channel
for {
// capture the response and channel state
res, ok := <-q.responseChannel
// break the loop if the channel is closed
if !ok {
break
}
// send the results to the data channel
d <- &res.Results
// send errors to error channel
if res.Error != nil {
e <- res.Error
}
// if there is a next page, transform the original request object
// by setting the `pagination_token` parameter to get the next page
if res.Results.Meta != nil && res.Results.Meta.NextToken != "" && q.auto {
// create new url values and add the pagination token
nv := url.Values{}
nv.Add("pagination_token", res.Results.Meta.NextToken)
// update request's url Values
req.UpdateURLValues(nv)
// reset request's results
req.ResetResults()
// add next request to the channel
q.requestsChannel <- req
//go to start
continue
}
// we are done! break the loop and close the channels
break
}
})(queue, data, errors, request)
// return the data channel
return data, errors
}
// GetUserTweets returns Tweets composed by a single user, specified by the requested user ID.
// Endpoint URL: https://api.twitter.com/2/users/:id/tweets
// Official Documentation: https://developer.twitter.com/en/docs/twitter-api/tweets/timelines/api-reference/get-users-id-tweets
// Authentication Methods: OAuth 1.0a User Context, OAuth 2.0 Bearer Token
// Rate Limit: 1500/15m (app), 900/15m (user)
func (api *Twitter) GetUserTweets(id string, v url.Values, options ...QueueOption) (chan *Data, chan error) {
// create the queue to process requests
queue := NewQueue(15*time.Minute/1500, 15*time.Minute, true, make(chan *Request), make(chan *Response), options...)
// create the temp results channel
data := make(chan *Data)
errors := make(chan error)
// create the request object
request, _ := NewRquest("GET", fmt.Sprintf("%s/users/%s/tweets", api.baseURL, id), v, nil)
// start the requests channel processor
go queue.processRequests(api)
// add the 1st request to the channel
queue.requestsChannel <- request
// async process the response channel
go (func(q *Queue, d chan *Data, e chan error, req *Request) {
// on done close channels
// close data channel
defer close(d)
// close error channel
defer close(e)
// listen channel
for {
// capture the response and channel state
res, ok := <-q.responseChannel
// break the loop if the channel is closed
if !ok {
break
}
// send the results to the data channel
d <- &res.Results
// send errors to error channel
if res.Error != nil {
e <- res.Error
}
// if there is a next page, transform the original request object
// by setting the `pagination_token` parameter to get the next page
if res.Results.Meta != nil && res.Results.Meta.NextToken != "" && q.auto {
// create new url values and add the pagination token
nv := url.Values{}
nv.Add("pagination_token", res.Results.Meta.NextToken)
// update request's url Values
req.UpdateURLValues(nv)
// reset request's results
req.ResetResults()
// add next request to the channel
q.requestsChannel <- req
//go to start
continue
}
// we are done! break the loop and close the channels
break
}
})(queue, data, errors, request)
// return the data channel
return data, errors
}
// GetTweets returns a variety of information about the Tweet specified by the requested ID or list of IDs.
// Endpoint URL: https://api.twitter.com/2/tweets
// Official Documentation: https://developer.twitter.com/en/docs/twitter-api/tweets/lookup/api-reference/get-tweets
// Authentication Methods: OAuth 1.0a User Context, OAuth 2.0 Bearer Token
// Rate Limit: 300/15m (app), 900/15m (user)
func (api *Twitter) GetTweets(v url.Values, options ...QueueOption) (chan *Data, chan error) {
// create the queue to process requests
queue := NewQueue(15*time.Minute/1500, 15*time.Minute, true, make(chan *Request), make(chan *Response), options...)
// create the temp results channel
data := make(chan *Data)
errors := make(chan error)
// create the request object
request, _ := NewRquest("GET", fmt.Sprintf("%s/tweets", api.baseURL), v, nil)
// start the requests channel processor
go queue.processRequests(api)
// add the 1st request to the channel
queue.requestsChannel <- request
// async process the response channel
go (func(q *Queue, d chan *Data, e chan error, req *Request) {
// on done close channels
// close data channel
defer close(d)
// close error channel
defer close(e)
// listen channel
for {
// capture the response and channel state
res, ok := <-q.responseChannel
// break the loop if the channel is closed
if !ok {
break
}
// send the results to the data channel
d <- &res.Results
// send errors to error channel
if res.Error != nil {
e <- res.Error
}
// if there is a next page, transform the original request object
// by setting the `pagination_token` parameter to get the next page
if res.Results.Meta != nil && res.Results.Meta.NextToken != "" && q.auto {
// create new url values and add the pagination token
nv := url.Values{}
nv.Add("pagination_token", res.Results.Meta.NextToken)
// update request's url Values
req.UpdateURLValues(nv)
// reset request's results
req.ResetResults()
// add next request to the channel
q.requestsChannel <- req
//go to start
continue
}
// we are done! break the loop and close the channels
break
}
})(queue, data, errors, request)
// return the data channel
return data, errors
}
// GetTweetByID returns a variety of information about a single Tweet specified by the requested ID.
// Endpoint URL: https://api.twitter.com/2/tweets/:id
// Official Documentation: https://developer.twitter.com/en/docs/twitter-api/tweets/lookup/api-reference//get-tweets-id
// Authentication Methods: OAuth 1.0a User Context, OAuth 2.0 Bearer Token
// Rate Limit: 300/15m (app), 900/15m (user)
func (api *Twitter) GetTweetByID(id string, v url.Values, options ...QueueOption) (chan *Data, chan error) {
// create the queue to process requests
queue := NewQueue(15*time.Minute/1500, 15*time.Minute, true, make(chan *Request), make(chan *Response), options...)
// create the temp results channel
data := make(chan *Data)
errors := make(chan error)
// create the request object
request, _ := NewRquest("GET", fmt.Sprintf("%s/tweets/%s", api.baseURL, id), v, nil)
// start the requests channel processor
go queue.processRequests(api)
// add the 1st request to the channel
queue.requestsChannel <- request
// async process the response channel
go (func(q *Queue, d chan *Data, e chan error, req *Request) {
// on done close channels
// close data channel
defer close(d)
// close error channel
defer close(e)
// listen channel
for {
// capture the response and channel state
res, ok := <-q.responseChannel
// break the loop if the channel is closed
if !ok {
break
}
// send the results to the data channel
d <- &res.Results
// send errors to error channel
if res.Error != nil {
e <- res.Error
}
// we are done! break the loop and close the channels
break
}
})(queue, data, errors, request)
// return the data channel
return data, errors
}