Thanks to @libertyteeth , who pointed out a few minor errors in one of my early steem-python tutorials, I decided to do an updated version on how to get started with the steem-python library and share some code examples for doing a few different things on the blockchain. I will however not be going through the process of setting up your Python environment but you're welcome to have a look at the following tutorial on how to set up your environment in Ubuntu.
- Setting up your environment in Linux(Ubuntu). Link
This is just one tutorial I found online but feel free to use any other tutorial you can find as long as you have at least Python 3.5 and pip
Awesome! Let's get started
First off we need to get the steem-python library so lets run the following command to get it:
$ pip install steem
I'll be using the Geany editor. If you want to use Geany you can run the following command to get it or you could use any other text editor of your choice.
$ sudo apt-get install geany
Next we'll create and open our python file where we'll be coding:
$ geany filename.py
From here on I'll be sharing two code examples I wrote on how to do different things on Steemit and the Steem blockchain including a bot that upvotes posts and comments mentioning us as well as n welcoming bot.
Upvoting comments and posts that mention us
from steem import Steem from steem.blockchain import Blockchain from steem.post import Post #crate steem instance and pass it our key s = Steem(keys = ["<your private posting key>"]) #create blockchain instance b = Blockchain() #stream the blockchain, map it to a Post object to make life easier and filter #them by comment since all comments and posts are of type: comment stream = map(Post, b.stream(filter_by=['comment'])) #go through all the posts in the block for post in stream: try: #get all the users tagged in the post users = post.json_metadata.get('users', ) #check if we are tagged if "<your-steemit-username-in-lowercase>" in users: #if we are tagged we'll upvote the post/comment with 10% voteweight post.vote(10.0, "<your steemit username>") else: #we are not tagged so we will skip this post pass except Exception as e: print("Error: "+str(e))
Welcoming new people to Steemit with a comment bot
from steem import Steem from steem.blockchain import Blockchain from steem.post import Post #create steem instance and pass it our key s = Steem(keys = ["<your private posting key>"]) #create blockchain instance b = Blockchain() while True: try: # stream the blockchain stream = map(Post, b.stream(filter_by=['comment'])) # go through all the posts in the block for post in stream: # get all the tags of the post postTags = post.json_metadata.get('tags', ) # check if "introduceyourself" is in the list of tags if "introduceyourself" in postTags: title = post.title # check if the post/comment has a title if title == "": #title is empty so it's most likely a comment pass else: #We have a title so it's a fresh posts so let's welcome them post.reply("Welcome to Steemit!", "", "<your steemit username>") else: #no introduceyourself tag so skip the post pass except Exception as e: print("Error: "+str(e))
To run any one of these scripts you can issue the following command:
$ python3 filename.py
I don't advice anyone to actually use these. These are merely just examples of how you could implement the steem-python library. Each of these have their own problems which I did not fully address. I also doubt that these examples follow the best coding practice. Here are some of the problems:
- Upvote Bot
If people realise that you're upvoting any comment or post with your name in it people might abuse it and spam your name for a free upvote.
- Comment Bot
A lot of people abuse the introduceyourself tag because it's one of the tags with the highest amount of payouts. There are also loads of people running welcoming bots which turns into spam and people don't take to kindly to that so you'll most likely get flagged if you do the same. Bots can also programmatically add titles to their comments so checking if the title is empty or not in order to determine if it's a post or a comment is not the best way to go about it.
- Other things to keep in mind
As far as I know Steemit only allows you to upvote every 3 seconds and comment every 20 seconds