forked from ChimeraCoder/anaconda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
friends_followers.go
145 lines (120 loc) · 4.47 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
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 Friendship struct {
Name string
Id_str string
Id int64
Connections []string
Screen_name string
}
type FollowersPage struct {
Followers []User
Error error
}
//GetFriendshipsNoRetweets s 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{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(BaseUrl+"/followers/ids.json", v, &c)
return
}
func (a TwitterApi) GetFriendsIds(v url.Values) (c Cursor, err error) {
response_ch := make(chan response)
a.queryQueue <- query{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{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{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{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{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{BaseUrl + "/friends/list.json", v, &c, _GET, response_ch}
return c, (<-response_ch).err
}
// 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)
if v == nil {
v = url.Values{}
}
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 next_cursor == "0" {
close(result)
break
}
}
}(a, v, result)
return result
}
// 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) (c Cursor, err error) {
response_ch := make(chan response)
a.queryQueue <- query{BaseUrl + "/friends/ids.json", v, &c, _GET, response_ch}
return c, (<-response_ch).err
}
// PostFriendshipsCreateToUserId is used to follow a user by ID
func (a TwitterApi) PostFriendshipsCreateToUserId(userId int64) (user User, err error) {
v := url.Values{}
v.Set("user_id", strconv.FormatInt(userId, 10))
return a.postFriendshipsCreateImpl(v)
}
// PostFriendshipsCreateToScreenName is used to follow a user by username
func (a TwitterApi) PostFriendshipsCreateToScreenName(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{BaseUrl + "/friendships/create.json", v, &user, _POST, response_ch}
return user, (<-response_ch).err
}