Hive Developers logo

Hive Developer Portal

Get Account Replies

Fetching the replies written to a particular account.

Full, runnable src of Get Account Replies can be downloaded as part of: tutorials/python (or download just this tutorial: devportal-master-tutorials-python-08_get_account_replies.zip).

Tutorial will explain and show you how to access the Hive blockchain using the beem library to fetch a list of comments made on a specific accounts content.

Intro

In Hive there are built-in functions in the library beem that we are going to use throughout all Python tutorials. For this one we are using the reply_history function.

Also see:

Steps

  1. App setup - Library install and import
  2. Post list - List of filters to select from
  3. Comment details - Form a query
  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
from beem.account import Account

h = Hive()

2. Post list

Next we will fetch and make a list of posts and setup pick properly.

q = Query(limit=2, tag="")
d = Discussions()

#post list for selected query
#we are merely using this to display the most recent posters
#the 'author' can easily be changed to any value within the 'reply_history' function

posts = d.get_discussions('created', q, limit=2)

title = 'Please choose author: '
options = []
#posts list
for post in posts:
  options.append(post["author"])

# get index and selected filter name
option, index = pick(options, title)

# option is printed as reference
pprint.pprint("Selected: " + option)

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. We will also print the selection on screen for easy reference.

3. Comment details

Next we will allocate variables to make the function easier to use as well as provide a limit for the number of replies that we want to print. To retreive the replies we only need the author object. This is then used in the reply_history function present in the beem library.

# in this tutorial we are showing usage of reply_history of post where the author is known

# allocate variables
_author = Account(option)
_limit = 1

# get replies for specific author
replies = _author.reply_history(limit=_limit)

4. Print output

Next, we will print the details obtained from the function by iterating the array.

# print specified number of comments

for reply in replies:
  pprint.pprint(reply.body)

Final code:

import pprint
from pick import pick
# initialize Hive class
from beem import Hive
from beem.discussions import Query, Discussions
from beem.account import Account

h = Hive()
q = Query(limit=2, tag="")
d = Discussions()

#post list for selected query
#we are merely using this to display the most recent posters
#the 'author' can easily be changed to any value within the 'reply_history' function

posts = d.get_discussions('created', q, limit=2)

title = 'Please choose author: '
options = []
#posts list
for post in posts:
  options.append(post["author"])

# get index and selected filter name
option, index = pick(options, title)

# option is printed as reference
pprint.pprint("Selected: " + option)

# in this tutorial we are showing usage of reply_history of post where the author is known

# allocate variables
_author = Account(option)
_limit = 1

# get replies for specific author
replies = _author.reply_history(limit=_limit)

# print specified number of comments

for reply in replies:
  pprint.pprint(reply.body)



To Run the tutorial

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