# Hive Developer Portal

### PY: Follow A User

How to follow or unfollow an author using Python.

Full, runnable src of Follow A User can be downloaded as part of: tutorials/python.

In this tutorial we will explain and show you how to follow or unfollow any author on the Steem blockchain using the `commit` class found within the steem-python library.

## Intro

The Steem python library has a built-in function to transmit transactions to the blockchain. We are using the `follow` and `unfollow` methods found within the `commit` class in the the library. Before we can follow/unfollow we first have to check what the current ‘follow status’ is of the author. We use another function for this which is explained in the tutorial entitled `get_following_and_follower_list`. There are 3 parameters within the `follow/unfollow` methods:

1. follow/unfollow - The name of the author that will be followed/unfollowed
2. what - The list of states to be followed. Currently this defaults to `blog` as it’s the only option available on the block chain at this stage
3. account - The name of the account that is executing the follow/unfollow

## Steps

1. App setup - Library install and import. Connection to testnet
2. User information and steem node - Input user information and connection to Steem node
3. Check author status - Validity check on requested autor to follow
4. Follow status - Check whether specified author is already followed
5. Follow/Unfollow commit - Follow/unfollow commit to the blockchain

#### 1. App setup

In this tutorial we use 3 packages:

- `steem` - steem-python library and interaction with Blockchain
- `pick` - helps select the query type interactively

We import the libraries and connect to the `testnet`.

    import steembase
    import steem
    from pick import pick

    steembase.chains.known_chains['STEEM'] = {
        'chain_id': '79276aea5d4877d9a25892eaa01b0adf019d3e5cb12a97478df3298ccdd01673',
        'prefix': 'STX', 'steem_symbol': 'STEEM', 'sbd_symbol': 'SBD', 'vests_symbol': 'VESTS'
    }

Because this tutorial alters the blockchain we connect to the testnet so we don’t create spam on the production server.

#### 2. User information and steem node

We also require the `private posting key` of the user that wishes to follow a selected author in order to commit the action to the blockchain. This is why we have to specify this along with the `testnet` node. The values are supplied via the terminal/console before we initialise the steem class. We have supplied a test account, `cdemo` to use with this tutorial but any demo account set up on the testnet can be used.

    #capture user information
    username = input('Please enter your username: ')
    postingkey = input('Please enter your private posting key: ')

    #connect node and private posting key, demo account being used: cdemo, posting key: 5JEZ1EiUjFKfsKP32b15Y7jybjvHQPhnvCYZ9BW62H1LDUnMvHz
    s = steem.Steem(nodes=['https://testnet.steem.vc'], keys=[postingkey])

#### 3. Check author status

To insure the validity of the `follow` process, we first check to see if the `author` provided by the user is in fact an active username. This is done with a simple call to the blockchain which returns an array of all the user information. If the author does not exist, the return value is `null`.

    #capture variables
    author = input('Author to follow: ')

    #check author status
    result = s.get_account(author)

Once the author name is confirmed to be valid we can move on to check the follow status of that author.

#### 4. Follow status

If the author check comes back with a value we use a simple `if` statement to initialise the database query for the follow status. A comprehensive tutorial is available to retrieve a list of followers and users that are being followed in the tutorial specified in the `intro`. As we are only interested in a very specific author we can narrow the query results down to a single result. That result then determines what the available actions are.

    #result from previous step
    if result :
    	#check for author name in the current following list
    	follow = s.get_following(username, author, 'blog', 1)
    	if len(follow) > 0 and follow[0]['following'] == author :
    		title = "Author is already being followed, please choose action"
    	else:
    		title = "Author has not yet been followed, please choose action"
    else:
    	print('Author does not exist')
    	exit()

The result from the `follow` query is printed on the UI and the user is asked to select the next action to take based on that information. If the author does not exit the program exits automatically.

    #get index and selected action
    options = ['Follow', 'Unfollow', 'Exit']
    option, index = pick(options, title)

Once we know what the user wants to do, we can move on to the actual `commit` to the blockchain.

#### 5. Follow/Unfollow commit

Once the user has selected which action to take we user another `if` statement to execute that selection.

    if option == 'Follow' :
    	s.commit.follow(author, ['blog'], username)
    	print(author + ' is now being followed')
    else:
    	if option == 'Unfollow' :
    		s.commit.unfollow(author, ['blog'], username)
    		print(author + ' has now been unfollowed')
    	else:
    		print('Action Cancelled')

A simple confirmation of the chosen action is printed on the screen.

You can also check on the testportal for a list of the authors being followed by the demo account.

### To Run the tutorial

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