Retrieve Steem and SBD Balances and Voting Power of Your Account Using Python and steem-python Modules

in #steemit6 years ago (edited)

This post contains a variation on code that I published in a previous post. The difference between this post and that post is that here I fully use the steem-python library classes "Account", "Amount", and "utils". The previous post only used the "Steem" class and the Python "datetime" class. The previous post where I don't use the steem-python classes lets you know what is going on "under the hood".

I used two-fewer lines of code and my code is more elegant and simple when I use the steem-python library classes.

Tips on installing and using steem-python on an Apple or Linux computer
Download and install steem-python
Download and install Python

#This script will print out the SBD and STEEM balances and the current user's voting
# power.

from steem.account import Account
from steem.amount import Amount
from steem import utils

# Substitute your username for "numberjocky".

userName = 'numberjocky'

# Get the account data for userName

userAcct = Account(userName)

# The SBD and Steem balances of the account are the values of the "sbd_balance" and
# "balance" keys in the userAcct dictionary object.  The keys are two-word
# strings that can be input into the steem-python "Amount" module and the values
# of the balances can be extracted using the "amount" variable of the Amount object.
# N.B.  Amount only works on two-word strings, not one-word values

sbdBalance = Amount(userAcct['sbd_balance']).amount
steemBalance = Amount(userAcct['balance']).amount

# The voting_power and last_vote_time store the voting power of the user the last time
# they voted.  To compute the present voting power, the last_vote_time must be subtracted
# from the present time and the difference multiplied by the vote replenishment factor
# of 20% per day.  Then, the result is added to the voting power at the last vote time to
# determine the current voting power.  The "time_elapsed" function in the steem-python
# "utils" module is used to calculate the amount of time since the last vote.  It outputs
# a "timedelta" object (timeSinceLastVote) as defined in Python's own "datetime" module.

votingPowerLast = userAcct['voting_power']/100
timeSinceLastVote = utils.time_elapsed(userAcct['last_vote_time'])
votingPower = timeSinceLastVote.seconds*20/86400+votingPowerLast
if votingPower > 100:
    votingPower = 100
    
# Print out the results

print("%s" % userName)
print("%s SBD" % sbdBalance)
print("%s STEEM" % steemBalance)
print("%.5s voting power" % votingPower)

Enjoy!

Coin Marketplace

STEEM 0.15
TRX 0.16
JST 0.028
BTC 68804.96
ETH 2441.52
USDT 1.00
SBD 2.33