create account

My Hive Blog Feeds Are Now Embedded In My Portfolio. by rufans

View this thread on: hive.blogpeakd.comecency.com
· @rufans · (edited)
$3.03
My Hive Blog Feeds Are Now Embedded In My Portfolio.
Hello buddies! My journey to building on Hive has just begun. Yesterday, and the day before, I learned how to use the [how to pull blog posts with the ```Dhive```library](https://developers.hive.io/tutorials-javascript/blog_feed.html).to fetch blog entries yesterday and the day before. I literally grinned while working with the code throughout the course since I chose to use Javascript rather than Python to develop :).

The author suggested that after finishing the course, one might integrate hive blog articles on websites. And soon once I realized that this idea was perfect for me because I've always wanted to showcase my Hive blog pieces in my portfolio. So I commenced...

### Installing The Needed Npm Package
---

Before the tutorial, I assumed javascript's **Fetch** method would be used to retrieve hive blog feeds. I was shocked to learn that this is not the case.

 Fetching hive blog feeds would require one to install a package known as **[@hiveio/dhive](https://www.npmjs.com/package/@hiveio/dhive)**
> @hiveio/dhive is a JavaScript library for interfacing with the Hive blockchain. It provides a simple and easy-to-use API for working with Hive transactions, blocks, accounts, and other data.


### Importing  Dhive Client

This tutorial doesn't require a bunch of imports as fetching blog feed requires just the import below. 

```const { Client } = require("@hiveio/dhive");```

```Client ``` alone doesn't have the ability to communicate with the hive blockchain as it expects a node URL. The tutorial stated that one could run their node to connect via WebSockets. But because I don't have any experience of such, I went with using the popular live node most of y'all might be familiar with ```https://api.hive.blog```;

```const client = new Client('https://api.hive.blog');```

After declaring an instance of the Client with a node URL, we can now access every detail of a post using the **client** variable.

#### Understanding Fetch Aata And Format
---
 I learned that ```condenser_api.get_discussions_by_*``` followed by the discussion type is a way to fetch a specific discussion type on the hive blockchain. Let's say we need to fetch a blog discussion, we have to do it this way```condenser_api.get_discussions_by_blog```. However,  ```Dhive package``` has a different way it accepts the discussion type. instead of the above style, we do it this way;  ``` client.database.getDiscussions("blog", query)```.

As you can see, we made use of the client variable declared before now to access the database, the **getDiscussions()**, which then accept discussion type, 'blog' in this case, and query which is an object.

As stated, the query is an abject with a tag, and a limit defined. For the tag, just as the name states, it's used to **Get** specific posts based on the tag provided. But in this case, we are using it to fetch for user's post.   So, I passed in my username ('rufans'). The limit is the number of posts to fetch, as shown below.

```
 client.database
    .getDiscussions("blog", {
    tag: 'rufans', 
    limit: 3
})
    .then((result) => {
console.log(result)
}
````

### Writing Them In NextJs

Just in case you haven't noticed, the above code wasn't written in async await but rather in promise. Since I was going to fetch my post to my portfolio, I got the opportunity to rewrite the function the way I would like. I approached it with async await;

```
// connecting to hive blogchain
  const [posts, setPosts] = useState([]);
  const getUserPost = async () => {
    const post = [];
    const data = await client.database.getDiscussions("blog", {
      tag: "rufans",
      limit: 3,
    });
    data.map((postDetails) => {
      //   setPosts(postDetails);
      post.push(postDetails);
      setPosts(post);
    });
  };

```
 Because  I was using nextjs, a reactjs framework, I made sure to utilize the useState() hook, which is why I set the posts details inside **setPosts(post)**.

#### Displaying And Formating the result

At this point, I was almost done with everything. It was left to map over my post because it's an array.

```
posts?.map((post) => {
          const json = JSON.parse(post.json_metadata);
          const postMarkDown = post.body;
          const htmlbody = markdown.render(postMarkDown);
          const postBody = stripHtml(htmlbody.substring(0, 200)).result;
          const postDate = new Date(post.created).toDateString();
}
```
I faced a little issue where the post body was still in markdown format. I had to install these libraries to convert the post to plaintext.

```
const markdown = require("markdown-it")();
import { stripHtml } from "string-strip-html";
```
 

---
Finally, I had my posts displayed inside these cards. Each of the cards can be clicked which then navigates to the original post on hive.
<center>
![image.png](https://images.hive.blog/DQmcdGSJXseSxw4pH79HhQmF5LBRPRz559pg3n5YmWnWgca/image.png)</center>

That's all for this post. Next, I will be working on Hive authentication.
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 216 others
properties (23)
authorrufans
permlinkmy-hive-blog-feeds-are-now-embedded-in-my-portfolio
categoryhive-169321
json_metadata{"tags":["hivedevs","leofinance","neoxian","waivio","programming","palnet","vyb"],"users":["hiveio"],"image":["https://images.hive.blog/DQmcdGSJXseSxw4pH79HhQmF5LBRPRz559pg3n5YmWnWgca/image.png"],"links":["https://developers.hive.io/tutorials-javascript/blog_feed.html"],"app":"hiveblog/0.1","format":"markdown"}
created2022-12-16 22:49:42
last_update2022-12-17 00:28:48
depth0
children3
last_payout2022-12-23 22:49:42
cashout_time1969-12-31 23:59:59
total_payout_value1.533 HBD
curator_payout_value1.493 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,991
author_reputation101,081,674,466,198
root_title"My Hive Blog Feeds Are Now Embedded In My Portfolio."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,169,629
net_rshares7,753,704,976,256
author_curate_reward""
vote details (280)
@hivebuzz ·
Congratulations @rufans! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

<table><tr><td><img src="https://images.hive.blog/60x70/http://hivebuzz.me/@rufans/upvotes.png?202212180908"></td><td>You distributed more than 19000 upvotes.<br>Your next target is to reach 20000 upvotes.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@rufans) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



**Check out our last posts:**
<table><tr><td><a href="/hive-102201/@hivebuzz/wc2022-recap-final-day1"><img src="https://images.hive.blog/64x128/https://i.imgur.com/uNPMgnd.png"></a></td><td><a href="/hive-102201/@hivebuzz/wc2022-recap-final-day1">HiveBuzz World Cup Contest - Recap of 3rd Place match</a></td></tr><tr><td><a href="/hive-139531/@hivebuzz/proposal-2324"><img src="https://images.hive.blog/64x128/https://i.imgur.com/RNIZ1N6.png"></a></td><td><a href="/hive-139531/@hivebuzz/proposal-2324">The Hive Gamification Proposal Renewal</a></td></tr></table>

###### Support the HiveBuzz project. [Vote](https://hivesigner.com/sign/update_proposal_votes?proposal_ids=%5B%22248%22%5D&approve=true) for [our proposal](https://peakd.com/me/proposals/248)!
properties (22)
authorhivebuzz
permlinknotify-rufans-20221218t092339
categoryhive-169321
json_metadata{"image":["http://hivebuzz.me/notify.t6.png"]}
created2022-12-18 09:23:39
last_update2022-12-18 09:23:39
depth1
children0
last_payout2022-12-25 09:23:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,386
author_reputation369,834,706,217,582
root_title"My Hive Blog Feeds Are Now Embedded In My Portfolio."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,199,984
net_rshares0
@poshtoken ·
https://twitter.com/801010600755286017/status/1603886313375137797
<sub> The rewards earned on this comment will go directly to the people sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.</sub>
properties (22)
authorposhtoken
permlinkre-rufans-my-hive-blog-feeds-are-now-embedded-in-my-portfoli86844
categoryhive-169321
json_metadata"{"app":"Poshtoken 0.0.1","payoutToUser":[]}"
created2022-12-16 22:57:27
last_update2022-12-16 22:57:27
depth1
children0
last_payout2022-12-23 22:57:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length253
author_reputation5,884,938,676,724,306
root_title"My Hive Blog Feeds Are Now Embedded In My Portfolio."
beneficiaries
0.
accountreward.app
weight10,000
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id119,169,906
net_rshares0
@stemsocial ·
re-rufans-my-hive-blog-feeds-are-now-embedded-in-my-portfolio-20221217t044704391z
<div class='text-justify'> <div class='pull-left'>
 <img src='https://stem.openhive.network/images/stemsocialsupport7.png'> </div>

Thanks for your contribution to the <a href='/trending/hive-196387'>STEMsocial community</a>. Feel free to join us on <a href='https://discord.gg/9c7pKVD'>discord</a> to get to know the rest of us!

Please consider delegating to the @stemsocial account (85% of the curation rewards are returned).

You may also include @stemsocial as a beneficiary of the rewards of this post to get a stronger support.&nbsp;<br />&nbsp;<br />
</div>
properties (22)
authorstemsocial
permlinkre-rufans-my-hive-blog-feeds-are-now-embedded-in-my-portfolio-20221217t044704391z
categoryhive-169321
json_metadata{"app":"STEMsocial"}
created2022-12-17 04:47:03
last_update2022-12-17 04:47:03
depth1
children0
last_payout2022-12-24 04:47:03
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length565
author_reputation22,933,079,831,358
root_title"My Hive Blog Feeds Are Now Embedded In My Portfolio."
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id119,174,639
net_rshares0