[Tutorial] How to check if a Steem Post is pending initial payout with Node.js
utopian-io·@aley·
0.000 HBD[Tutorial] How to check if a Steem Post is pending initial payout with Node.js
 __What you will learn__ - how to initialize a NPM project - how to install [Steem.js](https://github.com/steemit/steem-js) using [NPM](https://www.npmjs.com) - how to use the [steem NPM package](https://www.npmjs.com/package/steem) 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](https://nodejs.org/en/) and [NPM](https://www.npmjs.com) 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](https://gist.github.com/steemcode/b650283130d9efb194d5544c8435b141) Thanks for reading :) _node.js image by [pixabay](https://pixabay.com/en/node-js-logo-nodejs-javascript-736399/)_ <br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@aley/tutorial-how-to-check-if-a-steem-post-is-pending-initial-payout-with-node-js">Utopian.io - Rewarding Open Source Contributors</a></em><hr/>