Second Step In Learning Steem-Python (Find Out Your Followers Voting Power)

in #utopian-io6 years ago (edited)

Python Logo

Source

Much more progress this time around

Now that I do not have to deal with setting up my environment anymore, the real learning part can begin. One thing that I have always been curious of, who follows me that has a ton of voting power? I know you have thought it too, don't lie.

What fun is it to go through manually and see how much SP someone has? It isn't. Lets use some of my new found skills in tackling this mystery!

Now, obviously, if you want to use this code, you need all of the stuff for steem-python installed already. And replace my username with yours if you do that.

What do we have to start with?

Old Code

nodes=['https://api.steemit.com']
from steem import Steem
s = Steem(nodes)
print(s.get_followers('iamredbar', '', 'blog', 50))

What did this do? It just output a list of 50 of my followers. Not too useful. I want to be able to see who are heavy hitters in the upvote department.

Edits

nodes=['https://api.steemit.com']
from steem import Steem
s = Steem(nodes)
followers = s.get_followers('iamredbar', '', 'blog', 300)
print(followers)

I hard coded 300 result limit because I have less than 300 followers 😅
Change this if you have more followers than that.

Biggest change of these two similar pieces of code is that instead of just printing after I get what I want, I put it into a variable. This way, we can use it for more things.

Let's use all of that! But how...

from steem import Steem
s = Steem()
unsortedFollowerVests = {}
followers = s.get_followers('iamredbar', '', 'blog', 300)

for follower in followers:
    # grabs all your followers and makes it sort nice
    f = follower['follower']
    floatVests = s.get_account(f)["vesting_shares"]
    unsortedFollowerVests[f] = float(floatVests[0 : len(floatVests) - 6])

Let's create a dictionary and put some stuff in it, like their username and vesting shares. But the way that I want to sort this is going to prove problematic, so lets make that string into a float and call it a day. That is why there is some weird slicing going on. I am hoping for suggestions on that one for sure.

Time to sort now

There is an addition below the for loop. This is where we are going to sort out the list of users and their vests. Should look like this:

from steem import Steem
s = Steem()
unsortedFollowerVests = {}
followers = s.get_followers('iamredbar', '', 'blog', 300)

for follower in followers:
    # grabs all your followers and makes it sort nice
    f = follower['follower']
    floatVests = s.get_account(f)["vesting_shares"]
    unsortedFollowerVests[f] = float(floatVests[0 : len(floatVests) - 6])

# sorts the unsorted followers by vests
sortedFollowerVests = sorted(unsortedFollowerVests, key=unsortedFollowerVests.get)

There is an issue with this, however. sortedFollowerVests is just a list of strings. But it isn't just a list of strings, it is more helpful! It is the usernames that are sorted in order of vests!

Nothing like a good for loop to drive us home.

Lets wrap this thing up

Here is the finished code:

from steem import Steem
s = Steem()
unsortedFollowerVests = {}
followers = s.get_followers('iamredbar', '', 'blog', 300)

for follower in followers:
    # grabs all your followers and makes it sort nice
    f = follower['follower']
    floatVests = s.get_account(f)['vesting_shares']
    unsortedFollowerVests[f] = float(floatVests[0 : len(floatVests) - 6])

# sorts the unsorted followers by vests
sortedFollowerVests = sorted(unsortedFollowerVests, key=unsortedFollowerVests.get)

for follower in sortedFollowerVests:
    # This loop is for printing out after the dictionary has been sorted
    tab = "\t \t \t"
    if len(follower) > 15:
        tab = "\t"
    elif len(follower) > 7:
        tab = "\t \t"

    print(follower + tab + s.get_account(follower)['vesting_shares'])

You will notice the last loop to print has some code that is just fluff, but I like fluff. When this prints out it looks really nice and it's soothing to watch print in the terminal.


I feel like for being my first little project with Python I think it went pretty well. This list lists them from smallest to biggest. I like it that way because it shows the biggest ones right by where I don't have to search for them! You can reverse the order (if you want) by adding reverse = True here:

sortedFollowerVests = sorted(unsortedFollowerVests, key=unsortedFollowerVests.get, reverse = True)

My next plans are:

  • Extend the search for heavy hitters within one connection of me
  • Find out how often those big voters vote

PLEASE

If you see something that I am doing that I can improve upon, please let me know (more constructive the better). I want to learn as much as I can and I know that there are a lot of great developers on here. Thank you!


Series (sort of)

Earlier parts of me stumbling along in learning Python:



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it does not follow the Utopian Rules.

The contribution cannot be approved because it doesn't follow the rule:

  • Blogs must be strongly related to the promotion and development of an open-source project.

Suggestions:

  • Blog posts in Utopian are expected to be directly about either development phases or updates or promotion of the project. Therefore, a blog post format where you guide through your code using the basics of a library and your recent improvements on your code doesn't exactly fit "promotion" part. It's also not a part of development of the project itself, so your contribution cannot be approved.
  • I can suggest that you can tweak your format to fit any of the categories blog posts, tutorials or development with improvements like this:
    • If you want to go on a blog posts series, make sure that you overview the context with a more editorial manner and give details about ideas instead of code examples. Blog format about programming is more proper with less code and more details and design ideas.
    • You can teach technicality of your work with an instructive manner, which will make your posts fit into tutorials category. If you plan to, please consider providing enough details to teach and give examples.
    • You can also prepare development contributions, if your work is enough to be a project itself, or you can provide non-trivial and/or major improvements to the project.

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

Thank you for responding, this does clarify the rules for me. I appreciate the hard work you moderators do!

Coin Marketplace

STEEM 0.19
TRX 0.14
JST 0.030
BTC 59715.05
ETH 3186.24
USDT 1.00
SBD 2.42