Steem-Python for dummies #1

in #utopian-io6 years ago (edited)

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

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

.

I use mac(python 3.6) on my dev environment. For prod, same python version with ubuntu.

Updating the post, thank you for pointing it. :)

Cool, thanks for this tuto, I have a question, do you know if I can pull out one of my posts in steemit and put it in a web page or wordpress?

yes, it's possible. get_account_history includes operations related to a single account that might do the job. There is no plug and play solution though.

Qurator
Your Quality Content Curator
This post has been upvoted and given the stamp of authenticity by @qurator. To join the quality content creators and receive daily upvotes click here for more info.

Qurator's exclusive support bot is now live. For more info click HERE or send some SBD and your link to @qustodian to get even more support.

Merhaba kardeş geçen gün yaşadığımız diyaloğu karşılıklı unutarak discord a donmeni rica ederim. Ben kin sahibi olmayan üç günlük dünyada kalp kırmaya değecek birşey olmadığına inanan bir insanım kendi adıma seni kırdıysam özür dilerim.

Hey @emrebeyler I am @utopian-io. I have just upvoted you at 7% Power!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • I introduced a competition factor. My vote is based also on how competitive the category used is.

Human Curation

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Is that a typo? Shouldn't it be this?

from steem import Steem
s = Steem(
    nodes=["http://rpc.buildteam.io"], 
    keys=["posting_private_key", "active_private_key"]
)

with pip install -U Steem

i get (after some process) these:

...
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

----------------------------------------

Command "/usr/bin/python -u -c "import setuptools, tokenize;file='/tmp/pip-build-6p4nZG/scrypt/setup.py';f=getattr(tokenize, 'open', open)(file);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, file, 'exec'))" install --record /tmp/pip-uWPRkr-record/install-record.txt --single-version-externally-managed --compile --user --prefix=" failed with error code 1 in /tmp/pip-build-6p4nZG/scrypt/

Any ideas what went wrong and how i can solve this?

Birthdaypost !BEER



Hey @emrebeyler, here is a little bit of BEER from @isnochys for you. Enjoy it!