📜 ⬆️ ⬇️

Getting vk.com community members in seconds

It's no secret that the VK API returns for a single request to the groups.getMembers method no more than 1,000 participants. In one second, you can get a maximum of 3,000 participants, since there is a limit on the number of requests per second to 3. This problem is solved by the execute method, with which you can get more than 100,000 participants in one second and up to 25,000 participants in one request . In this article I will tell you how I implemented it.

Without the use of the execute method, the process of getting group members with an audience of 4,000,000 people will take about 22 minutes, and we will need to execute about 4,000 API requests. Using the execute method, we accelerate this process to approximately 40 seconds and execute only about 160 queries.

Content:


')

I. A little about execute


This is a universal method that allows you to run a sequence of other methods, saving and filtering intermediate results.
An ordinary request is made as to all other methods, but in the parameters you need to pass code written in VKScript .

What supports vkscript and what is it?


It is a language similar to JavaScript or ActionScript. The algorithm should end with the command return% expression%. Operators must be separated by a semicolon.

Supported:


With this method, we can get the result of several methods in a single API request.

Ii. Javascript implementation


To work with Open Api, we need to connect the OpenApi library.
<script src="http://vk.com/js/api/openapi.js" type="text/javascript"></script> 


Let's go through authorization of the VK application and declare an array:
 VK.init({ apiId: 4235235 // ID   VK }); var membersGroups = []; //    


Get information about the group:
 //        function getMembers(group_id) { VK.Api.call('groups.getById', {group_id: group_id, fields: 'photo_50,members_count', v: '5.27'}, function(r) { if(r.response) { $('.group_info') .html('<img src="' + r.response[0].photo_50 + '"/><br/>' + r.response[0].name + '<br/>: ' + r.response[0].members_count); getMembers20k(group_id, r.response[0].members_count); //        membersGroups } }); } 


To get the members of the group, we will use execute , for one request we will get 25,000 members. Execute allows you to make up to 25 requests described using the language VKScript . In the code parameter we need to transfer the algorithm in the VKScript language. I did this as follows.

 VK.init({ apiId: 6456476 // ID   VK }); var membersGroups = []; //    getMembers(30666517); //        function getMembers(group_id) { VK.Api.call('groups.getById', {group_id: group_id, fields: 'photo_50,members_count', v: '5.27'}, function(r) { if(r.response) { $('.group_info') .html('<img src="' + r.response[0].photo_50 + '"/><br/>' + r.response[0].name + '<br/>: ' + r.response[0].members_count); getMembers20k(group_id, r.response[0].members_count); //        membersGroups } }); } //   , members_count -   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); //      } }); } 


Using:
 getMembers(ID); 


Sources and examples


In case VK is, it looked like this:
image

Work example: http://vk.com/app4236781
Sources: github.com/romkagolovadvayha/getmembersVKAPI.git

Source: https://habr.com/ru/post/248725/


All Articles