Steem-JS Tutorials #2 - Getting Post Content

in #utopian-io8 years ago (edited)

What Will I Learn?

  • Get Post Content by User, permlink at by Trending

Requirements

Difficulty

  • Basic

Tutorial Contents

In this tutorial, I will learn you how to get post content by user / permlink / trending.

Curriculum

Get Post Content

#####Get posts by user

first create 2 variables, the first username, the second limit
(you need the variables just for comfort, you can to just put the info instead on the function)

limit - will limit the amount of posts we should get.

username - from which blogger the post come from.

const username = "lonelywolf", lim = 1;

steem.api.getDiscussionsByBlog({tag: username, limit: lim}, function(err, result){

console.log(err, result);

});

if all done correctly we should get this result -

image.png

result (compacted):
null (1) […]​0: Object { id: 42430471, author: "lonelywolf", permlink: "adding-jackpot-game-and-game-history-global-and-private", … }​length: 1​__proto__: Array [] localhost:14:85

(I used the browser version because I can't give a clear view of the console, you can use this both In the browser or on server-side [console])

from here you can do anything you want with the post, for example -

steem.api.getDiscussionsByBlog({tag: "lonelywolf", limit: 1}, function(err, result) {
  console.log("Title: " + result[0].title + ", Pending Payout: " + result[0].pending_payout_value + ", author: " + "lonelywolf" + ".");
});

result : Title: Adding JackPot game & game history, Pending Payout: 9.616 SBD, author: lonelywolf.

picture -

image.png

this is just cool example of what you can do.

Get post by permlink

to get a post by permlink first we need the actual permlink -

permlink is the permanent link of the post, for example, my is adding-jackpot-game-and-game-history-global-and-private, this is what comes after https://steemit.com/utopian-io/@lonelywolf/

create 2 variables, one for the permlink second username for the user who opened the post

const permlink = "adding-jackpot-game-and-game-history-global-and-private", username = "lonelywolf";

steem.api.getContent(username, permlink, function(err, result) {

console.log(err, result);

});

like the first one, this is the same, giving the post content just here it's by the permlink and author.
image.png

result:

null Object { id: 42248788, author: "lonelywolf", permlink: "some-shots-from-the-last-week-c1be138c4d8b1", category: "photography", parent_author: "", parent_permlink: "photography", title: "Some pictures from the last week!", body: "Hey steemians, in the past week I took some pictures, hope it's good :)\n\n\n\n \n\n\n\n \n\n---\n\n<center></center>", json_metadata: "{\"image\":[\"https://img.esteem.ws/\",\"\",\"\",\"\",\"\"],\"tags\":[\"photography\",\"esteem\",\"picture\",\"nature\",\"animal\"],\"app\":\"steemit/0.1\",\"format\":\"markdown\",\"community\":\"esteem\",\"users\":[\"lonelywolf\"]}", last_update: "2018-04-05T19:27:45", … } localhost:16:65

(this is another post example because my last one was pretty long so I needed to change the post permlink).

(pictures removed because it replaced with a full picture)

as the first one, you can do anything you want with the information.

Get Posts by Trending

to get a post by trending we need the tag we want to get the posts from and limit the count of posts, create this 2 variables tag - limit to make this process comfortable.

const Tag = "photography", Limit: 2;

and now get the posts

steem.api.getDiscussionsByTrending({tag: Tag, limit: Limit}, function(err, result){

console.log(result);

});

and if all correctly you should get this -
image.png

result (compacted):

(2) […]​0: Object { id: 42363974, author: "slowwalker", permlink: "bunhwangsa-pagoda-of-shilla-dynasty-unique-style-of-pagoda", … }​1: Object { id: 42517728, author: "velimir", permlink: "original-art-photography-series-by-velimir-254-another-world-another-time", … }​length: 2​__proto__: Array [] localhost:15:5

and like the other ones you can do anything you want from here, for example, upvote the posts or follow the authors etc.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

Submissions focused on the use of functions that are already well documented in the project documentation will be rejected.

https://github.com/steemit/steem-js/tree/master/doc#content

Submissions containing substantial instruction in ubiquitous functions (Save, Open, Print, etc.) or basic programming concepts (variables, operators, loops, etc.) will be rejected.

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

Thanks for the review, learned something for next time!

Hey @zonguin, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

You got a 14.80% upvote from @redlambo courtesy of @lonelywolf! Make sure to use tag #redlambo to be considered for the curation post!

Loading...

Coin Marketplace

STEEM 0.05
TRX 0.32
JST 0.083
BTC 62530.78
ETH 1673.29
USDT 1.00
SBD 0.42