Hive Developer logo

Hive Developer Portal

PY: Get Post Details

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.

Intro

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.

Also see:

Steps

  1. App setup - Library install and import
  2. Post list - List of posts to select from created filter
  3. Post details - Get post details for selected post
  4. Print output - Print results in output

1. App setup

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()

2. Post list

Next we will fetch and make list of posts and setup pick properly.

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 index and option variables.

3. Post details

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 author and permlink fields to fetch fresh post details.

details = Comment(option)

4. Print output

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.; json()["created"]), 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

  1. review dev requirements
  2. git clone https://gitlab.syncad.com/hive/devportal.git
  3. cd devportal/tutorials/python/05_get_post_details
  4. pip install -r requirements.txt
  5. python index.py
  6. After a few moments, you should see output in terminal/command prompt screen.