What you will learn
- how to initialize a NPM project
- how to install Steem.js using NPM
- how to use the steem NPM package to get the content and creation date of a steem post
- calculate when a steem post will be paid or if it was already paid out
What you require
Any operating system or environment with Node.js and NPM installed.
Your level of experience
Basic: You need to know how to use the command line interface of your OS.
Let's get started!
Open the console, create your prefered project directory and navigate into it.
> mkdir -p ~/Development/steem
> cd ~/Development/steem
Now initialize a NPM project by doing this
> npm init -y
It will basically create a package.json file for you. The -y option will use only defaults and not prompt you for any options.
Okay, now install Steem.js, so we can use it in your script.
> npm install --save steem
This command will install the steem package and any dependencies, if there are any. It will also --save Steem.js as dependency in the package.js project file.
Cool. Let's create the script file using your favorite editor.
> nano pending_payout.js
and put the logic there
var steem = require('steem');
var author = 'aley',
permlink = 'tutorial-how-to-install-angular-cli-and-show-steemit-posts-in-an-angular-web-app';
steem.api.getContent(author, permlink, function(err, post) {
var created_days_ago = Math.ceil(
(Date.now() - Date.parse(post.created)) / 24 /*hr*/ / 60 /*min*/ / 60 /*sec*/ / 1000 /*msec*/
);
console.log('The post: ' + post.title);
if (created_days_ago < 7) {
console.log('.. will be paid in ' + (7 - created_days_ago) + ' day(s).');
} else {
console.log('.. was paid ' + ((created_days_ago - 7) == 0 ? 'today.' : (created_days_ago - 7) + ' day(s) ago.'));
}
});
In the first line we include the Steem.js library, so we can use the Steem API further down the script. Then we set the author and permalink of the post we want to check. Now as everything is defined, we can call the getContent method of the steem.api to fetch the post info we need. That's the post creation date, so we can make the math.
var created_days_ago = Math.ceil(
(Date.now() - Date.parse(post.created)) / 24 /*hr*/ / 60 /*min*/ / 60 /*sec*/ / 1000 /*msec*/
);
Let's break it down. We subtract the current timestamp from the timestamp when the post was created and divide this figure by hours of day, minutes of hour, seconds of minute and milliseconds of second, so we have the difference between now and the creation date in days.
At last we simply check if the post is younger or older than 7 day and that's it :)
Feel free to fetch the snippet as Gist: steem-pending-payout.js
Thanks for reading :)
node.js image by pixabay
Posted on Utopian.io - Rewarding Open Source Contributors