Hive Developer logo

Hive Developer Portal

PY: Get Account Comments

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.

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.

Also see:

Steps

  1. App setup - Library install and import
  2. Post list - List of posts to select from created filter
  3. Comments list - Get comments list made by selected account
  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

h = 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
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 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
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.


Try it

Click the play button below:

To Run the tutorial

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