Learning Python by Making a Steemit Bot


For awhile now I've been putting off learning to code in Python. I am truly embarrassed by some of the sluggish scripts I've written using PHP to do tasks that a language like Python or even good'ol Perl could tackle in a fraction of the time.

Well the time has finally come and writing some simple Steemit bots seemed like a great way to learn the ropes. In a previous post I documented installing Piston on CentOS you can also go straight to the source and check out @xeroc's profile. He's the author of Piston and this is his latest post with instructions for other operating systems Changelog Piston 0.3.

Alright so Piston is installed and ready to be used as a library but what should my first bot do? Hmmm I know let's make a really simple minnowsunite bot that just scans incoming posts and comments for the word "minnowsunite" and upvote that post.

I created the following in a file called test.py inside the piston/tests folder just to try things out.

from piston.steem import Steem
import os
import json
steem = Steem(wif="YOUR_POSTING_PRIVATE_KEY")
for c in steem.stream_comments():
    #print('{}'.format(c.__dict__))
    if "minnowsunite" in c["body"].lower():
        print(c.upvote(100,"YOUR_POSTING_ACCOUNT_NAME"))

So the script is pretty basic but let's go over what it's doing.

from piston.steem import Steem
import os
import json

This is pretty straight forward we're just loading in the Steem class from those Piston libraries we installed in the last post. Wait a second there are no semicolons ending each line. Okay that feels a little weird not ending my line without a semicolon but okay let's keep going.

steem = Steem(wif="YOUR_POSTING_PRIVATE_KEY")
We are just creating a new Steem object using the posting private key of an account. This will get things started and connects us to the Steem network.

for c in steem.stream_comments():
We've just kicked off an indefinite for loop reading in a stream of posts and comments. I notice again a lack of punctuation that I would traditionally associate with a for loop declaration. It looks like Python uses a colon for encapsulation, seems simple enough. I'm also digging not having to put a $ in front of every variable name. I'm making sure to keep to strict indentation of 4 spaces. This is because I seem to remember Python being a language where white spacing matters and given that there doesn't appear to be anything terminating the colon encapsulation I'm assuming indentation is taking care of nesting issues.

#print('{}'.format(c.__dict__))
This line is commented out but I included it because it was so useful for learning. If you uncomment this line it will print out everything in the stream in JSON format. It will let you see all the various element names and values available to you via the Post object (which is what c is).

if "minnowsunite" in c["body"].lower():
We are now checking the contents of every post and comment's body to see if we can find the string "minnowsunite" and making sure to convert the body text to lower case to nullify any case sensitivity issues.

print(c.upvote(100,"YOUR_POSTING_ACCOUNT_NAME"))
Ok so if we've found a post or comment that contains "minnowsunite" let's upvote it! We are going to wrap the call to c.upvote inside a print so we can see the results while the bot is running. The number 100 represents how much voting power you wish to use for the vote.

That's it we now have a functioning upvote bot and learned a little about the differences in coming from PHP to Python.

Shout out to @someguy123 for his help

H2
H3
H4
3 columns
2 columns
1 column
12 Comments