Second Step In Learning Steem-Python (Find Out Your Followers Voting Power)
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
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:
Suggestions:
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!