In this tutorial will explain and show you how to access the Hive blockchain using the beem library to fetch list of posts to randomize account list and get replies of selected account.
Intro
The beem library has built-in function to get comments list made by specific account. Since we don’t have predefined account list, we will fetch newly created posts and show their authors for selection and give option to choose one account to get its comments. The get_discussions_by_comments function fetches list of comments made by account. Note that get_discussions_by_created filter is used for fetching 5 posts and after selection of its author tutorial uses author of the post to fetch that account’s comments.
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,Discussionsh=Hive()
2. Post list
Next we will fetch and make list of accounts and setup pick properly.
q=Query(limit=2,tag="")d=Discussions()#author list from created post list to randomize account list
posts=d.get_discussions('created',q,limit=2)title='Please choose account: 'options=[]#accounts list
forpostinposts:options.append(post["author"])# get index and selected account name
option,index=pick(options,title)
This will show us list of accounts to select in terminal/command prompt. And after selection we will get account name as a option variable.
3. Comments list
Next we will form another query to get comments list of account
# 5 comments from selected author
q=Query(limit=5,start_author=option)# get comments of selected account
comments=d.get_discussions('comments',q,limit=5)
Note that start_author variable in query should be account name so that get_discussions_by_comments can provide us current information.
4. Print output
Next, we will print result, comments of selected account and details of each comment.
# print comment details for selected account
forcommentincomments:pprint.pprint(comment)pprint.pprint("Selected: "+option)
The example of result returned from the service is a JSON object with the following properties:
From this result you have access to everything associated to the comments of account including content of comment, timestamp, active_votes, etc., so that you can use in further development of your applications with Python.
Final code:
importpprintfrompickimportpick# initialize Hive class
frombeemimportHivefrombeem.discussionsimportQuery,Discussionsh=Hive()q=Query(limit=2,tag="")d=Discussions()#author list from created post list to randomize account list
posts=d.get_discussions('created',q,limit=2)title='Please choose account: 'options=[]#accounts list
forpostinposts:options.append(post["author"])# get index and selected account name
option,index=pick(options,title)# 5 comments from selected author
q=Query(limit=5,start_author=option)# get comments of selected account
comments=d.get_discussions('comments',q,limit=5)# print comment details for selected account
forcommentincomments:pprint.pprint(comment)pprint.pprint("Selected: "+option)
Get Account Comments
How to retrieve account comments from the Hive blockchain.
Full, runnable src of Get Account Comments can be downloaded as part of: tutorials/python (or download just this tutorial: devportal-master-tutorials-python-09_get_account_comments.zip).
In this tutorial will explain and show you how to access the Hive blockchain using the beem library to fetch list of posts to randomize account list and get replies of selected account.
Intro
The beem library has built-in function to get comments list made by specific account. Since we don’t have predefined account list, we will fetch newly created posts and show their authors for selection and give option to choose one account to get its comments. The
get_discussions_by_comments
function fetches list of comments made by account. Note thatget_discussions_by_created
filter is used for fetching 5 posts and after selection of its author tutorial usesauthor
of the post to fetch that account’s comments.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 accounts and setup
pick
properly.This will show us list of accounts to select in terminal/command prompt. And after selection we will get account name as a
option
variable.3. Comments list
Next we will form another query to get comments list of account
Note that
start_author
variable in query should be account name so thatget_discussions_by_comments
can provide us current information.4. Print output
Next, we will print result, comments of selected account and details of each comment.
The example of result returned from the service is a
JSON
object with the following properties:From this result you have access to everything associated to the comments of account including content of comment, timestamp, active_votes, etc., so that you can use in further development of your applications with Python.
Final code:
To Run the tutorial
git clone https://gitlab.syncad.com/hive/devportal.git
cd devportal/tutorials/python/09_get_account_comments
pip install -r requirements.txt
python index.py