Introduction
<p dir="auto">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.
<h4>What Will I Learn?
<ul>
<li>How to get ALL comments from a post
<li>How to choose a random winner from your comments
<li>How to get the most upvoted comment
<li>How to send SBD/STEEM
<h4>Requirements
<ul>
<li>Python 3.6
<li>Steemit-Python
<h4>Difficulty
<ul>
<li>Basic
<h4>Tutorial Contents
<h5>Basic Setup
<p dir="auto">Obviously, we need to import <code>steem first but to choose a random winner we also have to import the built-in library <code>random. Then we need to set the <code>node, include our private posting key and our private active key in order to send SBD/STEEM later and finally need to initialize the <code>Steem Class.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639475/aocwjbdmx15taflqif8q.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639475/aocwjbdmx15taflqif8q.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639475/aocwjbdmx15taflqif8q.png 2x" />
<p dir="auto">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.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639808/xadureawo946eozrnxui.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639808/xadureawo946eozrnxui.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639808/xadureawo946eozrnxui.png 2x" />
<h5><code>get_all_comments()
<p dir="auto">Getting all comments may seem simple, however if we just use the simple function <code>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 <code> for loop we can get the replies of every comment under our post. The <code>children element will tell us how many replies a comment has.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639530/igpxlrrgolq0rrbyqtsd.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639530/igpxlrrgolq0rrbyqtsd.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639530/igpxlrrgolq0rrbyqtsd.png 2x" />
<p dir="auto">Now we simply add these new comments to our list of the original comments and return it. We are done with our first function.
<h5><code>random_winner()
<p dir="auto">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 <code> for loop and an <code>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 <code>random.randrange(length(commentators_list)) to return a random number and finally just return the entry of this number from the commentators list...
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639557/vfdg7kfxqgcsey0zdaun.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639557/vfdg7kfxqgcsey0zdaun.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639557/vfdg7kfxqgcsey0zdaun.png 2x" />
<h5><code>community_winner()
<p dir="auto">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 <code>net_votes of the comment.
<p dir="auto">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…
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639620/x9fxweejjszw3uhr3kqd.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639620/x9fxweejjszw3uhr3kqd.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639620/x9fxweejjszw3uhr3kqd.png 2x" />
<p dir="auto">Finally, we only need to return the author of the top comment.<br />
<img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639637/ppthwh0i2cwnhinka7un.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639637/ppthwh0i2cwnhinka7un.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639637/ppthwh0i2cwnhinka7un.png 2x" />
<h5><code>reward_winners()
<p dir="auto">For transferring funds on the Steem Blockchain we can use this command: <code>transfer(to, amount, asset, memo='', account=your_account). I only created an extra function to simplify the process of sending the funds…
<p dir="auto">We will need a <code>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: <code> 1 / length(winners_list) * reward_pool.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639678/ip8gqo3oyhubwulfkup4.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639678/ip8gqo3oyhubwulfkup4.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639678/ip8gqo3oyhubwulfkup4.png 2x" />
<p dir="auto">Furthermore, every winner we will receive a different memo from our lists of memos. To know which memo to use we will use the <code>i of the for loop in <code>range(length(winners)). If we only have a list of 2 winners, <code>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.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639696/patjyycwmvwsv0hu6xh8.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639696/patjyycwmvwsv0hu6xh8.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639696/patjyycwmvwsv0hu6xh8.png 2x" />
<p dir="auto">To be fair, it would require less lines of code for only 2 winners, if we just wrote the transfer part without any functions...
<h5>main programm
<p dir="auto">Now we have written all functions but we will still need to execute them. First of all, let us set the variable <code> comments to our function <code>get_all_comments() and use the url as an input.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639716/heoztqnifwfl9akjppjd.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639716/heoztqnifwfl9akjppjd.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639716/heoztqnifwfl9akjppjd.png 2x" />
<p dir="auto">Secondly, we need to get our winners with the previous functions and create a list with the returned value of each function.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639745/fsauoujqhvknoui3veqq.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639745/fsauoujqhvknoui3veqq.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639745/fsauoujqhvknoui3veqq.png 2x" />
<p dir="auto">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.
<p dir="auto"><img src="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639760/awobs0owcbehesmyu7ya.png" alt="image.png" srcset="https://images.hive.blog/768x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639760/awobs0owcbehesmyu7ya.png 1x, https://images.hive.blog/1536x0/https://res.cloudinary.com/hpiynhbhq/image/upload/v1520639760/awobs0owcbehesmyu7ya.png 2x" />
<h3><a href="https://github.com/ko-redtruck/steemit-comments-rewarder" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Final Code (Github.com)
<h4>Curriculum
<h5>Steemit Python Bots
<ul>
<li><a href="https://steemit.com/utopian-io/@wil1liam/steemit-python-bot-or-part-1-how-to-find-someone-s-newest-post" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Part 1: How to find someone's newest post<br />
<li><a href="https://steemit.com/utopian-io/@wil1liam/steemit-python-bot-or-part-2-how-to-return-a-list-of-the-people-who-upvoted-your-post" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Part 2: How to return a list of the people who upvoted your post<br />
<li><a href="https://steemit.com/utopian-io/@wil1liam/steemit-python-bot-or-part-3-relike-someones-post-if-they-liked-yours" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Part 3: Relike someones post if they liked yours
<h5>Steemit Python Analysis
<ul>
<li><a href="https://steemit.com/utopian-io/@wil1liam/steem-power-delegations-sale-blocktrades-what-are-people-doing-with-their-sp-analysis" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Steem Power delegations sale (BlockTrades) - What are people doing with their SP? - Analysis<br />
<p dir="auto"><br /><hr /><em>Posted on <a href="https://utopian.io/utopian-io/@wil1liam/steemit-python-utility-or-part-1-how-to-give-away-sbd-steem-to-your-commentators" target="_blank" rel="noreferrer noopener" title="This link will take you away from hive.blog" class="external_link">Utopian.io - Rewarding Open Source Contributors<hr /><p>
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
I am not being able to get steem-python installed in my laptop :(
It's great to see you developing tools using steem-python :)
a few days ago I also struggled really hard to install it on my raspberry pi 3 ...
<p dir="auto"><code>git clone https://github.com/steemit/steem-python.git<br /> cd steem-python<br /> pipenv install --three --dev # use --two instead of --three for Python 2.7<br /> pipenv install .<br /> <p dir="auto"><span>I think this someone worked in the end but on my main windows machine I used a tutorial by <a href="/@holger80">@holger80.thanks for pointing me to @holger80, it helped :)
This is a great tutorial. I learnt something new as a beginner in python. To prevent abuse we can just check if it's already in the list.
I use STEEM piston but I may start learning STEEMIT-Python for fun.
Thanks a lot @wil1liam, well deserved upvotes ;)
Great article. I'm going to have to try to do this one day.
Awesome
Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.Congratulations! This post has been upvoted from the communal account, @minnowsupport, by ReD_TrucK from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the
If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.
Greetings! I am a minnow exclusive bot that gives a 5X upvote! I recommend this amazing guide
Hey @wil1liam I am @utopian-io. I have just upvoted you!
Achievements
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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
great tut man!