Part 17: 3 Methods To Prevent Double Voting On Posts

in #utopian-io7 years ago (edited)

steem-python.png

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:
Filtering
Voting
Posting
Constructing
Rewards
Transfers
Account Analysis

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

Sort:  

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

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

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

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

Coin Marketplace

STEEM 0.19
TRX 0.15
JST 0.029
BTC 63657.90
ETH 2656.15
USDT 1.00
SBD 2.84