Random Voter, A small script to vote randomly every few minutes

1 Random Voter

So I kind of think this could be a cool project.

1.1 Goal

I want to have this be a script that I run every few minutes that will vote for a few posts. But since I will run it in cron, I want to make sure that it is so it will wait a bit before it starts to avoid a specific time to be better to post. Ideally this should make me a bit of money just running on a pi.

1.2 Setup

Okay so I have not actually done any setup for this yet, but since this is an org-mode file in emacs, I can do it in this file

pip3 install beem

Having installed this we are ready to interact with Hive more directly.

1.3 Coding

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

h = Hive ()

Minimal setup, but it should be sufficient for my needs.

q = Query(limit=10, tag="")
d = Discussions()

q for querying the latest post along with d, both are needed as far as I can tell.

The actual query part is fairly simple in my case

posts = d.get_discussions('created', q, limit=10)

And then I need to get the random number so I can vote for one of the posts

import random
r=random.randint(0,10)

So now I just need to glue all these parts together, and I have a minimal script that does the thing I set out to design.

1.4 Oops

Turns out that the just getting the posts are not that useful, since it is a very limited number I will just convert to a list instead of a generator, can't take a random element of a generator. I know there is itertools and such, but I prefer not to import more than necesarry, and I don't want to write a lot of repetition code either, so this is what I will do.

posts = d.get_discussions('created', q, limit=10)
list_of_posts = [p for p in posts]

Now I can take the random number in r and use that to get the post I want to vote for. I could turn r into a function call, so I can just call the function and get a new random number and use it for several things instead of just one, it is not even very complicated to that.

import random

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

This would probably make everything much simpler later on.

1.5 Casting a vote.

So me being a bit lazy and not wanting to sit down and read up on how to write the code to actually cast a vote, when I can do a lazy version and use a commandline utility instead of writing the code.

So I want to put my key in the variable UNLOCK for the duration of the call to beempy, now I will not actually put this code in here for obvious reasons. However it is as simple as

UNLOCK=YOURKEYHERE ; command

when command finishes UNLOCK should no longer be set, so it is not a big security issue, surely it is better than putting the key in a piece of code.

So I think the easiest way to do this is to just run this script as the command in that example, that takes away a lot of the problems.

I almost forget the command to cast the vote on the commandline is

beempy upvote -a 'accountname' -w percentage @user/permlink

replace things in for the right values and if you did setup your beempy correctly you can upvote on the commandline, which is mostly useful for scripts such as what I am writing right now.

1.6 Handling the keys safely

Key safety is probably the biggest problem to solve while working on something like this, I have a few ideas how it can be done though.

1.6.1 Variable in external script

So one solution is to write a script

UNLOCK=KEYHERE ; $1

This has the nice effect of running the thing you pass to it with the context of the key set, probably the easiest solution.

1.6.2 Storing key in text file

You could do this, but don't do it unless you encrypt the text file, and then we are sort of back to the initial problem safely handling the key. Perhaps not the best idea in the world.

1.6.3 hiding the key in source code

Okay this is not a good idea, since what safe guards do you have on source code ? No I think the variable in script is better (although it is kind of this one too). At least that script is not going to be under version control.

1.7 Even a good idea

Okay perhaps this is not the best idea in the world, since there is some content I would not want to encourage. Basically anybody who just repost something off from the net, regardless actually.

But I might do it anyway, it is mostly good content on hive.

1.8 Putting the thing together

So with all the pieces build, we only need to put them together, and possibly add a littly glue code to make it stick together.

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

h = Hive ()
import time
import os
time.sleep(r*60)
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)

Okay so beempyupvote.sh is just a small script that takes the function call as input and prepend the UNLOCK=KEY infront.

Also I have not really tested it, so beware of bugs.

H2
H3
H4
3 columns
2 columns
1 column
4 Comments
Ecency