Hello,
I am a long-time junkie of steem-python. I have discovered its edge cases, gotchas, capabilities in my steem related projects.
I am starting a new series of steem-python for dummies. This series will include my experiments, usage examples.
Introduction
steem-python is the official package for python developers to work with steem blockchain. It covers most of the operations and works like a charm overall.
Installation
In order to work with steem-python, you need to install python 3.6 or higher. Based on your operating system, you can read official howtos about installing python from here.
After you get your python installed, you can install steem-python. I suggest using virtualenvs for all of python projects, so I will continue with that.
Creating a Virtulenv
Virtual environments are isolated copies of Python and its standard library which
allows you to work on a specific project without worry of affecting other projects.
# create a directory to keep your virtual environments
mkdir /users/Emre/Environments
python3.6 -m venv mysteemproject /Users/emre/Environments/hellosteem
source /Users/emre/Environments/hellosteem/bin/activate
The source command activates the virtual environment in your shell. If you find virtual environment step confusing, you can skip these steps, but it's highly recommended.
Installing steem-python
pip install -U steem
You're all set. At this point, you're ready to work with steem blockchain via steem-python.
Getting Started
We need to initialize a Steem instance for starting.
from steem import Steem
s = Steem()
For -almost- all operations we will use this instance.
Steem calls takes some parameters. I will only talk about the two parameters I use mostly.
nodes
This is useful. By default, steem-python uses default public nodes. You can change it here if you want to connect a different public node. I am using http://rpc.buildteam.io for a while and it works very well.
keys
A list of private keys. Some operations require private keys of accounts. If you want to upvote a post via Steem instance, you need to put posting private key here.
If you want to transfer funds to another user, then you need to put active private key here also.
If you don't want to do anything requires a private key, then you don't need to fill these.
For example, I have a welcome bot for tr tag. It works with that configuration since it does upvotes and transfers.
from steem import Steem
s = Steem(
nodes=["http://rpc.buildteam.io",
keys=["posting_private_key", "active_private_key"]]
)
Real action
1. Creating Posts
We don't need Steemit, Busy, etc. to create posts. As long as you sign a Comment transaction as expected, you can broadcast your post to the blockchain from anywhere.
Tip: Posts are actually are Comment objects in the steem blockchain. If a comment doesn't have any parent, that means it's a post.
s.commit.post(
"this is my post title",
"this is my post body for testing purposes",
"author username",
tags=["test",]
)
- First parameter is your title.
- Second parameters is your post's body. Real content.
- Third parameter is your account username. (author)
- tags parameter is a list of tags related to your post.
This is the minimum example to post via steem-python. There are more parameters like
- reply_identifier
- json_metadata
- comment_options=None,
- community
- beneficiaries=None,
- self_vote
Most of them self-explanatory. But if you have a question about them, just shoot your question in the comments.
2. Upvoting Comments
s.commit.vote(
"username/permlink",
+10,
account="voter_account",
)
- First parameter is permlink. Ex: emrebeyler/a-sunday-in-mardin
- Second parameter is voting weight. Range: -100.0 - +100.0
- Last parameter is the account name in use for voting.
Tip: Negative values on weight parameter means downvoting(flagging).
Tip: At the time of this post is created, steem-python has a known bug which prevents voting for newly created posts that don't have a vote. It has a waiting pull request for a potential solution until it will be reviewed and merged to upstream, you can use this workaround:
from steem.post import Post
post = Post("emrebeyler/a-sunday-in-mardin")
post.upvote(weight=+10, voter="voter_account")
3. Making Transfers
s.commit.transfer(
"to_account",
amount,
"SBD",
memo="memo text",
account="sender"
)
It's pretty straight-forward and self-explanatory.
Tip: the Third parameter is the asset of the transfer. It can be SBD or STEEM.
4. Getting account data
account = Account('emrebeyler')
print(account)
This class includes all public data for accounts.
- Voting power
- Reputation value
- Balances
- Witness votes
- Account creation data
- Profile information (Location, avatar, website etc.)
and a bunch of things. See this link for my account's data sample.
This is all for now. We will dive into more advanced things in the future.
Feel free to ask questions or request topics about steem-python for the incoming posts. Also thanks stoodkev for inspiring this series.
Posted on Utopian.io - Rewarding Open Source Contributors