Hive Developer Portal
Get post details from list of posts from the blockchain with
created filter and tag then display selected post details.
Full, runnable src of Get Post Details can be downloaded as part of: tutorials/python (or download just this tutorial: devportal-master-tutorials-python-05_get_post_details.zip).
We will explain and show you how to access the Hive blockchain using the beem library to fetch list of posts filtered by a filter and tag.
Hive python library has built-in function to get details of post with author and permlink as an argument. Since we don’t have predefined post or author/permlink. We will fetch post list from previous tutorial and give option to choose one option/post to get its details.
get_content function fetches latest state of the post and delivers its details. Note that
get_discussions_by_created filter is used for fetching 5 posts which by default contains details of each post, but for purpose of this tutorial we will showcase
get_content function to fetch details.
- App setup - Library install and import
- Post list - List of posts to select from created filter
- Post details - Get post details for selected post
- Print output - Print results in output
In this tutorial we use 3 packages,
pick - helps us to select filter interactively.
beem - hive library, interaction with Blockchain.
pprint - print results in better format.
First we import all three library and initialize Hive class
import pprint from pick import pick # initialize Hive class from beem import Hive from beem.discussions import Query, Discussions from beem.comment import Comment h = Hive()
Next we will fetch and make list of posts and setup
q = Query(limit=2, tag="") d = Discussions() #post list for selected query posts = d.get_discussions('created', q, limit=2) title = 'Please choose post: ' options =  #posts list for post in posts: options.append(post["author"] + '/' + post["permlink"]) # get index and selected filter name option, index = pick(options, title)
This will show us list of posts to select in terminal/command prompt. And after selection we will get index and post name to
Next we will fetch post details with
get_content. By default
get_discussions_by_created function already contains post details, but for this tutorial purpose we will ignore all other fields but only use
permlink fields to fetch fresh post details.
details = Comment(option)
Next, we will print result, details of selected post.
# print post body for selected post pprint.pprint('Depth: ' + str(details.depth)) pprint.pprint('Author: ' + details.author) pprint.pprint('Category: ' + details.category) pprint.pprint('Body: ' + details.body)
Also see: beem.comment.Comment
The example of result returned from the service:
'Depth: 0' 'Author: hiveio' 'Category: hive' ('Body: ' '\n' '\n' . . .
From this result you have access to everything associated to the post including additional metadata which is a
JSON string (e.g.;
active_votes (see: beem.comment.Comment.get_vote_with_curation) info, post title, body, etc. details that can be used in further development of applications with Python.
To Run the tutorial
- review dev requirements
git clone https://gitlab.syncad.com/hive/devportal.git
pip install -r requirements.txt
- After a few moments, you should see output in terminal/command prompt screen.