Steemit-Python-Bot | Part 3: Relike someones post if they liked yours

Dress to Kill.png

What Will I Learn?

  • How to upvote a post
  • How to build a basic bot

Requirements

  • Python 3.6
  • steem-python

(I personally use Windows and it was quite challenging to install the library but this tutorial helped me a lot.

Difficulty

  • Beginner

Tutorial Contents

1. Setup

We first need to import some necessary librarys and set up a variable (we need it later).

image.png

Obviously, we need to specify a node to connect to.
Then you need to change account to your own account name and add your private posting key and your private active key in order to be able to upvote later.

With this information set, we can finally initialize the steem class.

image.png

2. relike()

(The first two functions we use are explained in detail in my last two tutorials)

image.png

In the beginning, we need to know what our newest post is and therefore we input our username in the get_newest_post function, which returns the "post identifier" of our post ( Author + Permalink).

Now we can build on top of that and get a list of the users who upvoted our post with the get_voter function.

Finally we just pass this list into our new upvote_newest_post function and it will upvote their posts.

image.png

3. upvote_newest_post()

Because the upvote function of the python-steem library needs your account-name we need to pass it into the function. Secondly, we need the "post identifier" to know what post to upvote.

This function will basically just go over every entry in the list of people who upvoted our post and finds their newest posts and then upvotes them.

To do this we start with a for loop : for i in usernames and then get_newest_post(i) ( get the newest post for every entry in the list).

In the end, we just try to upvote the post and if this fails ( we already upvoted this post ) we will except this error and just continue running the script.

image.png

As you can see I set weight to 100: this means that you will upvote the post with 100% voting power. You can of course change this number if you wish.

( If your weight is -100 you will downvote the post -- please don't do that :) )

3. main loop

To let the bot run infinitely we make a while(1) loop. ( 1 == always true )

Because I personally had problems with the bot stopping I added one line which debugs the time to let you know if it's still running.
Now just execute the relike function with your account name as the one and only input.

Because it is unnecessary to execute this code every second [ it will put load on the API --> slows other people down :( ] I let it execute every 5 minutes.

image.png

Complete Code on Github

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now