Steem.Js for dummies #5: Getting user's last poststeemCreated with Sketch.

in #utopian-io7 years ago

image.png

As usual, I'll try to keep it short and show you how to efficiently use SteemJs to get the last post (or last posts) that a given user posted on the Steem blockchain.

For this, we will need the following function:

steem.api.getDiscussionsByAuthorBeforeDate

Let's see what we can find in the Steem.js documentation:

image.png

First thing, contrarily to getAccounts function, we can notice that we can only check one account at a time here.

Therefore, if you have more than one account to check, do the following:

accounts.forEach(function (element,i,array){
    steem.api.getDiscussionsByAuthorBeforeDate(element,startPermlink,beforeDate,limit, ... );}

Let's now see about the other parameters.

  • startPermlink is the Permlink starting from which the results will be fetched. Here, we want the last post, so we won't use this option and simply input null. If you want to use it, for information, the permlink is not the whole URL but only what comes after the last '/'.

  • beforeDate cannot be null and as its name shows, the function will only return the results from before this date. Once again, since we want the last post, we will take the Date from now. It took me a while to understand the time format used there. It corresponds to the first part of an ISO String, but since I'm nice, I'm giving it for you to use directly ;)

beforeDate=new Date().toISOString().split('.')[0];
  • limit simply tells the blockchain how many results we want. Let's say we want 3 results:
accounts=["stoodkev","steem-plus"];
accounts.forEach(function (element,i,array){
    steem.api.getDiscussionsByAuthorBeforeDate(element,null, new Date().toISOString().split('.')[0],3 , function(err, result) {
        console.log(result);
    });
});

Let's take a look at the result:

image.png

We indeed got the three last posts of my two accounts.
To conclude, let's see some of the useful information found in there:

image.png

This screenshot only shows few of the data returned in the object but we can already see some useful information such as the the post's body , its list of active_votes and beneficiaries, its pending_payout_value and much more.

image.png

For some reason, replies and reblogged_by remain empty and necessitate another call to the blockchain.

Hope this helps!

@stoodkev



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

thanks @stoodkev just started JS on Steem need more guides for expert users :)

wow amazing post @stoodkev
if you can visit to my post

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @stoodkev I am @utopian-io. I have just upvoted you at 6% Power!

Achievements

  • WOW WOW WOW People loved what you did here. GREAT JOB!
  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • You are generating more rewards than average for this category. Super!;)
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Work on your followers to increase the votes/rewards. My vote is on the human input. Good luck!
  • Contribute more often to get higher and higher rewards. I want to see you often!
  • Wondering why other contributions got more? I introduced a competition factor. My vote is based also on how competitive the category used is.

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Is there any chance to get the accounts which reblogged a post? I'm using the JavaScript API of Steemit.

The only chance i see is searching in the post history of every user upvoted or commented the post.

HELP ME!!! 😁

Ah! I just found it!

steem.api.getRebloggedBy(author, permlink, function(err, result) {
console.log(err, result);
});

on this site

Hello @stoodkev. I found this article very useful. Thanks for that. I'm just getting my hands on Steem JS. I have a quick question. Is there a way I can get the list of all the posts that were posted after 5 days before.

Eg: Today's date is 21-04-2019.

I want all the posts that were posted after 16-04-2019. Is there a way we can do that?

Some reason this doesn't work anymore... Anyone know why?

{"name":"RPCError","code":-32603,"data":{"error_id":"e0c58d43-029c-4807-8208-c304a043ce15","jussi_request_id":"000106051135344114"}}