Steem js - Get all comments from an author using recursion.

Words
238
Reading
2 min
Listen
Play
7y

I'm sharing this with everyone because it was a bitch for me to figure out. There's not a lot of good documentation out there(at least for getting the comments) so I hope others looking to do the same thing will find this useful.

Objective:

The point of this code is to loop through all the author's comments or at least until you find what your looking for and stop.

Here I created a recursive function using getDiscussionsByCommentsAsync(). This API call returns a max of 100 comments at a time for a particular author. I'm passing two parameters - pAuthor and pPermLink

  • pAuthor is the username like wakeupnd

  • pPermLink is the permanent link the blockchain stores. Example: re-snaxteam-snax-integrates-with-steem-20190508t214059705z

Full URL: https://steemit.com/steem/@wakeupnd/re-snaxteam-snax-integrates-with-steem-20190508t214059705z

After the first 100 comments are called the function will use the last Permlink in the results and call getDiscussionsByCommentsAsync again to get the next 100 comments and will do this until you tell it to stop or it will loop through every single comment created by the author until there's none left.

function getAuthorsComments(pAuthor, pPermLink)
{
    Steem.api.getDiscussionsByCommentsAsync({
       "start_author": pAuthor,
       "limit": 100
        ,"start_permlink": pPermLink
  }).then((result) => {
       //get the next 100 comments
       getAuthorsComments(pAuthor, result[99].permlink);
   });
}



To call the recursive function add this to your main section of code.
Note: a null Permlink will return the newest comments.

//Loop though the comments of an author
getAuthorsComments("wakeupnd", null);



And that's pretty much it. I hope someone else will find this useful and save themselves a bunch of time. It took my a couple days of trying different API calls, reading blog and asking for help to get this to work.

Enjoy,
wakeupnd@wakeupnd

Steem js - Get all comments from an author using recursion. | Ecency