Introduction
Recently, I saw more and more people giving away SBD oder ETH for their followers, who commented on their post. I’m not aware of any solution to automate such a process and therefore I created a simple script to do so.
What Will I Learn?
- How to get ALL comments from a post
- How to choose a random winner from your comments
- How to get the most upvoted comment
- How to send SBD/STEEM
Requirements
- Python 3.6
- Steemit-Python
Difficulty
- Basic
Tutorial Contents
Basic Setup
Obviously, we need to import steem
first but to choose a random winner we also have to import the built-in library random
. Then we need to set the node
, include our private posting key and our private active key in order to send SBD/STEEM later and finally need to initialize the Steem Class
.
Having finished the basic stuff, we should start getting the comments from our post. Because we do not want to change the url in the code every time using the script, I decided to ask for a user input.
get_all_comments()
Getting all comments may seem simple, however if we just use the simple function s.get_content_replies(account,post_identifier)
, we will not get all comments, any sub-comment will not be included…. With the help of a for loop
we can get the replies of every comment under our post. The children
element will tell us how many replies a comment has.
Now we simply add these new comments to our list of the original comments and return it. We are done with our first function.
random_winner()
Let us continue with choosing a random winner. This is actually the most straightforward function but we still want to prevent abuse by users posting a lot of comments. So no matter how many comments you write your chance always stays the same. To achieve this we create an empty list in which we will save all commentators and choose a random winner from that. Using a for loop
and an if statement
we will go through every comment and add the commentator to the list if he isn’t included already. Then we use the function random.randrange(length(commentators_list))
to return a random number and finally just return the entry of this number from the commentators list...
community_winner()
Now we want to get the comment who got the most upvotes. You may think this is the first comment under each post but that’s only the comment with the highest payout. Because, in this case I want every upvote to be weighted equally we will use the net_votes
of the comment.
Usually the first comment will be the one with the most upvotes, so we simply set it as our top comment but to make sure we will go through all comments again and check if they have more upvotes. If one of them has more we set it as our new top comment and continue through our list of comments…
Finally, we only need to return the author of the top comment.
reward_winners()
For transferring funds on the Steem Blockchain we can use this command: transfer(to, amount, asset, memo='', account=your_account)
. I only created an extra function to simplify the process of sending the funds…
We will need a for loop
to go through the list of our winners and send the funds for each winner. In addition, I created a few variables: asset (SBD/STEEM), reward_pool, memo for the random winner, memo for the community winner and a list including all the before created memos. Each winner will get an equal amount of SBD/STEEM: 1 / length(winners_list) * reward_pool
.
Furthermore, every winner we will receive a different memo from our lists of memos. To know which memo to use we will use the i
of the for loop in range(length(winners))
. If we only have a list of 2 winners, i
will be 0 and then 1. If you have three winners or more, please make sure to have a list of memos of the same length and that the memos are arranged in the same order as the list of the winners.
To be fair, it would require less lines of code for only 2 winners, if we just wrote the transfer part without any functions...
main programm
Now we have written all functions but we will still need to execute them. First of all, let us set the variable comments
to our function get_all_comments()
and use the url as an input.
Secondly, we need to get our winners with the previous functions and create a list with the returned value of each function.
As the last step in the tutorial, we will need to send the rewards. I prefer to print the returning value of this function to determine if the money was sent.
Final Code (Github.com)
Curriculum
Steemit Python Bots
- Part 1: How to find someone's newest post
- Part 2: How to return a list of the people who upvoted your post
- Part 3: Relike someones post if they liked yours
Steemit Python Analysis
Posted on Utopian.io - Rewarding Open Source Contributors