Hive Developer Portal
Get Follower And Following List
Get the followers of a user/author & the authors that user is following.
Full, runnable src of Get Follower And Following List can be downloaded as part of: tutorials/python (or download just this tutorial: devportal-master-tutorials-python-19_get_follower_and_following_list.zip).
This tutorial will explain and show you how to access the Hive blockchain using the beem library to fetch list of authors being followed or authors that a specified user is following.
Intro
We are using the get_followers
and get_following
functions that are built into the beem library. These functions allow us to query the Hive blockchain in order to retrieve either a list of authors that are being followed or a list of authors that are currently following a specified user. There are 4 parameters required to execute these functions:
- account - The specific user for which the follower(ing) list will be retrieved
- start follower(ing) - The starting letter(s) or name for the search query. This value can be set as an empty string in order to include all authors starting from “a”
- follow type - This value is set to
blog
and includes all users following or being followed by theuser
. This is currently the only valid parameter value for this function to execute correctly. - limit - The maximum number of lines that can be returned by the query
Also see:
Steps
- App setup - Library install and import
- Input variables - Collecting the required inputs via the UI
- Get followers/following Get the followers or users being followed
- Print output - Print results in output
1. App setup
In this tutorial we use 2 packages, pick
- helps us to select the query type interactively. beem
- hive library, interaction with Blockchain.
First we import both libraries and initialize Hive class
from pick import pick
# initialize Hive class
from beem import Hive
from beem.account import Account
hive = Hive()
2. Input variables
We assign two of the variables via a simple input from the UI.
# capture username
account = input("Username: ")
account = Account(account, blockchain_instance=hive)
# capture list limit
limit = input("Max number of followers(ing) to display: ")
limit = int(limit)
Next we make a list of the two list options available to the user, following
or followers
and setup pick
.
# list type
title = 'Please choose the type of list: '
options = ['Follower', 'Following']
# get index and selected list name
option, index = pick(options, title)
print("List of " + option)
This will show the two options as a list to select in terminal/command prompt. From there we can determine which function to execute. We also display the choice on the UI for clarity.
3. Get followers/following
Now that we know which function we will be using, we can form the query to send to the blockchain. The selection is done with a simple if
statement.
# create empty list
follows = []
# parameters for get_followers function:
# start_follower, follow_type, limit
if option=="Follower" :
follows = account.get_followers()[:limit]
else:
follows = account.get_following()[:limit]
The output is captured.
4. Print output
Next, we will print the result.
# check if follower(ing) list is empty
if len(follows) == 0:
print("No " + option + " information available")
exit()
print(*follows, sep='\n')
This is a fairly simple example of how to use these functions but we encourage you to play around with the parameters to gain further understanding of possible results.
Final code:
from pick import pick
# initialize Hive class
from beem import Hive
from beem.account import Account
hive = Hive()
# capture username
account = input("Username: ")
account = Account(account, blockchain_instance=hive)
# capture list limit
limit = input("Max number of followers(ing) to display: ")
limit = int(limit)
# list type
title = 'Please choose the type of list: '
options = ['Follower', 'Following']
# get index and selected list name
option, index = pick(options, title)
print("List of " + option)
# create empty list
follows = []
# parameters for get_followers function:
# start_follower, follow_type, limit
if option=="Follower" :
follows = account.get_followers()[:limit]
else:
follows = account.get_following()[:limit]
# check if follower(ing) list is empty
if len(follows) == 0:
print("No " + option + " information available")
exit()
print(*follows, sep='\n')
To Run the tutorial
- review dev requirements
git clone https://gitlab.syncad.com/hive/devportal.git
cd devportal/tutorials/python/19_get_follower_and_following_list
pip install -r requirements.txt
python index.py
- After a few moments, you should see output in terminal/command prompt screen.