Part 17: 3 Methods To Prevent Double Voting On Posts
This tutorial is part of a series where different aspects of programming with steem-python
are explained. Links to the other tutorials can be found in the curriculum section below. This part will look at 3 different methods to prevent trying to upvote a post already upvoted. These methods can be used for script that automatically upvote posts.
What will I learn
- How to prevent double upvotes
- Check for post time
- Create a list of posts upvoted
- Check in post data if already upvoted by account
Requirements
- Python3.6
steem-python
Difficulty
- Basic
Tutorial
Setup
Download the files from Github. There is 1 file upvoter.py
which contains the code. There are 3 variables that have to be set, where the identifier
is the post we want to check for and max age
is the maximum age of the post allowed.
account = "sttest1"
identifier = '@photocontests/photocontests-selection-201801260600032712'
max_age = datetime.timedelta(minutes=30)
Run scripts as following:
> python upvoter.py
Error message
Trying to upvote a post already upvoted leads to the following error message. Whenever a user edits his/her post the post can be seen as a new post on the blockchain when streaming for posts. As there is no distinction between edited and new posts.
You have already voted in a similar way
Catching these errors would normally be sufficient to prevent a double vote on the same post. However when working with a dynamic voting weight, upvoting with a different voting weight will update the upvote. This will not replace the previous vote, so voting power is drained twice for 1 vote.
Check for post time
A simple method would be to look at the current age
of the post and filter any post older than x
amount of time. However this is not fail proof as an user can edit before the maximum time
is reached.
import datetime
current_time = datetime.datetime.now()
max_age = datetime.timedelta(minutes=30)
creation_time = post['created']
if current_time - creation_time > max_age:
print ('post is to old')
Create a list of posts upvoted
A fool proof but disk space intense method is to store each identifier
for every post upvoted. These can also be stored in a database to work together with a website for example. The identifier
can be found in the post object
.
upvote_list =[]
if post['identifier'] not in upvote_list:
try:
steem.vote(post['identifier'], vote_percentage, account)
upvote_list.append(post['identifier'])
except Exception as e:
print (repr(e))
Check in post data if already upvoted by account
The most efficient way to to check if the post is already upvoted is in the post object
on the blockchain. Inside the post object
there is a list active_votes
. Inside these lists are dicts containing the upvote data. The account can be checked against 'voter'
{
'voter': 'sttest1',
'weight': 118,
'rshares': 122857038,
'percent': 10000,
'reputation': -15644899714,
'time': '2018-01-29T00:05:36'
}
post_votes = post['active_votes']
print (post['active_votes'])
for upvoter in post_votes:
if upvoter['voter'] == account:
print ("already voted")
Running the script
Running the script will simple upvote the post and then try to do it again.
python upvoter.py
Post upvoted
Waiting 3 seconds
Already voted
Curriculum
Set up:
- Part 0: How To Install Steem-python, The Official Steem Library For Python
- Part 1: How To Configure The Steempy CLI Wallet And Upvote An Article With Steem-Python
Filtering
- Part 2: How To Stream And Filter The Blockchain Using Steem-Python
- Part 6: How To Automatically Reply To Mentions Using Steem-Python
Voting
- Part 3: Creating A Dynamic Autovoter That Runs 24/7
- Part 4: How To Follow A Voting Trail Using Steem-Python
- Part 8: How To Create Your Own Upvote Bot Using Steem-Python
Posting
- Part 5: Post An Article Directly To The Steem Blockchain And Automatically Buy Upvotes From Upvote Bots
- Part 7: How To Schedule Posts And Manually Upvote Posts For A Variable Voting Weight With Steem-Python
Constructing
Rewards
- Part 9: How To Calculate A Post's Total Rewards Using Steem-Python
- Part 12: How To Estimate Curation Rewards Using Steem-Python
- Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python
Transfers
- Part 11: How To Build A List Of Transfers And Broadcast These In One Transaction With Steem-Python
- Part 13: Upvote Posts In Batches Based On Current Voting Power With Steem-Python
Account Analysis
- Part 15: How To Check If An Account Is Following Back And Retrieve Mutual Followers/Following Between Two Accounts
- Part 16: How To Analyse A User's Vote History In A Specific Time Period Using Steem-Python
The code for this tutorial can be found on GitHub!
This tutorial was written by @juliank in conjunction with @amosbastian.
Posted on Utopian.io - Rewarding Open Source Contributors
Thank you for the contribution. It has been approved.
You can contact us on Discord.
[utopian-moderator]
Hey @vladimir-simovic, 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!
This is what I am looking for, thank you very much
Thanks information
Hey @steempytutorials I am @utopian-io. I have just upvoted you!
Achievements
Suggestions
Get Noticed!
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