This tutorial is a continuation of the Part 2: How To Stream And Filter The Blockchain Using Steem-Python series and How to create a simple autovoter using steem-python by @amosbastian. Be sure to check up on those tutorials first so you are fully up to date.
What Will I Learn?
- Learn how to keep python scripts running on your server
- Make a more advanced upvoter
- Introduction with the Account class
- Make a dynamic upvote weight based on account voting power
Requirements
- Python3.6
- steem-python
- tmux
Difficulty
- Basic
Tutorial
Keep your python script running
If you want to make scrips that run 24/7 you will have to use a server to run these scripts. Connecting to a server normally goes with ssh
from your own pc. This works great, however when you shut down your terminal the python script you were running also gets shut down. To prevent this we will be using tmux
to create a session for each script. When we reconnect to our server we can then reconnect to whatever tmux session we would like and also receive the output of that script, great! So how does this work? We will be creating a session called upvoter, reconnect to it and then destroy it.
Create a new session
tmux new -s upvoter
Reconnect to the session
tmux a -t upvoter
Destroy the session
tmux kill-session -t upvoter
Get a list of all active sessions
tmux ls
When connected to a session you can run your python script as normal. Now you can close your terminal and reconnect to the session later. Your script will still be running.
Upgrading the simple upvoter
Start by downloading upvoter.py
and upvote_list.json
from here. We have made some changes. It now uses the file upvote_list.json
to get a dict of whom it will be upvoting. Every user that you wish to upvote has two variables, one indicating the weight of the upvote and the other one how many upvotes this person can receive every day. Either use the provided list or adjust it to your own liking. Each user is defined as follows:
"steempytutorials": {
"upvote_weight": 100,
"upvote_limit": 1
}
To make this bot simple it stores in memory a record of how many upvotes each person has received for that day. This means when you turn off the bot this data is lost. To make sure you do not have to turn off the bot, the bot reloads the upvote_list.json
every hour. This means you can make changes to the file without shutting down the bot, and every hour the bot will update itself. Also once a day the bot clears the memory of who it upvoted. This is done with the following code:
if int(time.strftime("%d")) is not date:
upvoted = {}
date = int(time.strftime("%d"))
if int(time.strftime("%-H")) is not hour:
upvote_list = json.load(open('upvote_list.json'))
hour = int(time.strftime("%-H"))
At this stage you have a bot which you can run 24/7 in a tmux session on your server. Upvoting any user that you wish to upvote for a set amount of times and set upvote weight each day. You are also able to freely update your list of users whom you wish to be upvoted and this is automatically loaded into your bot every hour.
Account class introduction
The account class can be used to get any data related to your account, like for example your current voting power. First import the right module and then create an account object. Calling the voting_power()
function will return the current voting power of the account.
from steem.account import Account
user = Account('steempytutorials')
print ("Voting power is at {}%".format(user.voting_power()))
python account.py
Voting power is at 98.0%
First weekly challenge, 3 steem prize pool
With all the information given and what you have learned in this tutorial you will be able to alter the upvote bot in such a way that it will divide the upvote weight that a user will get from the bot by 2 when the voting power of the account used for upvoting is lower than 90%.
Please provide a link below to the full code used to perform this. There are two prizes, the first person to provide a valid solution will receive 50% of the prize pool. The other 50% will be rewarded for the most elegant solution, for this we will be looking at efficiency in coding.
Curriculum
- 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
- Part 2: How To Stream And Filter The Blockchain Using Steem-Python
Credits
This tutorial was written by @juliank in conjunction with @amosbastian.
The code for this tutorial can be found on GitHub!
Posted on Utopian.io - Rewarding Open Source Contributors