Fetch comments made on each content or post using Python.
Full, runnable src of Get Post Comments can be downloaded as part of: tutorials/python (or download just this tutorial: devportal-master-tutorials-python-07_get_post_comments.zip).
This tutorial will explain and show you how to access the Hive blockchain using the beem library to fetch list of posts and get replies info on selected post.
Hive python library has built-in function to get active voters information if 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 post to get its active voters. get_content_replies function fetches list of replies on content. Note that get_discussions_by_hot filter is used for fetching 5 posts and after selection of post tutorial uses author and permlink of the post to fetch replies.
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
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 pick properly.
q = Query(limit=2, tag="")
d = Discussions()
#post list for selected query
posts = d.get_discussions('hot', q, limit=2)
title = 'Please choose post: '
options = 
for post in posts:
# 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.
Next we will replies on selected post with get_content_replies.
details = Comment(option)
# get replies for given post
replies = details.get_all_replies()
Next, we will print result, replies on selected post, selected post details and number of replies.
# print post details for selected post
pprint.pprint("Selected: " + option)
pprint.pprint("Number of replies: " + str(len(replies)))
The example of results returned from the service:
'Number of replies: 11'
From this result you have access to everything associated to the replies including content of reply, author, timestamp, etc., so that you can be use in further development of applications with Python.
Click the play button below:
git clone https://gitlab.syncad.com/hive/devportal.git
pip install -r requirements.txt