Historically, applications that wanted to retrieve comments written by a particular account would use get_state. But this method has been scheduled for deprecation. So we’ll use a more supported approach in this tutorial using get_account_history.
To request the latest comments by a particular author, we can use the get_account_history method:
api=Radiator::Api.newapi.get_account_history(account_name,-1,10000)do|history|history.eachdo|index,item|type,op=item.opnextunlesstype=='comment'nextifop.parent_author.empty?# skip postsnextifop.parent_author==account_name# skip replies to account# .# ... your code here# .endend
Notice, the above example request up to 10,000 operations from history, starting from the oldest. From these results, we iterate on each item in history to locate a) type of comment, and b)parent_author that do not match the account_name.
Example api call
If we want to get the comments by user @lordvader …
From the example we get the following output from our script:
.
.
.
Reply to @saarliconvalley in discussion: "The Empire has sent you a friend request."
body_length: 33 (7 words)
replied at: 2018-03-27T16:02:45
net_votes: 0
https://steemit.com/@lordvader/re-saarliconvalley-re-lordvader-2018327t16025594z-20180327t160243538z
Reply to @teenovision in discussion: "The Empire has sent you a friend request."
body_length: 90 (16 words)
replied at: 2018-03-27T15:53:39, updated at: 2018-03-30T17:25:18, active at: 2018-03-30T17:25:18
net_votes: 0
https://steemit.com/@lordvader/re-teenovision-re-lordvader-the-empire-has-sent-you-a-friend-request-20180327t155339532z
Reply to @gtg in discussion: "gtg witness log"
body_length: 130 (25 words)
replied at: 2018-04-06T04:29:00
net_votes: 2
https://steemit.com/@lordvader/re-gtg-ffdhu-gtg-witness-log-20180406t042906872z
Comment fields
Comments in the results of get_account_history will only return the following fields:
parent_author
parent_permlink
author
permlink
title
body
json_metadata
In our example script, we want more detail than this, so for every comment, we call get_content to retrieve more detail. For a full explanation of the results provided by get_content, please refer to the tutorial: Get Post Details
To Run
First, set up your workstation using the steps provided in Getting Started. Then you can create and execute the script (or clone from this repository):
<account-name>
git clone git@github.com:steemit/devportal-tutorials-rb.git
cd devportal-tutorials-rb/tutorials/09_get_account_comments
bundle install
ruby get_account_comments.rb <account-name>
RB: Get Account Comments
Fetching the comments written by a particular account.
Full, runnable src of Get Account Comments can be downloaded as part of the RB tutorials repository.
Historically, applications that wanted to retrieve comments written by a particular account would use
get_state
. But this method has been scheduled for deprecation. So we’ll use a more supported approach in this tutorial usingget_account_history
.Sections
Making the api call
To request the latest comments by a particular author, we can use the
get_account_history
method:Notice, the above example request up to 10,000 operations from history, starting from the oldest. From these results, we iterate on each item in history to locate a) type of
comment
, and b)parent_author
that do not match theaccount_name
.Example api call
If we want to get the comments by user @lordvader …
Example api call using script
And to do the same with our tutorial script
Example Output
From the example we get the following output from our script:
Comment fields
Comments in the results of
get_account_history
will only return the following fields:parent_author
parent_permlink
author
permlink
title
body
json_metadata
In our example script, we want more detail than this, so for every
comment
, we callget_content
to retrieve more detail. For a full explanation of the results provided byget_content
, please refer to the tutorial: Get Post DetailsTo Run
First, set up your workstation using the steps provided in Getting Started. Then you can create and execute the script (or clone from this repository):
<account-name>