Beem Tutorials #1 - Introduction to Beem and creating a post with it

in #utopian-io6 years ago (edited)

Repository of Beem

https://github.com/holgern/beem

Tutorial Content

  • You will learn how to get started with Beem.
  • You will learn how to create a post via Beem.

Requirements

  • Python 2.7.x or 3.4+
  • Basic Python knowledge

Introduction

Beem is an alternative python library by @holger80 to interact with the STEEM blockchain.

After seeing it's constant development and getting very good feedbacks about the library, I have decided to switch to beem for my bots and scripts.

I will be learning Beem in the next couple of weeks and will write my experience on it as a tutorial series. Welcome to the first part of it.

Installation

Installing the library is already covered well in the official documentation. It supports both Python2.7+ and Python 3.4+ versions.

For the Osx, I have followed these steps to install:

$ mkvirtualenv -p python3.6 beemtutorials
$ pip install beem

Note: First line creates a virtual environment. It's the industry standard on python development at the moment. I highly suggest working
with virtual environments if you don't practise that way already.

Setting up the Steem instance

from beem import Steem
s = Steem()

print(s.get_blockchain_version())

You will see an output as 0.19.2.

This is the main class we use to interact with the blockchain. You can see the parameters and their definitions at source code/steem.py. However, you don't really need all of them. I will cover the important ones.

Setting a node

The first parameter is a list of nodes you want to connect. Let's run the same example with a different node:

from beem import Steem
s = Steem(
    node=["https://api.steemit.com",]
)

print(s.get_blockchain_version())

You will see the output has changed as 0.19.4 since api.steemit.com runs a newer version of steemd.

Setting keys
from beem import Steem
s = Steem(
    node=["https://api.steemit.com",],
    keys=["<private_key_1>", "<private_key_2>"]
)

Keys are private keys you have on steem accounts. If you want to create a post, you need posting private key entered here. However, storing keys raw in source codes may not be the best solution since you may push your
private keys to the repositories.

There are a couple of options to solve that issue:

  • Using environment variables
import os

from beem import Steem
s = Steem(
    node=["https://api.steemit.com",],
    keys=[os.getenv("POSTING_KEY"), ]
)

This example gets the posting key from environment variables. You need to set that POSTING_KEY variable before executing the code.

POSTING_KEY=<posting_key_of_account> python script.py
  • Using Beem's wallet

Beem has a wallet implementation to make account/wallet related options more practical.

The following code creates a wallet with unique-pass passphrase and adds a private key
into the wallet.

from beem import Steem
steem = Steem()
steem.wallet.create("unique-pass")
steem.wallet.unlock("unique-pass")
steem.wallet.addPrivateKey("<posting_wif>")

After doing that, whenever you need to broadcast something you can unlock the wallet by setting UNLOCK environment variable.

Note: If you already created a wallet and don't remember the password, you can wipe the old
wallet by

steem.wallet.wipe(True)

before creating a new wallet. Otherwise you will get an error states that there is already a wallet
exists.

You can also use the command line tool (beempy) ships with the package to perform
wallet related issues.

To create wallet:

$ beempy createwallet

To add private keys to the wallet:

$ beempy addkey <your_private_key>

Note: Beem stores wallet information on a SQLITE database. It's stored at ~/Library/Application\ Support/beem/beem.sqlite for my OSX setup. You can see where it's located from the steem.config object:

from beem import Steem
steem = Steem()
print(steem.config.data_dir)

Note: If you use Beem's wallet, you don't need to use the keys parameter
of Steem() since Beem will get keys from the unlocked wallet.

Creating a Post

You don't need any interface (Steemit, Busy, etc.) to create posts. Let's do it with Beem.

Note: Posts are actually are Comment objects in the steem blockchain. If a comment doesn't have any parent, that means it's a post.

I will explicitly set private posting key to keep things simple.

from beem import Steem
steem = Steem(
    keys=["<private_posting_wif>"]
)
steem.post(
    "My title for the post",
    "Beem is awesome! (post body)",
    author="beemtutorials",
    permlink="beemtutorials-test-post",
    app="myapp/0.0.1",
    tags="beem test test-post"
)

You can see the created post here. Let's see the possible parameters for the post:

The first two parameter are obviously post title and post body. However, post() functions can get lots of keyword arguments.

ParameterValue
authorAuthor of the post. Not needed if you set a default account via beempy.
permlinkPermlink of the post. Should be unique together with author and permlink. If not set, beem will auto-generate it from title.
reply_identifierIdentifier (Ex: @author/permlink) for the parent post. Not needed if you create a new post. Required for comments.
json_metadataCustom json metadata of the post. If you develop an app and need customized info, you can put it here.
comment_optionsSome options for the post. You can specify: max_accepted_payout, percent_steem_dollars, allow_curation_rewards etc.
communityThe community we're posting into. Example: Utopian was using "utopian" here. Overrides json_metadata.community.
appThe app used for creating the post. Busy, Steemit and other dapps puts their information here like "Busy/2.4.0"
tagsA list of tags for the post. Example: "steem steemit utopian beem"
beneficiariesA list of beneficiaries for reward sharing.
self_voteIf you set this as True, author will upvote the created post right after the creation.

Beneficiaries is an interesting option here, we will discuss it in the upcoming tutorials in this series.

Curriculum

Sample Codes

Samples


Thanks to @holger80 and @crokkon for the proof-reading and review.

Sort:  

Thank you for your contribution.

While I liked reading your tutorial, yet beem library is extensively well-documented throughout the reference you shared.
I was able to find quick and extensive reference for most of what is shared here.

I liked though that you pointed out some specifics for your OSX installation, and the outcome of the version difference was nice to learn about, in relevance to utilizing api.steemit.com node.

I would recommend for your future tutorials to try and come up with something more useful to the community, in terms of utilizing beem in ways not so clearly depicted in the official documentation.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

Thanks for the great feedback and review @mcfarhat. :)

Resteemed.
The upvote will follow soon:)

Nice work there.
This will be a fun series!

thanks, I want to post one tutorial every day, let's see if I can keep that pace :)

thanks for info

thanks for awesome tutorial dude.

Sweet. I've been looking for Beem tutorials.

Follow to see more upcoming days :)

Beem is certainly a better API than steem-python. I have already switched to Beem months ago. I started a little series but found the post payouts were too paltry to bother with continuing. Check out my blog and you could source some stuff from there. You may want to go back a couple of months.

Will check, thanks! I follow you normally but It seems I have missed that.

Finally, one of the most famous python developer on steem is starting to use beem :). Good work, I'm curious about the next posts.

Thanks for the kind words @holger80 :)

This is on my weekend to do list. I will build a game on steemit and this will greatly help me.

Please check out my post about the SBD price drop.

Hey @emrebeyler
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.029
BTC 66902.20
ETH 3248.49
USDT 1.00
SBD 2.64