Updating the Random Voter, to require some reputation.

in Programming & Dev3 years ago

442_4427900_reptilia_mascot_sass_the_snake_python.png

1 Upvote Random, Improved Version

So with my last script being popular, but having a major flaw I decided, I would want to rectify this in the simplest manner possible. So today I will extend the random voter, by selecting only accounts with a minimum reputation, where the reputation is configurable so if you want to help new people you can set it low though I recommend a minimum of 30. If you are going for a more profit oriented strategy you can put it higher.

anyway let's just take all the code from the last post.

from beem import Hive
from beem.discussions import Query, Discussions

h = Hive ()
import time
import os
time.sleep(r*60) # Funny this is a bug.
q = Query(limit=10, tag="")
d = Discussions()
posts = d.get_discussions('created', q, limit=10)
list_of_posts = [p for p in posts]
import random

def random_int(m):
return(random.randint(0,m))
r=random_int(10)
po = list_of_posts[r]

cmd = "beem_upvote.sh "beempy upvote -a iobates -w 5 ", po, """
os.system(cmd)

Now we need to implement a function that will verify whether the post we choose is from an account that has the required reputation, then we can vote for said account. Possibly also write any vote to a log file, just to make it easier to check it out later. Perhaps also add some stuff with regards to the available voting power, so we are not running out empty. Perhaps if we go below 30% we should cool off a bit.

1.1 Finding the reputation of the account we will vote for

Okay so going about finding the reputation of the account we will vote should not be all that difficult. But as I have not used beem all that extensively I need to do a bit of research.

from beem.account import Account
from beem import Hive
from beem.nodelist import NodeList

nodes = NodeList()
nodes.update_nodes()
stm = Steem(node=nodes.get_hive_nodes())

def account_reputation(name):
acc = Account(name, blockchain_instance=stm)
return(acc.get_reputation)

Doc

Okay so I got that digged up in the documentation, that is at least the first step. For a minimal product I honestly need only write the small function that gets the name from the permlink, which is really easy. So perhaps I should at least add the log file as well, which is not really all that hard either. With a log file it is pretty decent I think.

1.2 Name of the account we will vote for

# The permlink looks like @account/link-name
def get_account_name(plink):
        [name, link]=plink.split('/')
        return(name[1:])

1.3 Putting it together

Okay so now we need to get the name of the account we will consider for our vote.

from beem import Hive
from beem.discussions import Query, Discussions
from beem.account import Account
from beem import Steem
from beem.nodelist import NodeList
import random
import time
import subprocess

req_rep = 35 # just to have a value

log_file = "votes.log"

nodes = NodeList()
nodes.update_nodes()
stm = Steem(node=nodes.get_hive_nodes())

# The permlink looks like @account/link-name
def get_account_name(plink):
[name, link]=plink.split('/')
return(name[1:])

def account_reputation(name):
acc = Account(name)
return(acc.get_reputation())

def random_int(m):
return(random.randint(0,m))

def write_vote(s):
f=open(log_file,'a')
f.write(s)
f.close()

h = Hive ()
q = Query(limit=10, tag="")
d = Discussions()
def get_post_to_vote():
posts = d.get_discussions('created', q, limit=10)
list_of_posts = [p for p in posts]
r=random_int(9)
print(list_of_posts)
print(r)
po=list_of_posts[r]
return(po)

p=get_post_to_vote()

def cast_vote():
if (account_reputation(p.author) > req_rep):
cmd = "beem_upvote.sh "beempy upvote -a iobates -w 5 " + str(p) + """
subprocess.call (cmd, shell=True)
write_vote(str(p))
else:
get_post_to_vote()
cast_vote()

cast_vote()