Getting Started With Steem-Python : Upvote and Comment Bot Examples (Linux)

in #steemdev7 years ago (edited)

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.

  1. 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.


268678.jpg

source

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


Please Note

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

For the full Steem-Python documentation you can visit this Link.

Please let me know if you spot an error so I can fix it as soon as possible. Feel free to ask questions in the comments. I hope you found this tutorial helpful.

Sort:  

Great post. Thanks!

I wanna give a try. Though I'm not a professional coder let's start it give it a try.

Never Gonna Give you up(Rick Roll'd).

I still don't understand something about this API although i haven't put it in action, I've been to the steemit documentation before seeing your post

from steem import Steem
from steem.post import Post
#Here we are importing the required libraries needed for this task

s = Steem(keys = ["your private posting key"])
#so here we created a steem instance with our private posting key, i also noticed it's a list why ?

post.vote(10.0, "your steemit username")
#we just skipped to the post function, does the post.vote(float, string) just link from the instance to use your account to post

Also what if i want to upvote a particular vote by supplying its link

I know there are lots of errors here :)

Nice post! Thanks for making this. It aided me in a way. :)

I am really new to coding. I was wondering if it was possible to deploy these upvote and comment bots through Heroku?

Excellent follow me @ joserc.vzla

Hey @benniebanana, I'm trying to install steem and get an error that python 3.5 or newer is required and this error: Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-gQWp0C/steem/

Running python -v reveals Python 3.6.1 :: Anaconda 4.4.0 (32-bit)

Any suggestions? Thanks

Hi sorry for the very late reply. Unfortunately I have never had that error. I'll have a look around and let you know if I find something useful unless you've already figured it out?

I've not yet. Thanks

Great post man, would love to try this when I get Linux.

I've been having a terrible time trying to get a bot going. I have a string of code and have changed it so many times to try to fix the errors that come up I don't even know what I'm doing anymore. I never knew what I was doing to begin with :)

Anyway, I was trying to create an upvote bot that anyone could use.
Now, I'd like to create a bot that will comment for me on intro posts. Any chance you could help me with this?

Hi sorry for the super late reply, I've been pretty busy lately with my new job so I haven't really had time to update or edit these examples and there's a pretty high chance that these won't work anymore.

Let me know if you managed to find what you were looking for. I'll be happy to make an updated version.

Thanks for still getting back to me. I haven't been able to get anything working. I have the code and I think it must be close. The only thing is; because there are so many other bots out there - it may not even be worth doing.

The code I have is for people to automatically upvote authors they like. Currently, they can do that on Steemfollower, Streemian, and other places.

I would like to create a bot that people can pay for upvotes though.
Also, it would be cool to have a comment bot.

Let me know what you think of all of this. If you have discord, you can contact me there as well.

Yes the purpose of my code samples are for people to learn and to try and apply whatever they've learned to projects they might be busy with. You'd be better of using streemian or any of the other trusted voting and commenting bots already available out there.

I'm unable to find any commenting bots that I can use.
Do you have any suggestions as to where I can get one?

Here is one I made a while ago which will stream new blockchain posts and checks if the posts contains the "introduceyourself" tag. If the post contains the tag the bot comments a welcoming message on the post. But this is a old piece of code I wrote so there's a chance that it might be outdated by now . Hope this can serve as a guideline for what you'd like to do.

https://github.com/steandwt/Steemit-Welcome-Bot/blob/master/welcomeBot.py

Thanks, I'll look into it.

Coin Marketplace

STEEM 0.20
TRX 0.25
JST 0.038
BTC 97340.58
ETH 3390.06
USDT 1.00
SBD 3.10