How to find the number of incoming votes from and outgoing votes to different users for any given account

in #utopian-io7 years ago (edited)

This is my first real coding tutorial contribution here. Hope it really helps people in their journey to learn about and contribute to this wonderful platform.

steem-python.png

Image Credit: steem.io & python.org

divider-red

What Will I Learn?

  • Find the total number of incoming and outgoing votes for any account
  • For the same account, find the number of incoming votes from and outgoing votes to different users

Requirements

  • Python 3.6
  • steem-python

I use Windows and it is difficult to easily install steem-python on a Windows machine. This tutorial written by me should help out on that.

Difficulty

  • Basic

divider-red

Tutorial Contents

Step 0 - Basic Setup

In order to get this program running you first need to perform some basic steem-python set up activities in your machine including setting up nodes which are essential for accessing the blockchain.

All necessary details can be found in the below tutorial:

Part 0: How to install steem-python in Windows

divider-red

Step 1 - Import Libraries

The first step for any python program is of course to import the necessary libraries. You will see how these libraries are used in the following steps.

votecount1.png

Image Credit: @svkrulze for this and all following images

divider-red

Step 2 - Function definition

This is the function which does all the work in the program. It accepts the Steemit accountname for which the analysis is to be done and the start and end dates for the period for which the report needs to be created.

The function returns a dict of users and incoming and outgoing votes to and from those users vis-a-vis the Steemit account for which the report is being generated.

votecount2.png

divider-red

Step 3 - Date formatting

The dates received as YYYY-MM-DD need to be formatted. Please note how the end date is modified to include the entire day's time. If those 23 hours, 59 minutes, 59 seconds are not added then the program will not be able to fetch data for the end date as it will consider only till 12 AM of the end date which is really the beginning of the day.

votecount3.png

divider-red

Step 4 - Initial message and variable declaration

Two dictionaries are declared here along with two integer variables, these will be used shortly.

posts is a dictionary to store number of votes and voters list

authors is a dictionary to store usernames and count of incoming and outgoing votes

votecount4.png

divider-red

Step 5 - Main loop

Account.history_reverse() is used with filter_by="vote" to fetch only votes and not all the other data which is unnecessary for our purpose. The history_reverse() function is used instead of the history() function to make sure the latest vote by a user (after vote changes, vote deletions etc.) is picked up.

The for loop goes through all the votes received and given by the account in reverse order.

votecount5.png

Votes within the provided timeframe are only considered [if time_voted >= start and time_voted <= end].

A new post is added to posts by using permlink each time a new one is encountered.

Also note how votes with non-zero weights [if vote['weight'] == 0] are considered only by setting the vote value to 0 when there is a zero-weight vote. Negative votes are counted and not ignored or subtracted from the total vote count.

When a post is already present in posts and a voter who is already in the list of voters for that post is encountered again i.e. an earlier vote which was later revised, such an old vote is not considered [if vote['voter'] not in posts[permlink]['voters']].

When vote is on an already encountered post but from a voter who has not been encountered for that post, the vote count for the post is increased and the voter list updated.

divider-red

Step 6 - Votes to and from different users - still within Step 5 for loop

When a vote is valid as per Step 5, a check is performed whether the voter is the account in consideration for the report or not and accordingly the authors dictionary is updated with the vote either as incoming or outgoing.

votecount6.png

divider-red

Step 7 - Break out of Step 5 for loop

Break out of the loop when voting time is before the start time for the report.

votecount7.png

divider-red

Step 7 - Print results and return user list

Finally, once the for loop goes through all votes for the account within that timeframe, the results are printed using the authors dictionary and the same is returned by the function.

votecount8.png

divider-red

Step 8 - One call to run it all

The easiest thing to do remains: call the function passing the accountname and start and end dates.

votecount9.png

divider-red

Results

The results are displayed when the program is run. The below printscreen is a partial one showing the first few lines of the output.

votecount-results.png

Please note

Consider the date range you provide, as a long date range or a date range from quite some time back will take longer to run as the code has to traverse the blockchain sequentially from the newest to the oldest.

Hope this helped you in some way! Would love to know how it helped you in the comments section below.

divider-red

Curriculum

Part 0: How to install steem-python in Windows

Source Code

Full source code for this tutorial can be found on Github.

divider-red

Goodies: Benefits for You (Referral Links)

Sell Votes on SmartSteem

Get Followers and Upvotes on SteemFollower

Get Free Crypto Coins

free-crypto2.jpg

Image Credit

divider-red

upvote-share-resteem

steem-engine.png

crypto-empire

Click on the banners to learn more about each community

leave-comment.png

Sort:  

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

Violated Rule(s):

  • Submissions focused on the use of functions that are already well documented in the project documentation will be rejected.

My Opinion(s):

  • Documentation clearly states how to use these functions.

You can contact us on Discord.

[utopian-moderator]

Hey @yokunjon, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

Nice post bro. Thanks for sharing✌
Have a great time ahead @svkrulze.

You have been scouted by @promo-mentors. We are always on the look out for promising authors.

I would like to invite you to our discord group https://discord.gg/vDPAFqb.

When you are there send me a message if you get lost! (My Discord name is the same as the one here)

Congratulations! Your post has been selected as a daily Steemit truffle! It is listed on rank 5 of all contributions awarded today. You can find the TOP DAILY TRUFFLE PICKS HERE.

I upvoted your contribution because to my mind your post is at least 22 SBD worth and should receive 70 votes. It's now up to the lovely Steemit community to make this come true.

I am TrufflePig, an Artificial Intelligence Bot that helps minnows and content curators using Machine Learning. If you are curious how I select content, you can find an explanation here!

Have a nice day and sincerely yours,
trufflepig
TrufflePig

Frends! Can you help me one matter?

Please tell me.

I would like to create content with ico in utopian.io. But I will choose one of these here? do not understand.

I don't understand what you mean by creating content with ico... can you explain please?

If I upload my content about ico or cryptocurrency at utopian.io, then choose which one from here?

Utopian is for open source contributions... There is no separate option for ICOs. If you are developing an open source ICO project, you can submit it in Development category I think but make sure you read the rules first. Here is an invite to the utopian-io discord. Check there if you need more information. https://discord.gg/bYA2J5