Hello. Both of the following codes return comments on the same post from the wall. The difference is that the first one returns 100 comments to the post once, and the second to 2500. The question is to transfer this 25-fold iteration to the first code, since with the first code you can also extract individual user’s comments on his ID and in addition - refer to the post without specifying its ID - by number, whereas the second code in the arguments requires an exact ID of the post.

CODE number 1:

var owner_id = Args.owner_id; var user_id = Args.user_id; var offset = Args.offset; var post_count = Args.post_count; if (post_count == null) post_count = 1; // НА ОБРАБОТКЕ 1 ПОСТ // Получаем список постов var posts = API.wall.get({ "owner_id": owner_id, "offset": offset, "count" : 100, }); var i = 0; var userComments = {}; while(i < posts.items.length && i < post_count) { var post_id = posts.items[i].id; var comments = API.wall.getComments({ "owner_id": owner_id, "post_id": post_id, "offset": 0, "count" : 100, // максимально возможное количество коментов, // кот. можно получить с одного вызова }); var j = 0; while(j < comments.items.length) { if (user_id == null || comments.items[j].from_id == user_id) userComments.push(comments.items[j]); j = j + 1; } i = i + 1; } return userComments; 

CODE number 2:

 var owner_id = Args.owner_id; var post_id = Args.post_id; var offset = Args.offset; var count = Args.count; 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, "need_likes":1})["items"]; i = i + 1; } return {"count": items.length, "items": items}; 

    1 answer 1

    I think something like the code below should work

    Passing notes

    • it's worth fixing the style: js usually uses camelCase in variable names, not python_style. You have mixed both styles (although the Python style prevails)

    • to compare values ​​in js, it’s better to use === , rather than ==

    • It may be worth tries_count as an input parameter ( Args.tries_count ): this will allow you to vary the number of Args.tries_count downloaded.

    • it would be worthwhile not stupidly to send 25 requests, but to check the answer, and getting an empty answer to stop loading the network

    I did not begin to change the code strongly for my vision, so that the changes made would be more visible / obvious, and besides, I am not in the context of your project; if you want, change it yourself. there is something to improve ...

     var owner_id = Args.owner_id; var user_id = Args.user_id; var offset = Args.offset; var post_count = Args.post_count; var comments_count = Args.comments_count; if (post_count == null) post_count = 1; // НА ОБРАБОТКЕ 1 ПОСТ // Получаем список постов var posts = API.wall.get({ "owner_id": owner_id, "offset": offset, "count" : 100, }); var postIndex = 0; var userComments = {}; while(postIndex < posts.items.length && postIndex < post_count) { var post_id = posts.items[postIndex].id; var tries_count = 25; var try_nmb = 0; var comments = []; while (comments.length < comments_count && try_nmb < tries_count) { comments = comments + API.wall.getComments({ "owner_id": owner_id, "post_id": post_id, "offset": offset+100*try_nmb, "count": 100, "need_likes":1})["items"]; try_nmb = try_nmb + 1; } var commentIndex = 0; while(commentIndex < comments.length) { if (user_id == null || comments[commentIndex].from_id == user_id) userComments.push(comments[commentIndex]); commentIndex = commentIndex + 1; } postIndex = postIndex + 1; } return userComments; 
    • Pavel, when trying to save a procedure, an error occurs about cur_count - radikal.kz/images/2018/03/13/BEZYMYNNYI1d970ed3e3604c1c.png Apparently, for the same reason, it does not start. Commenting out and replacing with comments_count and post_count - did not yield any result)) - iskander1220
    • really left cur_count in one place. corrected. - Pavel
    • And now he writes error in line 35: ';' expected, '+ =' found . This is the line: try_nmb + = 1; - iskander1220
    • heh brought back try_nmb = try_nmb + 1 - Pavel
    • Pavel, unfortunately, the code still once only displays a hundred comments to the post, although in the example there were not five hundred (and it was expected - up to two thousand five hundred). Checked like this: owner_id = -60312130 , offset = 0 , comments_count = 1 , left the rest of the fields empty. Sometimes there was an error with other numbers. - iskander1220