Steem-Python Beginner Series : Competition Script - Picking a Random Winner

in #steemdev7 years ago (edited)

So you're hosting a competition, you're looking for a way to announce a random winner that fits a certain criteria and maybe you feel like getting to know the Steem-Python library a little better too. Well look no further. I'll be starting a new tutorial series called "Steem-Python Beginner Series" where I'll be going through a few methods in the steem-python library each time and show you how to use them and how you can apply them in your own scripts. As always, in order to use this script you need to have the steem-python library installed which you can get by running the following command: pip install steem . You'll also need to be running at least Python version 3.5 to use the library and have pip installed to run the above command.

562163adf14aee88c3e14ad522583038--java-code-coding.jpg
Badly photoshopped image. original

Today I'll be using the following methods from the library:

  1. get_active_votes()
  2. get_reblogged()
  3. get_followers()

In this example I'll be sharing a little script I wrote that you can use to find a winner in a competition you're hosting on Steemit. So lets say your competition rules are that everyone should upvote your post, resteem your post as well as follow you and that you will be picking a random lucky winner to win some money or whatever you feel like giving away. The following script will retrieve all the people who voted on your post, all the people who reblogged your post and all the people currently following you. The script then creates a list of all potential winners who fit the criteria and picks a random winner. You can copy this script or get it on my GitHub.

from steem import Steem
import random

#create steem instance
s = Steem()

#set your username
#my username as example
user = 'benniebanana' 
#set the link to your post
#my previous post for example
postLink = 'getting-started-with-steem-python-upvote-and-comment-bot-examples-linux' 

#get all the people who voted on the post
voters = s.get_active_votes(user, postLink)
#create a list of all the voters
voterList = [user['voter'] for user in voters]
#get a list of all the people who reblogged the post
reblogs = s.get_reblogged_by(user, postLink)
#get of your follower count 
followerCount = s.get_follow_count(user)['follower_count']
#retrieve all your followers names
followers = s.get_followers(user, 0, 'blog',  followerCount)
#create a list of all your followers
followerList = [follower['follower'] for follower in followers]
#create a list of all the people who voted, reblogged and followed you
potentialWinnerList = []

for potentialWinner in voterList:
    if potentialWinner in reblogs and potentialWinner in followerList:
        #user is following us and reblogged our post so lets add them to the list
        potentialWinnerList.append(potentialWinner)

#pick a random winner from the list
#check if our list is empty
if not potentialWinnerList: 
#our list is empty :(
    print("No winners :( The list is empty")
else:
#we have some potential winners so lets pick a random one
    winner = random.choice(potentialWinnerList)
    print("yaaay "+winner+" won the competition!!!")



Feel free to use and edit this script as you please. As always, let me know if you spot any errors and feel free to hit me up in the comments to ask any questions or to let me know what you think. For the full Steem-Python documentation you can visit this link. A big thanks to @furion for creating this library. You can view all his repositories over here.

.

Sort:  
Loading...

Awesome homie, keep steeming!

Thanks man. Appreciated

Once you get the hang of Python it becomes really fun to use!

I agree. I started with Python about a month ago when I signed up to Steemit and now I'm in love with it.

Thank you for sharing this valuable information.

My pleasure. Glad you liked it :) There's more coming so stay tuned.

Cool script! I need to take a quick class so I can start using some of these awesome tools! Btw, those fish arms and fingers are classic! 😂😂😂

haha thanks

Absolutely brilliant!

Upvoting and resteeming this ! ! !

Thanks dude! Appreciate the comment

Looking at your code looks so easy lol

I'm new at Python and new at Steemit.
Putting these two together is a perfect way of learning!
I hope you will write a lot of these beginner steemit/python tutorials.

Hey thanks for the reply and yes, I'll be doing more of these in the future so stay tuned!

I tried your code and it is working fine although I was having an error for a while until I figure it out, that I only posted one short introduction post, that of course no one re-blogged, so my list of potential winners was empty and that's why program crashed.

Maybe a quick check if list of potential winners is not empty before running random may be a slight improvement :-)

Ah thanks for pointing that out @veleje . I updated it and added some validation before picking a winner :)

This is amazing thank you for sharing

Thanks! Glad you liked it. There will be more of these examples/tutorials coming in the future so stay tuned.

Can you help with this error?

.local/lib/python3.5/site-packages/steem/account.py", line 8, in module
from funcy.simple_funcs import rpartial
ImportError: No module named 'funcy.simple_funcs'

try pip3 install funcy==1.8

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by Stéan from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, and someguy123. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in 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 like what we're doing please upvote this comment so we can continue to build the community account that's supporting all members.

Congratulations @benniebanana! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of comments

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Hi, what do you mean? You can run the script on as many posts as you like. You just have to add in your username and the link to the post and run it. It will pick a winner and you can then rerun it again as many times as you'd like.