Part 18: How To Analyse An Account's Resteemers Using Steem-Python

in #utopian-io6 years ago (edited)

steem-python.png

This tutorial is part of a series where we explain different aspects of programming with steem-python. Links to the other tutorials can be found in the curriculum section below. In part 9 of this series we learned how to calculate the total rewards of a post and in this tutorial we will learn how to estimate the curation rewards of a post!


What will I learn

  • How to iterate over an account's blog posts
  • How to find out who resteemed a post
  • How to print your top 10 resteemers

Requirements

  • Python3.6
  • steem-python

Difficulty

  • Basic

Tutorial

If you find it easier to follow along this way you can get the entire code for this tutorial from here.

Getting an account's blog posts

In the previous tutorials we used the function get_history_reverse() to iterate over an account's history and filtered it by "comments" to get the posts they made. Today we are going to use a different, and better way to do this. Instead of using the get_history_reverse() function to get all operations by an account and then filtering it, we can simply use the get_blog() function, that takes an account, entry_id and limit as arguments. So if you wanted to iterate over the last 20 posts an account made, you could use

for post in steem.get_blog(account, 0, 20):
    post = Post(post["comment"])
    # DO SOMETHING

It's that easy! Of course you still need to check if the post is a main post or a comment, and also if it's not someone else's post (resteemed posts occur here as well). So, if we wanted to iterate over all posts that this account has made, we could use the following code:

from steem import Steem
from steem.post import Post

account = "steempytutorials"

for post in steem.get_blog(account, 0, 500):
    post = Post(post["comment"])
    # Make sure it's a main post and also not a resteemed post
    if post.is_main_post() and post["author"] == account:
        # DO SOMETHNIG

Finding out who resteemed a post

To find out who resteemed a post is really simple as well. To do this we can use the aptly named get_reblogged_by() function that takes an account's name and a permlink as arguments and returns a list of account names that resteemed the given post. So for each post in an account's history we simply need to extract the permlink and pass this to the function alongside the account's name. For some reason posting something counts as resteeming something, so we need to make sure to remove our own account's name from the list of people that resteemed the post as well. After doing this we can just extend our list of resteemers with this list! To do this we can use the following code

resteemers = []
for post in steem.get_blog(account, 0, 500):
    post = Post(post["comment"])
    if post.is_main_post() and post["author"] == account:
        permlink = post["permlink"]
        resteemed_by = steem.get_reblogged_by(account, permlink)
        if not resteemed_by == []:
            resteemed_by.remove(account)
        resteemers.extend(resteemed_by)
print(resteemers)

which gives a huge list for @steempytutorials, so I'll show the output for my account @amosbastian instead

['munaa', 'cryptonewstoday', 'gazuua', 'vot', 'xnpbgsw', 'resteemsupport', 'datascience', 'jaronmct', 'juliank']

What if the account has > 500 posts?

The function get_blog() can only get a maximum of 500 posts, so if an account has more than 500, then you need to do it in batches. To do this we can simply create a while loop that keeps iterating over posts while the posts analysed are less than the account's total posts. We can do this with the following code

post_limit = 0

while post_limit < Account(account)["post_count"]:
    for post in steem.get_blog(account, post_limit, 500):
        # DO STUFF
    post_limit += 500

To test this I found someone I follow with more than 500 posts, @jestemkioskiem, and used him as an example. This gives us the following output

['tiotioahmad', 'avenal', 'ceybiicien', 'fitzgibbon', 'j4nke', 'promo-steem', 'simon-paluch', 'dufriadi', 'wens', 'martwykotek', 'pf-coin', 'shello', 'tiotioahmad', 'avenal', 'ceybiicien', 'fitzgibbon', 'j4nke', 'promo-steem', 'simon-paluch', 'dufriadi', 'wens', 'martwykotek', 'pf-coin', 'shello']

Creating and printing the top 10

An easy way to do this is to use a Counter from the collections package. We can create a Counter of our resteemers list and use the following code to print our top 10

for resteemer in Counter(resteemers).most_common(10):
    print("{0:16} has resteemed {1:16} {2:>2} times!".format(
        resteemer[0], account, resteemer[1]))

which for @steempytutorials gives the following

amosbastian      has resteemed steempytutorials 30 times!
muksal.mina      has resteemed steempytutorials  8 times!
photo-trail      has resteemed steempytutorials  5 times!
juliank          has resteemed steempytutorials  3 times!
isnamahyazani    has resteemed steempytutorials  2 times!
pinay            has resteemed steempytutorials  2 times!
vot              has resteemed steempytutorials  2 times!
cryptoryno33     has resteemed steempytutorials  1 times!
davidamsterdam   has resteemed steempytutorials  1 times!
abu-banderas     has resteemed steempytutorials  1 times!

Note: if an account has a lot of posts then this can really take a while. I tested this with @juliank's account as well, at it took forever, but it does work as seen below

sun-flower       has resteemed juliank          602 times!
pixresteemer     has resteemed juliank          348 times!
jaladin717       has resteemed juliank          312 times!
devasish         has resteemed juliank          300 times!
nasional         has resteemed juliank          180 times!
deerjay          has resteemed juliank          180 times!
photo-trail      has resteemed juliank          169 times!
fesbukan         has resteemed juliank          144 times!
janellelanae     has resteemed juliank          144 times!
steemsyndicate   has resteemed juliank          132 times!

Congratulations, you've now learned how iterate over all blog posts by an account using steem-python, how to get a list of people that resteemed each post and print an account's top 10 resteemers!

Curriculum

Set up:
Filtering
Voting
Posting
Constructing
Rewards
Transfers
Account Analysis

The code for this tutorial can be found on GitHub!

This tutorial was written by @amosbastian in conjunction with @juliank.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

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

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

Achievements

  • 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

Coin Marketplace

STEEM 0.20
TRX 0.14
JST 0.030
BTC 68523.63
ETH 3260.51
USDT 1.00
SBD 2.66