steem-python for dummies #3 - Coding an upvote bot

Hello,

This is the third post of "steem-python for dummies" series. If you didn't read the first and second post, take your time and have a look.

  1. steem-python for dummies #1 - Introduction and Basic Operations
  2. steem-python for dummies #2 - Playing with account data

In this post, we will study the get_account_history method of and create a simple random upvote bot by using it.

Requirements of the bot

Bot,

  • should listen transfers directed to it.
  • should check the memo is valid
  • upvote the memo URL with a random vote (between 1 and 100)

Listening Operations

One can listen transactions by listening new blocks in the each newly produced blocks. However, if you want to see just specific operations for specific accounts, then you have a shortcut.

Usage:

from steem.account import Account

acc = Account('emrebeyler')
print(list(acc.get_account_history(-1, 1)))

See the raw output. It includes last two operations relating to my account in the chain.

get_account_history has a bunch of parameters for slicing the data, filtering the operations. It's well documented in the source code, copy/pasting from there.

ParameterTypeHelp
indexintstart index for get_account_history
limitint(Optional) skip items until this index
startint(Optional) skip items until this index
stopint(Optional) stop iteration early at this index
order(1, -1)1 for chronological, -1 for reverse order
filter_by(str, list)filter out all but these operations
raw_output(bool)return history in raw format

So, for my case, What I am interested is "transfer" operations.

account_history = acc.get_account_history(-1, 100, filter_by=["transfer"])

This will return a list of transfer operations related to my account.

Tip: This will not return latest 100 transfers. Because filtering is done on the steem-python (client) side, so it will get last 100 operations and filter them by transfers.

Two steps of success

First step:

Creating an Account instance with bot's posting key.

BOT_ACCOUNT = "BOT_ACCOUNT_USERNAME"
s = Steem(nodes=["https://rpc.buildteam.io"], keys=["PRIVATE_POSTING_KEYS"])
acc = Account(BOT_ACCOUNT, steemd_instance=s)

Second step:

In an endless loop, check for all transfers and upvote the correct posts with a random vote weight.

while True:
    transfers = acc.get_account_history(-1, 250, filter_by=["transfer"])
    for transfer in transfers:
        if transfer["to"] != BOT_ACCOUNT:
            continue

        try:
            post = Post(transfer.get("memo"))
        except ValueError as e:
            if 'Invalid identifier' == e.args[0]:
                print("Invalid post link. Consider a refund. [%s]" %
                      transfer.get("memo"))
                continue

        # get a voting weight between 1 and 100
        vote_weight = float(random.randint(+1, +100))
        post.commit.vote(post.identifier, vote_weight, account=BOT_ACCOUNT)

        print("Sleeping for 3 seconds")
        time.sleep(3)

Voila! Bot constantly check for transfers sent to it, and if it sees a valid post, gives a random upvote. Also, reviews the memo is a valid post or not by using steem-python's built-in capabilities.

However, this bot is not production ready. If you want to continue developing it, here are my advises:

Missing Features

  1. Refunds.
  2. Check for archived posts. (+7 days)
  3. Check for duplicate transactions (Bot shouldn't vote same post again and again.)
  4. Handle node timeout errors with a try/except blocks.

That's all for this post. Feel free to ask questions or request topics about steem-python for the incoming posts.



Posted on Utopian.io - Rewarding Open Source Contributors

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