Steemit-Python-Utility | Part 1: How to give away SBD/STEEM to your commentators

in #utopian-io7 years ago (edited)

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.

image.png

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.

image.png

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.

image.png

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...

image.png

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…

image.png

Finally, we only need to return the author of the top comment.
image.png

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.

image.png

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.

image.png

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.

image.png

Secondly, we need to get our winners with the previous functions and create a list with the returned value of each function.

image.png

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.

image.png

Final Code (Github.com)

Curriculum

Steemit Python Bots
Steemit Python Analysis



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

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 ...

git clone https://github.com/steemit/steem-python.git
cd steem-python
pipenv install --three --dev # use --two instead of --three for Python 2.7
pipenv install .

I think this someone worked in the end but on my main windows machine I used a tutorial by @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.

if i["author"] not in commentators:

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

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 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.

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 on how to be a steemit rockstar! I was made by @EarthNation to make Steemit easier and more rewarding for minnows.

Requested by @wil1liam

Hey @wil1liam I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

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!