Making A Steemit Bot Part 2: Exceptions
This is a part of a series and will work to build upon the last part. Part 1 is here. This post expects a very basic understanding of python syntax. This is going to introduce concepts of the steem-python library slowly at first to allow people to understand how to program their own stuff.
Problem With Our Bot
so the bot has a major flaw in it at this moment that we need to take care of before we continue to add features to automatically produce the required link for upvoting. Now as of right now, our bot looks like this:from steem import Steem
from steem.post import Post
def loop():
account = input("Enter Account: ")
posting_key = input("Enter Key: ")
s = Steem(keys=posting_key)
while 1:
link = input("Enter Link: ")
link = link.replace(link[0:int(link.find('@'))],'')
vw = float(input("Enter Voting Weight: "))
pst = Post(link, s)
pst.vote(vw, account)
print('Voted!')
loop()
But it has a major flaw. What happens if we were to try to upvote the same post twice at the exact same weight? Well you will get the error printed out that I will have below.
Error
kryzsec@kryzsec-a101:~$ python manual_voter.py
Enter Link: https://steemit.com/programming/@themarkymark/how-to-install-steem-python
Enter Voting Weight: 25
Traceback (most recent call last):
File "manual_voter.py", line 17, in <module>
loop()
File "manual_voter.py", line 13, in loop
pst.upvote(vw, account)
File "/home/kryzsec/miniconda3/lib/python3.6/site-packages/steem/post.py", line 228, in upvote
return self.vote(weight, voter=voter)
File "/home/kryzsec/miniconda3/lib/python3.6/site-packages/steem/post.py", line 248, in vote
return self.commit.vote(self.identifier, weight, account=voter)
File "/home/kryzsec/miniconda3/lib/python3.6/site-packages/steem/commit.py", line 360, in vote
return self.finalizeOp(op, account, "posting")
File "/home/kryzsec/miniconda3/lib/python3.6/site-packages/steem/commit.py", line 128, in finalizeOp
return tx.broadcast()
File "/home/kryzsec/miniconda3/lib/python3.6/site-packages/steem/transactionbuilder.py", line 131, in broadcast
raise e
File "/home/kryzsec/miniconda3/lib/python3.6/site-packages/steem/transactionbuilder.py", line 129, in broadcast
self.steemd.broadcast_transaction(self.json())
File "/home/kryzsec/miniconda3/lib/python3.6/site-packages/steem/steemd.py", line 858, in broadcast_transaction
return self.exec('broadcast_transaction', signed_transaction, api='network_broadcast_api')
File "/home/kryzsec/miniconda3/lib/python3.6/site-packages/steembase/http_client.py", line 192, in exec
return_with_args=return_with_args)
File "/home/kryzsec/miniconda3/lib/python3.6/site-packages/steembase/http_client.py", line 213, in _return
raise RPCError(error_message)
steembase.exceptions.RPCError: 10 assert_exception: Assert Exception
itr->vote_percent != o.weight: You have already voted in a similar way.
I want you to look into that last line of this error print out really close. You will notice the line itr->vote_percent != o.weight: You have already voted in a similar way.
What this means is that the blockchain throws an error when you try to vote the exact same way twice. Now there are a few ways to prevent this error from being thrown but due to something called out-of-order execution which is very complex (and kind of a big deal in the security realm right now) any safe guards you put up have the potential to through this error even if it was never called in the first place. So to prevent this grief from crashing out program, we have a very eloquent solution. We surround it in a try and catch and usually just throw out the error. Here is what I mean:
from steem import Steem
from steem.post import Post
def loop():
account = input("Enter Account: ")
posting_key = input("Enter Key: ")
s = Steem(keys=posting_key)
while 1:
link = input("Enter Link: ")
link = link.replace(link[0:int(link.find('@'))],'')
vw = float(input("Enter Voting Weight: "))
pst = Post(link, s)
try:
pst.vote(vw, account)
print('Voted!')
except:
i=5
loop()
So the try and catch (or in python, try and except) tries to run a bit of code but if an exception is thrown it will catch it. Now you can do many things in the exception side of things with the exception, but for simplicity I am just making a variable i and setting it equal to 5. Why? Because I do not care about the exceptions for the time being. Though it may be more correct to do something like this:
try:
#do something
except Exception,e:
print("Error: " + str(e))
This will tell you if something is going wrong with your code and what is going on, you can do a lot of things with the exception but I am not going to care too much right now. Once you all have more practice with python and messing around with this bot, maybe you can go and try a bunch of cases to find a bunch of different exceptions and make it do specific things on specific failures.
This Post Is Not Just About Try and Catch (Except)
I understand that may be confusing for some since the entire first part was all about that. Instead this post is made specifically for you to help ease people into the idea of errors. They happen a lot when programming and you will need to learn how to pick them out. Now there are other errors that this try-except thing will prevent from crashing your bot, like it will prevent your bot from simply crashing in case some fake link accidentally gets passed in, or some other number of errors.Steem-Python Documentation: steem.readthedocs.io/en/latest/index.html
Installing Steem Python: steemit.com/programming/@themarkymark/how-to-install-steem-python
Python String Documentation: docs.python.org/3/library/stdtypes.html
Making A Vote Bot: steemit.com/steem-python/@kryzsec/making-a-steemit-bot-part-1-basic-voting
Python Errors and Exceptions docs.python.org/3/tutorial/errors.html
Do you enjoy reading or writing topics related to STEM (Science, Technology, Engineering, and Mathematics) then I would suggest checking out @steemstem! They do wonderful work curating the best STEM related posts on Steemit. For more information check out the SteemStem chat room on steemit.chat or check out their Guidlines and start writing.
nice try
thank you