Steem-python and piston Tutorial Series: #1 - A simple yet powerful voting bot.

in #steem-dev7 years ago (edited)

Hello all,

This is the first installment in a new tutorial series that will familiarize programming beginners with the steem-python and piston libraries and how to use python to interact with the Steem blockchain! I decided that a good place to start would be a simple voting bot.

Before we begin, please note that the code formatting looks better on ChainBB, where this post was written. Read on ChainBB

As this is the first installment of the tutorial series, I will first go over how to install steem-python and piston on your machine, although this is not too challenging of a task. NOTE: I'm assuming in these instructions that you have python3 installed on your machine already. If not, here are tutorials to install it on Windows, Mac, and Linux. Be sure to have pip3 installed on your machine as well (the tutorials include information about this).

Once you have python3 installed on your machine, you are ready to install steem-python.

To do so, open cmd (Windows) or terminal (OSX, Linux) and type:

pip3 install steem (hit enter)
pip3 install piston-lib (hit enter)

Steem-python and piston are ready to be used!

The last thing we need to do to get started with steem-python and piston is install an IDE (integrated development environment) in which we will write and run our python code. If you do not already have one on your machine, I recommend PyCharm Community Edition by JetBrains (Download). It is free, easy to get started with and has more than enough functionality for what we will be doing in this tutorial series.

Now that we are ready to write our python code, let's get started.

First, we need to import the piston library into our code so we are able to use it.

from piston.steem import Steem

Note that to import the entire library, "Steem" would be replaced by an asterisk, but I have only imported what we will need to write this voting bot.

Next, we need to connect to the Steem blockchain and set what tags/authors we wish to vote on and the voting weight that we wish to use.

steem = Steem(wif = "wif posting key here")
tagsOrAuthors = ["introduction", "introduceyourself", "introducemyself"]
voteWeight = 0.05

(NOTE: that you must populate this list with either tags or authors, you cannot choose both)
In the above example, I have chosen to vote on all posts of introduction, introduceyourself and introducemyself. If you would rather vote on all posts by certain authors than all posts on certain tags, simply populate the list with certain authors, like so:

tagsOrAuthors = ["zcgolf16", "jesta", "lukestokes"]

This will set up the bot to vote on all posts by me, @jesta and @lukestokes.

Now that we have connected to the Steem blockchain and set the tags or authors that we wish to vote on, we are ready to build the bot itself.

for p in steem.stream_comments():
        for tag in tagsOrAuthors:
            try:
                # make p["tags"] into p["author"] if you are voting by author and not by tag
                if tag in p["tags"] and p.is_main_post():
                    vote = p.upvote(weight = +voteWeight, voter = "your username here")
                    print("Upvoted post by @"+vote["operations"][0][1]["author"]+" using account @"+vote["operations"][0][1]["voter"]+"!")
            except:
                print("Failed to vote on post.")

To begin, we are using the stream_comments() method from piston.steem to stream in the latest posts from the Steem blockchain and using a for loop to loop through them. Next, we are using another for loop to loop through each tag or author that we have set in the tagOrAuthors list. Next, we have set up a try/except statement to prevent any errors that we may encounter from crashing the program. Instead, if an error is raised, "Failed to vote on post." will be printed to the console and the program will continue. Continuing on, we check to make sure that the post is tagged with one of our preset tags or authored by one of our pre-set authors. We also check to make sure that the post is an actual post and not a comment.

If all of these requirements are met, the post will be voted on! Fill in the voter = "your username here" with the account whose posting WIF key you have provided. The final print() line will neatly print information about the vote that has been placed to the console.

The bot is now ready to be used! Ensure that you have filled in your WIF key and username where necessary, and filled in the other variables correctly. Once you have, run the bot, and if all is well, you should begin to see messages like Upvoted post by @samueljacobsmith using account @zcgolf16! pop up in your console! You may also see some Failed to vote on post. messages as well, but do not worry as this is a simple bot and it may run into some problems.

If I have made an error anywhere in this tutorial, please do not hesitate to let me know.

I hope that this tutorial is useful to you. If you find this useful or helpful to you, please apply an upvote or resteem so all of the steemit community can see and use this tutoriall! I don't have a huge following on steemit, so I need your help with spreading this tutorial to all who can be helped by it.

All of the code used in this tutorial is on GitHub: here.

download.png

Sort:  

Extent post thank you. Are there other code examples?

I will be releasing more tutorials in the coming weeks.

Holy Shit bro! I've been looking for a tutorial for weeks now and don't know how to get started. Followed and Upvoted! Eagerly waiting for your next series.

Nice post !! Thanks

I am a beginner in programming and this wil help me alot with understanding python - blockchain interaction. Thank you very much!

Resteem, followed, upvoted and ready to learn, sensei!

Hey @zcgolf ! Please contact me! I need help! I need to bots created and I'll pay

If you want to make an account on steemit.chat and private message me, my account is zcgolf16. I don't need a huge payment... it's not about the money as much as it is about educating others and the programming itself for me.

Can we maybe make it a group chat. Maybe in Discord or Telegram or in Steemit Chat if possible. It'd be great if we can share ideas and if pleople like marc is willing to sponsor you then the better.

@zcgolf love it! Say you know what'd be super profitable for you?! Writing a stratum proxy. That's right. Take a look at slush, etc. 85% of bitmain miners stuck mining antpool. They'd like to cross over to crypto. But existing repos are dated.

Just throwing this to you, because I like your work.

Wheres the MAC install post?

Nice post. You can more examples @naveendavisv

Hi there, I was using your guidelines and I find them very helpful, but I keep getting stuck. I imported the library but it still gives me a syntax error on every operation I am trying to execute. Is there something I am doing wrong? Are those codes only to implement in the terminal directly maybe?

from piston.steem import steem
piston categories --limit 10

then I execute this file by going to the terminal and executing

python3 <path>

Coin Marketplace

STEEM 0.32
TRX 0.11
JST 0.034
BTC 66269.58
ETH 3204.67
USDT 1.00
SBD 4.24