-
Notifications
You must be signed in to change notification settings - Fork 1
/
groups.go
282 lines (263 loc) · 8.05 KB
/
groups.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
package vkutil
import (
"encoding/json"
"fmt"
"log"
"net/url"
"github.com/zhuharev/vk"
)
type Group struct {
ID int `json:"id"`
Name string `json:"name"`
ScreenName string `json:"screen_name"`
IsClosed int `json:"is_closed"`
Type string `json:"type"`
IsAdmin int `json:"is_admin"`
IsMember int `json:"is_member"`
City struct {
ID int `json:"id"`
Title string `json:"title"`
} `json:"city"`
Country struct {
ID int `json:"id"`
Title string `json:"title"`
} `json:"country"`
Place struct {
ID int `json:"id"`
Title string `json:"title"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
Created int `json:"created"`
Icon string `json:"icon"`
GroupID int `json:"group_id"`
GroupPhoto string `json:"group_photo"`
Checkins int `json:"checkins"`
Updated int `json:"updated"`
Type int `json:"type"`
Country int `json:"country"`
City int `json:"city"`
} `json:"place"`
Description string `json:"description"`
WikiPage string `json:"wiki_page"`
MembersCount int `json:"members_count"`
Counters struct {
Topics int `json:"topics"`
Videos int `json:"videos"`
} `json:"counters"`
CanPost int `json:"can_post"`
CanSeeAllPosts int `json:"can_see_all_posts"`
Activity string `json:"activity"`
Status string `json:"status"`
Contacts []struct {
UserID int `json:"user_id"`
Desc string `json:"desc"`
} `json:"contacts"`
Links []struct {
ID int `json:"id"`
URL string `json:"url"`
Name string `json:"name"`
Desc string `json:"desc"`
Photo50 string `json:"photo_50"`
Photo100 string `json:"photo_100"`
EditTitle int `json:"edit_title,omitempty"`
} `json:"links"`
Verified int `json:"verified"`
Site string `json:"site"`
Cover struct {
Enabled int `json:"enabled"`
Images []struct {
URL string `json:"url"`
Width int `json:"width"`
Height int `json:"height"`
} `json:"images"`
} `json:"cover"`
Photo50 string `json:"photo_50"`
Photo100 string `json:"photo_100"`
Photo200 string `json:"photo_200"`
}
func (g Group) Closed() bool {
return g.IsClosed != 0
}
func (api *Api) GroupsGet(args ...url.Values) ([]int, error) {
params := url.Values{}
if len(args) == 1 {
params = args[0]
}
r, err := api.VkApi.Request(vk.METHOD_GROUPS_GET, params)
if err != nil {
return nil, err
}
_, ids, err := ParseIdsResponse(r)
if err != nil {
return nil, err
}
return ids, nil
}
// GroupsGetByID get group by id
func (api *Api) GroupsGetByID(ids interface{}, args ...url.Values) ([]Group, error) {
params := url.Values{}
if len(args) == 1 {
params = args[0]
}
params = setToUrlValues("group_ids", ids, params)
r, err := api.VkApi.Request(vk.METHOD_GROUPS_GET_BY_ID, params)
if err != nil {
return nil, err
}
type GroupsResponse struct {
Groups []Group `json:"response"`
}
var gr GroupsResponse
err = json.Unmarshal(r, &gr)
if err != nil {
return nil, err
}
return gr.Groups, nil
}
func (api *Api) GroupsJoin(groupId int, args ...url.Values) error {
params := url.Values{}
if len(args) == 1 {
params = args[0]
}
params.Set("group_id", fmt.Sprint(groupId))
r, err := api.VkApi.Request(vk.METHOD_GROUPS_JOIN, params)
if err != nil {
return err
}
_, e := ParseIntResponse(r)
if e != nil {
return e
}
return nil
}
func (api *Api) GroupsLeave(groupId int, args ...url.Values) error {
params := url.Values{}
if len(args) == 1 {
params = args[0]
}
params.Set("group_id", fmt.Sprint(groupId))
r, err := api.VkApi.Request(vk.METHOD_GROUPS_LEAVE, params)
if err != nil {
return err
}
_, e := ParseIntResponse(r)
if e != nil {
return e
}
return nil
}
func (api *Api) GroupsGetMembers(gid int, args ...url.Values) (count int, ids []int,
err error) {
params := url.Values{}
if len(args) == 1 {
params = args[0]
}
params.Set("group_id", fmt.Sprint(gid))
r, err := api.VkApi.Request(vk.METHOD_GROUPS_GET_MEMBERS, params)
if err != nil {
return
}
count, ids, err = ParseIdsResponse(r)
return
}
func (api *Api) GroupsGetAllMembers(gid int) ([]int, error) {
var members []int = []int{}
count, _, err := api.GroupsGetMembers(gid)
if err != nil {
return nil, err
}
var fmtcode = `
var limiter = 0;
var cnt = %[1]d;
var c = %[3]d;
var members = API.groups.getMembers({"group_id": %[2]d,"v":"5.32","offset" : %[3]d, "count": "1000"}).items;
var offset = %[3]d+1000;
while (limiter < 24 && c < cnt)
{
members = members + API.groups.getMembers({"group_id": %[2]d, "v": "5.32", "count": "1000", "offset": offset}).items;
offset = offset + 1000;
c = c + 1000;
limiter = limiter+1;
};
return members;
`
//offset := 0
//limit := 1000
for len(members) < count {
code := fmt.Sprintf(fmtcode, count, gid, len(members))
resp, err := api.Execute(code)
if err != nil {
log.Println(err)
}
var r struct {
Response []int `json:"response"`
}
err = json.Unmarshal(resp, &r)
if err != nil {
fmt.Println(string(resp))
return nil, err
}
//arr := arrStrToInt(strings.Split(r.Response, ","))
//for _, j := range arr {
// if j != 0 {
members = append(members, r.Response...)
// }
//}
}
if len(members) > count {
members = members[:count]
}
return members, nil
}
/*
function getMembers20k(group_id, members_count) {
var code = 'var members = API.groups.getMembers({"group_id": ' + group_id + ', "v": "5.27", "sort": "id_asc", "count": "1000", "offset": ' + membersGroups.length + '}).items;' // делаем первый запрос и создаем массив
+ 'var offset = 1000;' // это сдвиг по участникам группы
+ 'while (offset < 25000 && (offset + ' + membersGroups.length + ') < ' + members_count + ')' // пока не получили 20000 и не прошлись по всем участникам
+ '{'
+ 'members = members + "," + API.groups.getMembers({"group_id": ' + group_id + ', "v": "5.27", "sort": "id_asc", "count": "1000", "offset": (' + membersGroups.length + ' + offset)}).items;' // сдвиг участников на offset + мощность массива
+ 'offset = offset + 1000;' // увеличиваем сдвиг на 1000
+ '};'
+ 'return members;'; // вернуть массив members
VK.Api.call("execute", {code: code}, function(data) {
if (data.response) {
membersGroups = membersGroups.concat(JSON.parse("[" + data.response + "]")); // запишем это в массив
$('.member_ids').html('Загрузка: ' + membersGroups.length + '/' + members_count);
if (members_count > membersGroups.length) // если еще не всех участников получили
setTimeout(function() { getMembers20k(group_id, members_count); }, 333); // задержка 0.333 с. после чего запустим еще раз
else // если конец то
alert('Ура тест закончен! В массиве membersGroups теперь ' + membersGroups.length + ' элементов.');
} else {
alert(data.error.error_msg); // в случае ошибки выведем её
}
});
*/
func (a *Api) GroupsSearch(q string, params ...url.Values) ([]Group, error) {
param := setToUrlValues("q", q, params...)
bts, err := a.VkApi.Request(vk.METHOD_GROUPS_SEARCH, param)
if err != nil {
return nil, err
}
type Response struct {
Resp struct {
Count int `json:"count"`
Items []Group `json:"items"`
} `json:"response"`
}
var r Response
err = json.Unmarshal(bts, &r)
if err != nil {
return nil, err
}
return r.Resp.Items, nil
}
func (api *Api) GroupIsMember(publicID, userID int) (bool, error) {
param := setToUrlValues("group_id", publicID)
param = setToUrlValues("user_id", userID, param)
var r ResponseInt
err := api.RequestTyped(&r, vk.METHOD_GROUPS_IS_MEMBER, param)
if err != nil {
return false, err
}
return r.Response == 1, nil
}