A
A
albertalexandrov2018-08-04 15:50:10
Python
albertalexandrov, 2018-08-04 15:50:10

What is the most efficient way to get a list of group members?

In continuation of the previous question .
In general, I refused to transfer the algorithm code to VKScript (it is inconvenient to write the code) in the request and used stored procedures. The stored procedure code is as follows:

var offset = parseInt(Args.offset);
var count = parseInt(Args.count);
var group_id = parseInt(Args.group_id);
if (group_id == 0) {
    group_id = Args.group_id;
}
var loop = 25;
var members = [];

while (offset < count && loop) { 
    members = members + API.groups.getMembers({"group_id": group_id, "offset": offset}).users;
    offset = offset + 1000;
    loop = loop - 1;
}

return {"offset": offset, "members": members};

python script code:
response = requests.get(url=f"{VK_API}/groups.getMembers",
                        params={
                            "group_id": VK_GROUP_ID,
                            "access_token": VK_TOKEN,
                            "v": VK_V
                        }).json()["response"]

count = response["count"]
members = response["users"]
offset = 1000

while offset < count:
    response = requests.get(url=f"{VK_API}/execute.getMembers",
                            params={
                                "group_id": VK_GROUP_ID,
                                "count": count,
                                "offset": offset,
                                "access_token": VK_TOKEN,
                                "v": VK_V
                            }).json()["response"]

    offset = response["offset"]
    members += response["members"]

That is, first I make a regular request in order to find out the number of group members, and only then get-requests to the stored procedure.
Maybe the task of getting a list of group members can be implemented more correctly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sgaynetdinov, 2018-08-09
@sgaynetdinov

You should pay attention to the py-vkontakte library , which allows you to work with the vk.com API.

>>> import vk
>>> api = vk.Api(TOKEN)
>>> group = api.get_group('team')  # vk.com/team
>>> user_items = [user for user in group.get_members()]

If you only need to get ` user_id `, you can use ` group.get_members_only_id ` and it's faster
>>> user_id_items = [user_id for user_id in group.get_members_only_id()]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question