Programming for Steem in Python : A grep for the Steem blockchain

in steem-dev •  6 years ago 

I found myself subscribing to blogs of people who write code and I remember that someone mentioned he was a programmer. I wanted to scan through all of the replies I got. The bad news is, there are too many replies to scan through.

greating_a_grep_for_steem.png

I took some software that uses account history and copyied it to steemgrep.py and started getting rid of stuff. I was left with a start like this:

#!/usr/local/bin/python3.6
import sys, re
if not ((3, 6) <= sys.version_info < (4, 0)):
    print("Must use python version 3")
    sys.exit(1)
import json
import datetime, time
import decimal
import io, signal
from optparse import OptionParser
import os, pwd
import secrets
from steem import blockchain
from steem import Steem
from time import sleep

I modified this in order to get a parameter -x. The x parameter is to display what transaction the body is in.

parser = OptionParser()
parser.add_option("-x", "--transaction", action="store_true", dest="tx", default=False)
(options, args) = parser.parse_args()


Now, I had to figure out the syntax. First, I start with hard coding the parameters:

username = 'steemfiles'

Then as test, I develop and add more parameters.

account_name = args[0]
op_type      = args[1]

if op_type not in ['reply', 'comment', 'post', 'all']:
    print("Specify 'reply', 'comment', 'post', or 'all' as second argument")
    sys.exit(0)

pattern = re.compile(args[2], re.I)

That's fine. Now there is a pattern, the user and what we are looking for. Now it is time to write an iterator loop to start from the last to the oldest:

steem = Steem()
try:
    tx_data = steem.get_account_history(account_name, index_from=10000000000, limit=step)
    minimum_history_number = None
    while minimum_history_number != 0:
        for history_number, tx in tx_data:
            if minimum_history_number is None:
                minimum_history_number = history_number
            if minimum_history_number > history_number:
                minimum_history_number = history_number 
            tx_id = tx['trx_id']
            for opi in range(0,len(tx['op']),2):
                op = tx['op'][opi+1]
                if tx['op'][opi] == 'comment':
                    if op_type == 'reply' and op['author'] == account_name:
                        continue
                    elif op_type == 'comment' and op['author'] != account_name:
                        continue
                    elif op_type == 'post' and op['parent_permlink'] is not None:
                        continue
                    body = op['body']
                    body_match = re.search(pattern, body)
                    if body_match is not None:
                        if options.tx:
                            print(history_number, tx_id, 'author:', op['author'], body)
                        else:
                            print(history_number, body)
        sleep(10)
        tx_data = steem.get_account_history(account_name, index_from=minimum_history_number-1, limit=step)
    print(minimum_history_number)
    

except KeyboardInterrupt as ke:
    pass

It is handy to have a KeyboardInterrupt exception to catch, so you can do a cntrl+c and get out. This loop iterates through the history. enjoy!

Dash XjzjT4mr4f7T3E8G9jQQzozTgA2J1ehMkV
LTC LLXj1ZPQPaBA1LFtoU1Gkvu5ZrxYzeLGKt
BitcoinCash 1KVqnW7wZwn2cWbrXmSxsrzqYVC5Wj836u
Bitcoin 1Q1WX5gVPKxJKoQXF6pNNZmstWLR87ityw (too expensive to use for tips)

See also

Get some Swift with an ad free faucet: https://www.swiftdemand.com/?referred_by=leprechaun


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!