Hive Developer Portal
Fetch list of comments made by account on posts or comments.
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.
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.
- get discussions
- App setup - Library install and import
- Post list - List of posts to select from created filter
- Comments list - Get comments list made by selected account
- 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 h = Hive()
Next we will fetch and make list of accounts and setup
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 for post in posts: 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
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)
start_author variable in query should be account name so that
get_discussions_by_comments can provide us current information.
Next, we will print result, comments of selected account and details of each comment.
# print comment details for selected account for comment in comments: pprint.pprint(comment) pprint.pprint("Selected: " + option)
The example of result returned from the service is a
JSON object with the following properties:
<Comment @happyme/qp4kl6> <Comment @happyme/qp4fiv> <Comment @happyme/qp4f2s> <Comment @happyme/qp461s> <Comment @happyme/qp40tt> 'Selected: happyme'
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.
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.