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.
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.
There are two ways I have in mind to get this functionality.
Flow 1
Flow 2
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.
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.