How to create a simple autovoter using steem-python

A lot of people on steemit use an autovoter like SteemVoter, but I'm a programmer so I thought I'd create one for myself using steem-python and show others how to do the same! If you haven't already installed steem-python or are having trouble doing so you can check out my tutorial on how to do that here.

Streaming The Blockchain

First of all we need a way to check all new posts on the blockchain so we can see if we want to upvote any of them. Luckily this is very easy!

def run(user_json):
    blockchain = Blockchain()
    stream = map(Post, blockchain.stream(filter_by=["comment"]))

    while True:
        try:
            for post in stream:
                # Do something

        except Exception as error:
            # print(repr(error))
            continue

Make sure to use try and except to catch any errors, because people could've deleted their post by the time it reaches our autovoter!

Creating Your Upvote List

Since this is a simple autovoter we will be upvoting our desired author's posts as soon as they reach us in the blockchain. However, you might want to upvote certain people with a higher % than others, or limit the amount of upvotes you give someone. Whatever you choose to do, you still need to make a list of people you want to upvote in the first place! You can do this manually, or if you are lazy like me you can simply create a JSON file of everyone you follow and treat them equally!

def create_json():
    user_json = {}
    for user in Account(username).export()["following"]:
        user_json[user] = {
            "upvote_weight" : 100.0,
            "upvote_limit" : 2,
            "upvote_count" : 0
        }
    return user_json

Obviously if you don't want this you can create a similar list in the same format and tweak everything to your specific needs! An example of my JSON file can be found here.

Validating Posts

Once you've specified how and who you want to upvote, you need to make sure that your autovoter is upvoting the correct posts. To do this you need to check each post that is published on the blockchain to make sure they are valid.

A post is valid when:

  1. It is a post and not a comment
  2. The author of the post is in our JSON file
  3. The author's upvote limit hasn't been reached

To do this we can simply define a function where we check if a post is valid, as seen below:

def valid_post(post, user_json):
    title  = post["title"]
    author = post["author"]
    
    if (post.is_main_post()
        and author in user_json
        and not limit_reached(user_json, author)):
        user_json[author]["upvote_count"] += 1
        return True
    else:
        return False

Here the function limit_reached is a simple function where we check if the upvote limit has been reached:

def limit_reached(user_json, author):
    if user_json[author]['upvote_limit'] == user_json[author]['upvote_count']:
        return True
    else:
        return False

Upvoting Posts

Once a post has been validated it should be upvoted! To upvote something you need to be logged in, but entering your password every time is very tedious. A way to avoid this is by setting an environment variable UNLOCK as your password and using that. After this we simply need to add our new functions to our while loop, as you can see below:

def run(user_json):
    username = "amosbastian"
    wif = os.environ.get("UNLOCK")
    steem = Steem(wif=wif)
    blockchain = Blockchain()
    stream = map(Post, blockchain.stream(filter_by=["comment"]))

    print("Checking posts on the blockchain!")
    while True:
        try:
            for post in stream:
                if valid_post(post, user_json):
                    try:
                        title  = post["title"]
                        author = post["author"]
                        print("Upvoting post {} by {}!".format(title, author))
                        post.upvote(weight=user_json[author]["upvote_weight"],
                            voter=username)
                    except Exception as error:
                        print(repr(error))
                        continue

        except Exception as error:
            # print(repr(error))
            continue

The variable username should be your own username of course!

That's it!

We are done! All that's left now is to run our simple autovoter! I've recently tested it to make sure it works and this was the output

Checking posts on the blockchain!
Upvoting post The Bug Is Over! Thanks For Your Patience by utopian-io!

and checking on my feed it indeed showed that it had actually upvoted

The JSON files and the actual autovoter autovoter_simple.py can be found on my repository here!


Improvements

There are of course some improvements that could be made. Streaming the blockchain this way results in posts already being roughly a minute old by the time they reach you, so maybe you are better off upvoting posts if they are 30 minutes old for example. If you are up for the challenge, you could try adding this to the simple autovoter yourself! If you aren't, then don't worry, as I'll more than likely release a tutorial on how to do this in the future!



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
2 Comments