/
twitter.go
355 lines (287 loc) · 7.63 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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// bsdf/twitter: an implementation of the twitter api in Go
// Copyright (C) 2012, 2013 bsdf
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package twitter
import (
"encoding/json"
"errors"
"fmt"
"strings"
)
type Twitter struct {
ConsumerKey string
ConsumerSecret string
OAuthToken string
OAuthTokenSecret string
DebugMode bool
}
func New(consumerKey, consumerSecret, oauthToken, oauthTokenSecret string) *Twitter {
return &Twitter{
consumerKey,
consumerSecret,
oauthToken,
oauthTokenSecret,
false,
}
}
// Retrieves a user's timeline
func (t *Twitter) GetUserTimeline(screenName string) (tweets []Tweet, err error) {
url := fmt.Sprintf("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=%s", screenName)
method := &RestMethod{
Url: url,
Method: "GET",
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
err = json.Unmarshal(body, &tweets)
return
}
// Send a tweet
// Returns the Tweet if successful, error if unsuccessful
func (t *Twitter) Tweet(message string) (tweet Tweet, err error) {
data := fmt.Sprintf("status=%s", encode(message))
method := &RestMethod{
Url: "https://api.twitter.com/1.1/statuses/update.json",
Method: "POST",
Data: data,
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
err = json.Unmarshal(body, &tweet)
return
}
// Follow a user
// Returns the User if successful, error if unsuccessful
func (t *Twitter) Follow(username string) (user User, err error) {
method := &RestMethod{
Url: "https://api.twitter.com/1.1/friendships/create.json",
Method: "POST",
Data: fmt.Sprintf("screen_name=%s", encode(username)),
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
err = json.Unmarshal(body, &user)
return
}
// Unfollow a user
// Returns the User if successful, error if unsuccessful
func (t *Twitter) Unfollow(username string) (user User, err error) {
method := &RestMethod{
Url: "https://api.twitter.com/1.1/friendships/destroy.json",
Method: "POST",
Data: fmt.Sprintf("screen_name=%s", encode(username)),
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
err = json.Unmarshal(body, &user)
return
}
// Retweets a tweet based upon its id
// Returns the Tweet if successful, error if unsuccessful
func (t *Twitter) Retweet(id int64) (tweet Tweet, err error) {
url := fmt.Sprintf("https://api.twitter.com/1.1/statuses/retweet/%d.json", id)
method := &RestMethod{
Url: url,
Method: "POST",
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
err = json.Unmarshal(body, &tweet)
return
}
// Destroys a tweet based upon its id
// Returns the Tweet if successful, error if unsuccessful
func (t *Twitter) Destroy(id int64) (tweet Tweet, err error) {
url := fmt.Sprintf("https://api.twitter.com/1.1/statuses/destroy/%d.json", id)
method := &RestMethod{
Url: url,
Method: "POST",
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
err = json.Unmarshal(body, &tweet)
return
}
// Destroys a tweet based upon its id
// Returns the Tweet if successful, error if unsuccessful
func (t *Twitter) Search(query string) (tweets []Tweet, err error) {
url := fmt.Sprintf("https://api.twitter.com/1.1/search/tweets.json?q=%s", encode(query))
method := &RestMethod{
Url: url,
Method: "GET",
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
var result SearchResult
err = json.Unmarshal(body, &result)
if err != nil {
return
}
return result.Results, err
}
// Returns current RateLimitStatus or error
// func (t *Twitter) GetRateLimitStatus() (status RateLimitStatus, err error) {
// method := &RestMethod{
// Url: "https://api.twitter.com/1.1/application/rate_limit_status.json",
// Method: "GET",
// }
// body, err := t.sendRestRequest(method)
// if err != nil {
// return
// }
// err = json.Unmarshal(body, &status)
// return
// }
func (t *Twitter) GetPrivacyPolicy() (policy string, err error) {
method := &RestMethod{
Url: "https://api.twitter.com/1.1/help/privacy.json",
Method: "GET",
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
var policyResult = struct {
Privacy string
}{}
err = json.Unmarshal(body, &policyResult)
if err != nil {
return
}
return policyResult.Privacy, err
}
func (t *Twitter) GetTOS() (tos string, err error) {
method := &RestMethod{
Url: "https://api.twitter.com/1.1/help/tos.json",
Method: "GET",
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
var tosResult = struct {
Tos string
}{}
err = json.Unmarshal(body, &tosResult)
if err != nil {
return
}
return tosResult.Tos, err
}
func (t *Twitter) GetUserFriends(user string) (friends []int64, err error) {
url := fmt.Sprintf("https://api.twitter.com/1.1/friends/ids.json?screen_name=%s", user)
method := &RestMethod{
Url: url,
Method: "GET",
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
var responseStruct = struct {
Ids []int64
}{}
err = json.Unmarshal(body, &responseStruct)
if err != nil {
return
}
return responseStruct.Ids, err
}
func (t *Twitter) LookupUsersById(ids []int64) (users []User, err error) {
if len(ids) > 100 {
return users, errors.New("LookupUsersById can only take 100 or less ids")
}
var strIds = make([]string, len(ids))
i := 0
for _, v := range ids {
strIds[i] = fmt.Sprintf("%d", v)
i++
}
urlBase := "https://api.twitter.com/1.1/users/lookup.json?include_entities=false&user_id=%s"
url := fmt.Sprintf(urlBase, encode(strings.Join(strIds, ",")))
method := &RestMethod{
Url: url,
Method: "GET",
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
err = json.Unmarshal(body, &users)
return
}
func (t *Twitter) GetDirectMessages() (dms []DirectMessage, err error) {
method := &RestMethod{
Url: "https://api.twitter.com/1.1/direct_messages.json",
Method: "GET",
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
err = json.Unmarshal(body, &dms)
return
}
func (t *Twitter) SendDirectMessage(user, text string) (dm DirectMessage, err error) {
data := fmt.Sprintf("screen_name=%s&text=%s", encode(user), encode(text))
method := &RestMethod{
Url: "https://api.twitter.com/1.1/direct_messages/new.json",
Method: "POST",
Data: data,
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
err = json.Unmarshal(body, &dm)
return
}
func (t *Twitter) DeleteDirectMessage(id int64) (dm DirectMessage, err error) {
url := fmt.Sprintf("https://api.twitter.com/1.1/direct_messages/destroy/%d.json", id)
method := &RestMethod{
Url: url,
Method: "POST",
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
err = json.Unmarshal(body, &dm)
return
}
func (t *Twitter) GetUser(userName string) (user User, err error) {
url := fmt.Sprintf("https://api.twitter.com/1.1/users/show.json?screen_name=%s", userName)
method := &RestMethod{
Url: url,
Method: "GET",
}
body, err := t.sendRestRequest(method)
if err != nil {
return
}
err = json.Unmarshal(body, &user)
return
}