Hive Developer logo

Hive Developer Portal

PY: Submit Post

How to submit post on Hive blockchain using Python.

Full, runnable src of Submit Post can be downloaded as part of: tutorials/python.

In this tutorial will explain and show you how to submit a new post to the Hive blockchain using the commit class found within the steem-python library.

Intro

The Hive python library has a built-in function to transmit transactions to the blockchain. We are using the post method found within the commit class in the the library. It should be noted that comments and new post are both treated as commit.post operation with the only difference being that a comment/reply has got an additional parameter containing the parent post/comment. There are 11 parameters within the post method:

  1. title - The title of the post
  2. body - The body of the post
  3. author - The account that you are posting from
  4. permlink - A unique adentifier for the
  5. tags - Between 1 and 5 key words that defines the post
  6. reply_idendifier - Identifier of the parent post(used for comments)
  7. json_metadata - JSON meta objec that can be attached to the post
  8. comment_options - JSON options object that can be attached to the post to specify additional options like ‘max_payouts’, ‘allow_votes’, etc.
  9. community - Name of the community you are posting into
  10. beneficiaries - A list of beneficiaries for posting reward distribution.
  11. self_vote - Upvote the post as author right after posting

We will only be using the first 5 parameters as these are the only ones required to create a basic post. If you want to explore the other parameters further you can find more information HERE.

Steps

  1. App setup - Library install and import. Connection to Hive node
  2. Variable input and format - Input and creation of varialbes
  3. Post submission and result - Committing of transaction to the blockchain

1. App setup

In this tutorial we use 4 packages:

We import the libraries, connect to the testnet and initialize the Hive class.

import random
import string
import steembase
import steem

steembase.chains.known_chains['HIVE'] = {
    'chain_id': '79276aea5d4877d9a25892eaa01b0adf019d3e5cb12a97478df3298ccdd01673',
    'prefix': 'STX', 'hive_symbol': 'HIVE', 'hbd_symbol': 'HBD', 'vests_symbol': 'VESTS'
}

#connect node and private posting key
client = steem.Hive(nodes=['https://testnet.steem.vc'], keys=['5JEZ1EiUjFKfsKP32b15Y7jybjvHQPhnvCYZ9BW62H1LDUnMvHz'])

Because this tutorial alters the blockchain we have to connect to the testnet. We also require the private posting key of the contributing author in order to commit the post which is why it is specified along with the testnet node. We have supplied a test account, cdemo to use with this tutorial.

2. Variable input and format

The first three variables are captured via a simple string input while the tags variable is captured in the form of an array.

#capture variables
author = input('Username: ')
title = input('Post Title: ')
body = input('Post Body: ')

#capture list of tags and separate by " "
taglimit = 2 #number of tags 1 - 5
taglist = []
for i in range(1, taglimit+1):
	print(i)
	tag = input(' Tag : ')
	taglist.append(tag)

The tags parameter needs to be in the form of a single string with the words split by an empty space, so we add a line to prepare that variable. We also use a random generator to create a new permlink for the post being created

" ".join(taglist) #create string joined with empty spaces

#random generator to create post permlink
permlink = ''.join(random.choices(string.digits, k=10))

The random generator is limited to 10 characters in this case but the permlink can be up to 256 bytes. If the permlink value is left empty then it auto creates a permlink based on the title of the post. The permlink is unique to the author only which means that multiple authors can have the same title for the thier post.

3. Post submission and result

The last step is to transmit the post through to the blockchain. This is done post method within the commit class. All the defined parameters are submitted with the function. As stated earlier in the tutorial, there are quite a few parameters for this function but for a basic post these 5 are the most important.

client.commit.post(title=title, body=body, author=author, tags=taglist, permlink=permlink)

print("Post created successfully")

A simple confirmation is printed on the screen if the post is committed successfully.

You can also check on the testportal for the post.

To Run the tutorial

  1. review dev requirements
  2. git clone https://gitlab.syncad.com/hive/devportal.git
  3. cd devportal/tutorials/python/10_submit_post
  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.