Create a Voting Bot

in #utopian-io6 years ago (edited)

Dear Steemians,

I would like to give you my take on a voting bot featured in the steem-python docs.

  • First you need Python (I assume you're familiar with it), then you need to install the 'steem-python' library, on Ubuntu it will be done by typing into the terminal the following command:
pip install steem
  • After the procces is done, we're ready to attack our problem of creating a simple voting bot. Create a new .py file and follow along. Obviously we need our imports:
from steem.blockchain import Blockchain
from steem.post import Post
from steem import Steem
  • After that's done, we define our 'run' function, in which we insert our 'private posting key' and 'private active ke>' in the correct field in the code (where you can see your keys? google it :D) also 'whoami' variable stores our username on steemit.
def run():

    s = Steem(keys=['private posting key','private active key'])

    whoami = 'my-steem-username'
  • Next we need our blockchain object, and stream (in which we filter only 'comments' - that is posts and comments, but they are under one name in steem-python)
b = Blockchain()
stream = map(Post, b.stream(filter_by=['comment']))
  • You need to be aware that 'stream' is generator object, so we iterate over it, we collect all mentions of users in the post/comment, and we upvote it if it mentions us.
    for post in stream:
      
        if post.json_metadata:

            mentions = post.json_metadata.get('users', [])

            if whoami in mentions:
                  post.upvote( voter=whoami)

The complete code:

from steem.blockchain import Blockchain
from steem.post import Post
from steem import Steem

def run():

    s = Steem(keys=['<private posting key>','<private active key>'])

    whoami = 'my-steem-username'

    b = Blockchain()
    stream = map(Post, b.stream(filter_by=['comment']))

    for post in stream:
      
        if post.json_metadata:

            mentions = post.json_metadata.get('users', [])

            if whoami in mentions:
                  post.upvote( voter=whoami)

if __name__ == '__main__':
    run()
  • Any suggestions? Post them in the comments.
  • Want your own specialized bot, contact me on steem.chat (username is blockdev0), let's collaborate:

Thank you for reading. Never stop learning.

Coin Marketplace

STEEM 0.16
TRX 0.15
JST 0.028
BTC 57975.44
ETH 2289.64
USDT 1.00
SBD 2.46