forked from ChimeraCoder/anaconda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
friends_followers.go
300 lines (252 loc) · 9.56 KB
/
friends_followers.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
package anaconda
import (
"net/url"
"strconv"
)
type Cursor struct {
Previous_cursor int64
Previous_cursor_str string
Ids []int64
Next_cursor int64
Next_cursor_str string
}
type UserCursor struct {
Previous_cursor int64
Previous_cursor_str string
Next_cursor int64
Next_cursor_str string
Users []User
}
type FriendsIdsCursor struct {
Previous_cursor int64
Previous_cursor_str string
Next_cursor int64
Next_cursor_str string
Ids []int64
}
type FriendsIdsPage struct {
Ids []int64
Error error
}
type Friendship struct {
Name string
Id_str string
Id int64
Connections []string
Screen_name string
}
type FollowersPage struct {
Followers []User
Error error
}
type FriendsPage struct {
Friends []User
Error error
}
// FIXME: Might want to consolidate this with FriendsIdsPage and just
// have "UserIdsPage".
type FollowersIdsPage struct {
Ids []int64
Error error
}
// GetFriendshipsNoRetweets returns a collection of user_ids that the currently authenticated user does not want to receive retweets from.
// It does not currently support the stringify_ids parameter.
func (a TwitterApi) GetFriendshipsNoRetweets() (ids []int64, err error) {
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/friendships/no_retweets/ids.json", nil, &ids, _GET, response_ch}
return ids, (<-response_ch).err
}
func (a TwitterApi) GetFollowersIds(v url.Values) (c Cursor, err error) {
err = a.apiGet(a.baseUrl+"/followers/ids.json", v, &c)
return
}
// Like GetFollowersIds, but returns a channel instead of a cursor and pre-fetches the remaining results
// This channel is closed once all values have been fetched
func (a TwitterApi) GetFollowersIdsAll(v url.Values) (result chan FollowersIdsPage) {
result = make(chan FollowersIdsPage)
v = cleanValues(v)
go func(a TwitterApi, v url.Values, result chan FollowersIdsPage) {
// Cursor defaults to the first page ("-1")
next_cursor := "-1"
for {
v.Set("cursor", next_cursor)
c, err := a.GetFollowersIds(v)
// throttledQuery() handles all rate-limiting errors
// if GetFollowersList() returns an error, it must be a different kind of error
result <- FollowersIdsPage{c.Ids, err}
next_cursor = c.Next_cursor_str
if err != nil || next_cursor == "0" {
close(result)
break
}
}
}(a, v, result)
return result
}
func (a TwitterApi) GetFriendsIds(v url.Values) (c Cursor, err error) {
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/friends/ids.json", v, &c, _GET, response_ch}
return c, (<-response_ch).err
}
func (a TwitterApi) GetFriendshipsLookup(v url.Values) (friendships []Friendship, err error) {
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/friendships/lookup.json", v, &friendships, _GET, response_ch}
return friendships, (<-response_ch).err
}
func (a TwitterApi) GetFriendshipsIncoming(v url.Values) (c Cursor, err error) {
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/friendships/incoming.json", v, &c, _GET, response_ch}
return c, (<-response_ch).err
}
func (a TwitterApi) GetFriendshipsOutgoing(v url.Values) (c Cursor, err error) {
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/friendships/outgoing.json", v, &c, _GET, response_ch}
return c, (<-response_ch).err
}
func (a TwitterApi) GetFollowersList(v url.Values) (c UserCursor, err error) {
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/followers/list.json", v, &c, _GET, response_ch}
return c, (<-response_ch).err
}
func (a TwitterApi) GetFriendsList(v url.Values) (c UserCursor, err error) {
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/friends/list.json", v, &c, _GET, response_ch}
return c, (<-response_ch).err
}
// Like GetFriendsList, but returns a channel instead of a cursor and pre-fetches the remaining results
// This channel is closed once all values have been fetched
func (a TwitterApi) GetFriendsListAll(v url.Values) (result chan FriendsPage) {
result = make(chan FriendsPage)
v = cleanValues(v)
go func(a TwitterApi, v url.Values, result chan FriendsPage) {
// Cursor defaults to the first page ("-1")
next_cursor := "-1"
for {
v.Set("cursor", next_cursor)
c, err := a.GetFriendsList(v)
// throttledQuery() handles all rate-limiting errors
// if GetFriendsListAll() returns an error, it must be a different kind of error
result <- FriendsPage{c.Users, err}
next_cursor = c.Next_cursor_str
if err != nil || next_cursor == "0" {
close(result)
break
}
}
}(a, v, result)
return result
}
// Like GetFollowersList, but returns a channel instead of a cursor and pre-fetches the remaining results
// This channel is closed once all values have been fetched
func (a TwitterApi) GetFollowersListAll(v url.Values) (result chan FollowersPage) {
result = make(chan FollowersPage)
v = cleanValues(v)
go func(a TwitterApi, v url.Values, result chan FollowersPage) {
// Cursor defaults to the first page ("-1")
next_cursor := "-1"
for {
v.Set("cursor", next_cursor)
c, err := a.GetFollowersList(v)
// throttledQuery() handles all rate-limiting errors
// if GetFollowersList() returns an error, it must be a different kind of error
result <- FollowersPage{c.Users, err}
next_cursor = c.Next_cursor_str
if err != nil || next_cursor == "0" {
close(result)
break
}
}
}(a, v, result)
return result
}
// GetListMembers implements /lists/members.json
func (a TwitterApi) GetListMembers(screenName string, listID int64, v url.Values) (c UserCursor, err error) {
v = cleanValues(v)
v.Set("list_id", strconv.FormatInt(listID, 10))
v.Set("screen_name", screenName)
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/lists/members.json", v, &c, _GET, response_ch}
return c, (<-response_ch).err
}
func (a TwitterApi) GetFollowersUser(id int64, v url.Values) (c Cursor, err error) {
v = cleanValues(v)
v.Set("user_id", strconv.FormatInt(id, 10))
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/followers/ids.json", v, &c, _GET, response_ch}
return c, (<-response_ch).err
}
// Like GetFriendsIds, but returns a channel instead of a cursor and pre-fetches the remaining results
// This channel is closed once all values have been fetched
func (a TwitterApi) GetFriendsIdsAll(v url.Values) (result chan FriendsIdsPage) {
result = make(chan FriendsIdsPage)
v = cleanValues(v)
go func(a TwitterApi, v url.Values, result chan FriendsIdsPage) {
// Cursor defaults to the first page ("-1")
next_cursor := "-1"
for {
v.Set("cursor", next_cursor)
c, err := a.GetFriendsIds(v)
// throttledQuery() handles all rate-limiting errors
// if GetFollowersList() returns an error, it must be a different kind of error
result <- FriendsIdsPage{c.Ids, err}
next_cursor = c.Next_cursor_str
if err != nil || next_cursor == "0" {
close(result)
break
}
}
}(a, v, result)
return result
}
func (a TwitterApi) GetFriendsUser(id int64, v url.Values) (c Cursor, err error) {
v = cleanValues(v)
v.Set("user_id", strconv.FormatInt(id, 10))
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/friends/ids.json", v, &c, _GET, response_ch}
return c, (<-response_ch).err
}
// FollowUserId follows the user with the specified userId.
// This implements the /friendships/create endpoint, though the function name
// uses the terminology 'follow' as this is most consistent with colloquial Twitter terminology.
func (a TwitterApi) FollowUserId(userId int64, v url.Values) (user User, err error) {
v = cleanValues(v)
v.Set("user_id", strconv.FormatInt(userId, 10))
return a.postFriendshipsCreateImpl(v)
}
// FollowUserId follows the user with the specified screenname (username).
// This implements the /friendships/create endpoint, though the function name
// uses the terminology 'follow' as this is most consistent with colloquial Twitter terminology.
func (a TwitterApi) FollowUser(screenName string) (user User, err error) {
v := url.Values{}
v.Set("screen_name", screenName)
return a.postFriendshipsCreateImpl(v)
}
func (a TwitterApi) postFriendshipsCreateImpl(v url.Values) (user User, err error) {
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/friendships/create.json", v, &user, _POST, response_ch}
return user, (<-response_ch).err
}
// UnfollowUserId unfollows the user with the specified userId.
// This implements the /friendships/destroy endpoint, though the function name
// uses the terminology 'unfollow' as this is most consistent with colloquial Twitter terminology.
func (a TwitterApi) UnfollowUserId(userId int64) (u User, err error) {
v := url.Values{}
v.Set("user_id", strconv.FormatInt(userId, 10))
// Set other values before calling this method:
// page, count, include_entities
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/friendships/destroy.json", v, &u, _POST, response_ch}
return u, (<-response_ch).err
}
// UnfollowUser unfollows the user with the specified screenname (username)
// This implements the /friendships/destroy endpoint, though the function name
// uses the terminology 'unfollow' as this is most consistent with colloquial Twitter terminology.
func (a TwitterApi) UnfollowUser(screenname string) (u User, err error) {
v := url.Values{}
v.Set("screen_name", screenname)
// Set other values before calling this method:
// page, count, include_entities
response_ch := make(chan response)
a.queryQueue <- query{a.baseUrl + "/friendships/destroy.json", v, &u, _POST, response_ch}
return u, (<-response_ch).err
}