A
A
albertalexandrov2018-08-01 18:09:58
Python
albertalexandrov, 2018-08-01 18:09:58

How to use execute (VK API)?

Hello!
The task is to parse all the members of the group. Here is the code we have now:

import requests

API = "https://api.vk.com/method"
ACCESS_TOKEN = "**********"
V = 5.80

response = requests.post(url=f"{API}/execute",
                         data={
                             "code": "var members = API.groups.getMembers({\"group_id\": \"tensor_company\"}).users;"
                                     "var count = members.count;"
                                     "var offset = 1000;" 
                                     "while (offset < count);"
                                         "{;"
                                            "members = members + \" , \" + API.groups.getMembers({\"group_id\": \"tensor_company\", \"offset\": offset}).users;"
                                            "offset = offset + 1000;"
                                         "};"
                                     "return members;",
                             "access_token": ACCESS_TOKEN,
                             "v": V,
                         })

print(response.json())

Now an error occurs:
{'error': {'error_code': 13, 'error_msg': 'Runtime error occurred during code invocation: Too many operations', 'request_params': [{'key': 'group_id', 'value': 'tensor_company' }]}}

How to fix it?
PS execute I don't really understand yet.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bkosun, 2018-08-01
@bkosun

Note!
https://vk.com/dev/execute
Most likely, you exceed the set limit, optimize the code so that no more than 25 API calls are made inside the procedure.

S
Sergey Sokolov, 2018-08-01
@sergiks

See project on github

Procedure code for execute
var step=1000, R, offset = parseInt(Args.offset), loop=0,
out = { oid: parseInt(Args.oid), ids:[], mass: parseInt(Args.mass), overlap: parseInt(Args.overlap), offset: 0, next: 0 };

while( offset <= out.mass  &&  loop < 25) {
  R = API.groups.getMembers({ "group_id": out.oid, "sort": "id_asc", "offset": offset, "count": step});
  if( !!R.items  &&  R.items.length > 0) {
    out.ids.push( R.items);
    out.mass = R.count;

    out.loop = loop;

    out.offset = offset;
    offset = offset + step - out.overlap;
    out.next = offset;
    
    loop = loop + 1;
  } else {
    out.error = "Empty items";
    out.r = R;
    return out;
  }
}

return out;

In one execute call, there are no more than 25 calls to the VK API, not too many iterations in cycles, and a lot of other restrictions that can only be groped for by errors. Also, no more than 3 calls per second with one token. Those. 3 execute() per second, then pause, wait.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question