steem-python for dummies #10 - Listening specific accounts

in #utopian-io7 years ago (edited)


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

Sort:  

I have tried to put this in a perpetual loop, but unfortunately the data does not seem to update. Do you have any idea what my mistake could be?

can you share what have you tried so far?

It works now. My mistake was apparently that I did not generate a new version of the variable which take the get_account_history. After doing this in the loop, it works now.

Great, thanks for the tutorial!

Also, very helpful ideas for the expansion. I will, as learning python project, add some kind of storing data as you suggested. And, at some time in the future, email notifications. Raspberry Pi may be a great, low electricity idea for running this non stop, or using chrone in Linux as you mentioned.

It is amazing, how little code is actually needed to be able to do such a useful things with Python. I really need to learn this thing :-)

emeğine sağlık. büyük bir ilgi ile takip ediyorum sizi.

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Will you also be covering logic like in Flow 1? It would be cool to "listen in" on the entire block chain.

Also, if I could humbly request that you use relative links in your article, that would be very helpful. I generally use the night mode and clicking utopian links when I don't actually need to go to utopian gives me a literal headache. For example, your first example link could have been to /utopian-io/@emrebeyler/steem-python-for-dummies-1 and then it would let users stay on their condensor of choice (steemit, busy, etc) instead of being carried off to the utopian.io. If not, it's no big deal.

Thanks for another useful tutorial!

ahh, I will do the relative links next time. :) thanks for the idea. Regarding flow1, I will send you some links. It's already covered well.

Okay, thanks, I very much appreciate it.

Hey @emrebeyler I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Look at that birthday pos. !BEER



Hey @emrebeyler, here is a little bit of BEER from @isnochys for you. Enjoy it!