steem-python for dummies #10 - Listening specific accounts


Hello,

This is the 10. post of "steem-python for dummies" series. If you didn't read the older ones 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
  3. steem-python for dummies #3 - Coding an upvote bot
  4. steem-python for dummies #4 - Private Memos
  5. steem-python for dummies #5 - Bundling Blockchain Operations
  6. steem-python for dummies #6 - Delegating
  7. steem-python for dummies #7 - Creating Accounts
  8. steem-python for dummies #8 - Calculating Post Rewards
  9. steem-python for dummies #9 - Creating orders in the market

In this post, we will learn how to listen specific accounts and get notified about their posts. This was originally @veleje's request on the last post.

Listening transactions or accounts


There are two ways I have in mind to get this functionality.

Flow 1

  1. Create a whitelist for the accounts we want to get notified
  2. Listen all transactions in the network
  3. Check the posts and their authors in that whitelist
  4. Notify!

Flow 2

  1. Create a list for the accounts we want to get notified
  2. Loop ths list forever and check get_account_history(username)
  3. If there is a new post, notify!

I will go with the second flow. Parsing all blocks, translations will create additional code. However, using get_account_history call will be so much simpler. One downside is that, if you watch hundreds of users, parsing blocks may be faster since we loop and check all users' account history here.

Here is the code snippet implementing the flow #2.

from steem import Steem
from steem.account import Account
from steem.post import Post
s = Steem()
accounts_to_watch = [
    'emrebeyler',
    'utopian-io'
]
def notify(post):
    print(post)
def check_for_posts(s, accounts):
    for account in accounts:
        acc = Account(account,  steemd_instance=s)
        posts = acc.get_account_history(
            -1, 250, filter_by=["comment"])
        for p in posts:
            post = Post("%s/%s" % (p["author"], p["permlink"]))
            if not post.is_main_post():
                continue
            notify(post)
check_for_posts(s, accounts_to_watch)

This is a simple implementation checks the last 250 account operations, and filter posts, and notify about them.


Running this gives me this output at the moment.

Important note: get_account_history gets last N operation of account and filter them. So, if the last 250 operation doesn't include a post, then you can't get anything. Setting that number to 1000, 2000 might be useful not to miss anything from the target accounts.

Potential Features


  • notify() function just prints the post at the moment. You can fill here with email notifications, sms notification or even push notifications into your phone. Up to you.

  • This script should run in a scheduled manner via crontab. Or write a scheduler in python (endless loop with time.sleep()) and use it that way.

  • Notified posts should be stored somewhere (text file, database, etc.) so, do not get same notifications again and again.


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