Making A Steemit Bot: Introducing the Blockchain

in steem-python •  7 years ago 

[Part 1] [Part 2] [Part 3]


So Far

So far everything we have done is taking an operation from some source, turning it into a string and then using string manipulations to extract data from it. Why? Because it is intuitive for us to do it that way for anyone used to C or C++ but we do not need to. The steem-python library turns all of these JSON operations into dictionaries which means we do not need to do all of this cheap hacky crap. Now at the same time I am also going to introduce something new, the blockchain module, and show you how we would have done it before and how we can do it with the new method. Ultimately we can use either method (but why would you want to?) and the reason why I didn't show this easier method first is because, well, I guess I just like being mean.
The program is going to work by looking at the blockchain (as blocks get minted) and look at all of the transfers (i.e. when someone sends someone else some Steem or SBD)

from steem.blockchain import Blockchain

def gm():
    b = Blockchain()
    transfer = b.stream(filter_by=['transfer'])
    for actions in transfer:
        action = str(actions)
        FROM = action[action.find("'from': '")+len("'from': '"):action.find("', 'to': '")]
        TO = action[action.find("'to': '")+len("'to': '"):action.find("', 'amount': '")]
        START = action.find("'memo': '")+len("'memo': '")
        END = action.find("'}]}]")
        MEMO = action[START:END]
        print("From: " + FROM + "\nTo: " + TO)
        print("memo: " + MEMO)
        print("\n##############################################################################")

gm()

But I am getting sick of using these text manipulations that are getting very very hacky.... so instead I am going to introduce you to a new way to make this exact program and have it function the exact same way...

from steem.blockchain import Blockchain

def gm():
    b = Blockchain()
    transfer = b.stream(filter_by=['transfer'])
    for action in transfer:
        FROM = action['from']
        TO = action['to']
        MEMO = action['memo']
        print("From: " + FROM + "\nTo: " + TO)
        print("Memo: " + MEMO)
        print("\n##############################################################################")

gm()

The outputs of both of these programs will be the exact same and will look like this:

kryzsec@kryzsec-a101:~/ssb$ python memo.py 
From: msp-reg
To: spalatino
Memo: Successful registration. Welcome to MSP & PALnet.

##############################################################################
From: omersurer
To: steemdice1
Memo: {"ui":"steemdice.net","type":"lower","number":60}

##############################################################################
From: mattmccauley
To: minnowbooster
Memo: https://steemit.com/bescouted/@mattmccauley/german-shorthaired-pointer-at-full-gallop

##############################################################################

Now you may ask is there any reason why I would show you this way now and not before... Yes. So in the hackiest way to make a curation bot you may have noticed we used a method s.get_account_history('ned', index_from=-1, limit=0), well, that method returns a list and not a dictionary. Now we could have converted it to a dictionary but what you are going to soon realize is that that method is way less reliable over the next method I am going to show you for creating a curation bot. Just to give you some information before hand I will say this, the method I am talking about involves using a method similar to Blockchain().stream(filter_by: typing.Union[str, list] = [], *args, **kwargs) except that there is a problem with stream() which is it requires a 100% uptime, and we maybe don't have/want to dedicate a computer to being up 24/7 or we do not have to worry about turning it off for updates and we will not be affected as much in case of a program crashing. So instead we will use Blockchain().stream_from(start_block=None, end_block=None, batch_operations=False, full_blocks=False, **kwargs) as it allows us to choose a block to start streaming at and stream from there onward, meaning if it stops and we have it constantly write to a file on what block it has gone through we can go over the last block looked at and start from there. But rather than just give you the code, I have to say that I currently feel like this is enough for this post, so until next time!


References
Python dictionaries www.tutorialspoint.com/python3/python_dictionary.htm
Printing An Error In Python: stackoverflow.com/questions/1483429/how-to-print-an-error-in-python
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
JSON: www.json.org/
Making A Vote Bot: steemit.com/steem-python/@kryzsec/making-a-steemit-bot-part-1-basic-voting

Kryzsec's Steemit Board


Image Source from @nitesh9

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.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Hello @kryzsec, since I don't have a direct means to chat you up and express my gratitude for your support on the @qurator's curation trail, I just feel I should drop my comment here
I'm deeply grateful , keep on with the good works

No worries! I am sorry for the later reply, just got on right now :-)

Excellent @kryzsec. Regards!.

Hey, this is a very cool series with lots of good info on getting started coding with Steem!

I just made a guide for getting steem-python setup in the cloud with a free Codenvy account. For anyone having trouble getting things working on their own machine, this can be a great alternative.