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.
Intro
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
importpprintfrompickimportpick# initialize Hive class
frombeemimportHivefrombeem.discussionsimportQuery,Discussionsfrombeem.commentimportCommenth=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('hot',q,limit=2)title='Please choose post: 'options=[]#posts list
forpostinposts: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. Replies list
Next we will replies on selected post with get_content_replies.
details=Comment(option)# get replies for given post
replies=details.get_all_replies()
4. Print output
Next, we will print result, replies on selected post, selected post details and number of replies.
# print post details for selected post
pprint.pprint(replies)pprint.pprint("Selected: "+option)pprint.pprint("Number of replies: "+str(len(replies)))
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.
Final code:
importpprintfrompickimportpick# initialize Hive class
frombeemimportHivefrombeem.discussionsimportQuery,Discussionsfrombeem.commentimportCommenth=Hive()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=[]#posts list
forpostinposts:options.append(post["author"]+'/'+post["permlink"])# get index and selected filter name
option,index=pick(options,title)details=Comment(option)# get replies for given post
replies=details.get_all_replies()# print post details for selected post
pprint.pprint(replies)pprint.pprint("Selected: "+option)pprint.pprint("Number of replies: "+str(len(replies)))
Get Post Comments
Fetch comments made on each content or post.
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.
Intro
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 thatget_discussions_by_hot
filter is used for fetching 5 posts and after selection of post tutorial usesauthor
andpermlink
of the post to fetch replies.Also see:
Steps
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
2. Post list
Next we will fetch and make list of posts and setup
pick
properly.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
andoption
variables.3. Replies list
Next we will replies on selected post with
get_content_replies
.4. Print output
Next, we will print result, replies on selected post, selected post details and number of replies.
The example of results returned from the service:
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.
Final code:
To Run the tutorial
git clone https://gitlab.syncad.com/hive/devportal.git
cd devportal/tutorials/python/07_get_post_comments
pip install -r requirements.txt
python index.py