Hello! There was a question about the solution proposed in this answer regarding receiving user comments left in a particular community on vk. It seems that I reproduced the procedure in my application, substituted the ID of the profile and the ID of a random user from the comment to the first post to this profile (changed their places), but every time an error message is displayed. Please take a look: https://i.stack.imgur.com/cixeP.png

In addition, the same method fails to use the following code ( from here ), which receives a list of community members:

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); // Π² случаС ошибки Π²Ρ‹Π²Π΅Π΄Π΅ΠΌ Π΅Ρ‘ } }); } 
  • Add the error text returned by vk. - Mr Morgan
  • By the first example, it turned out to be wrong - it was just for the community ID / user ID that I chose, for some reason, the procedure did not work. It remained unclear what to write in offset and in post_count ( i.stack.imgur.com/YbJVV.png ), so that all posts of the user specified in user_id without gaps are covered. In the second example, the following error when trying to run: i.imgur.com/KglPnqh.png And when trying to save a procedure: i.imgur.com/xAc0pCf.png - iskander1220
  • (to the second example) This code is for js. It is not for inserting it into procedures or execute. Your code here is quite working. This link to the answer in another question explains everything very clearly, how and what to do with this code. You need to pass arguments to execute.getCommentsFromPosts . For example, execute.getCommentsFromPosts?post_count=10&offset=0&user_id=1&owner_id=1 - Mr Morgan
  • And also I ask you to correct the question - Mr Morgan
  • Clearly, it means I did not understand the instructions from Habra, where the code was linked to execute . Otherwise, the questions are caused by non-standard behavior of the team (performed by the button from the β€œStored Procedures”) in two communities: in one community, if post_count specify 25 in the post_count field (without filling in the user_id and offset fields) - the Runtime error occurred during code window is displayed invocation: Too many operations ". In another community, where there are fewer comments and activities, the team works on 25, although the first one will earn only 5. What can this be connected with? - iskander1220

1 answer 1

You need to use the execute method

The documentation for this method is very clear that this method allows you to perform up to 25 requests to the API in 1 request to the method.

As far as I understand you, you need to collect all comments from the post (if not, write in the comments).
But here you need to immediately put such restrictions:
Because requests are only 25 , therefore we can get 25 * 100 comments. ( 100 because for 1 request to wall.getComments we will receive no more than 100 comments )

The next item is to make a code that will get these 2500 comments from a certain post.

 var post_id = Args.post_id; var owner_id = Args.owner_id; var count = Args.count; var offset = Args.offset; if(post_id == null || owner_id == null){ return {"count": 0, "items": []}; // Если ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ Π½Π΅ Π·Π°Π΄Π°Π½Ρ‹, Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌ пустой массив } if(count == null){ count = 10; // Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ } if(offset == null){ offset = 0; // Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ } var i = 0; var items = []; while(i < 25 && count > items.length){ var cur_count = count - items.length; if(cur_count > 100){ cur_count = 100; } items = items + API.wall.getComments({"owner_id": owner_id, "post_id": post_id, "offset": offset+100*i, "count": cur_count})["items"]; i = i + 1; } return {"count": items.length, "items": items}; 

This code can be passed as an argument to execute (the code argument), and along with this argument, the post_id , ower_id and others.
The second option is to create a procedure in your application and already call with the token received for your application to call this procedure with the parameters owner_id , post_id and the rest.

How to get more than 2500 comments?

To do this, you just need to call this code several times with different parameters of the offset (by constantly increasing it by 2500)
Python3 example :

 from math import ceil import json,time import vk_api vk = vk_api.VkApi(token='...') # Π’Π°Ρˆ Ρ‚ΠΎΠΊΠ΅Π½ api = vk.get_api() posts = api.wall.getById(posts='{owner_id}_{post_id}'.format(owner_id='<owner_id>', post_id='<post_id>')) # Π“Π΄Π΅ <owner_id> - id ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Ρ/Π³Ρ€ΡƒΠΏΠΏΡ‹, post_id - id поста. *см. ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΈ послС ΠΊΠΎΠ΄Π° if not posts: print('No posts found!') post = posts[0] posts = [] for offset in range(ceil(post['comments']['count'] // 2500)): posts.extend(api.execute.getCommentsFromPost(owner_id='<owner_id>', post_id='<post_id>', offset=2500*offset, count=2500)['items']) # Ρ‚ΡƒΡ‚ всё ΠΏΠΎ Π°Π½Π°Π»ΠΎΠ³ΠΈΠΈ, ΠΊΠ°ΠΊ ΠΈ Π² ΠΏΠ΅Ρ€Π²Ρ‹ΠΉ Ρ€Π°Π· time.sleep(0.34) # ΠΈΠ·Π±Π΅Π³Π°Π΅ΠΌ flood control with open('output.json', 'w') as file: json.dump(posts, file) 

It should be noted that community id should be specified with a minus, for example -1 .
It is also worth mentioning that by repeatedly invoking the same method, the VC blocks access to it.

  • Now I started testing your first code for execute and so I understand, it is only once able to return 100 comments - that is, the same amount as the standard command returns without using execute ? If it does not work in a cycle and does not receive 25 times 100 each - what is the point of using it? - iskander1220
  • Why 100? It should be no more than 2500 - Mr Morgan
  • I apologize. I'll fix it now. I forgot 1 letter - Mr Morgan
  • one
    Not. 0, 2500, 5000, 7500 and so on - Mr Morgan
  • one
    4 line below. Add ..., "need_likes":1} - Mr Morgan