Expanding on my last article I exlore further API calls to pull some data from my account and present it in the terminal. Follow along!
If you would like to follow what I'm doing this at home you should check out Part 1. Last time we pulled a list of my followers from my account. Today we want to get some more account data.
This tutorial is written in Ruby with the help of Radiator API client provided by @inertia.
To interact with the steem blockchain we need to connect to a public api. If that node fails, Radiator provides the functionality to fallback on a different node. We can implement it like this:
options = {
ur: 'https://api.steemit.com',
failover_urls: [
'https://api.steemitstage.com'
]
}
api = Radiator::Api.new(options)
Let's begin by grabbing my account and printing the username like this:
my_account = api.find_account('cryptonik')
puts "Looking up account data..."
puts "Account: #{my_account.name}"
Now we can get my STEEM balance, by calling the balance method and converting it to a float:
$steem_balance=my_account.balance.to_f
Very similar for SBD balance:
$sbd_balance=my_account.sbd_balance.to_f
Print it to console with puts:
puts "Balance (STEEM): #{$steem_balance}"
puts "Balance (SBD): #{$sbd_balance}"
We can now pull the voting power, and to get it right (in percent) we divide the stored value by 100:
$voting_power=(my_account.voting_power/100.to_f)
Let's get my reputation. This one is a little tricky. The reputation is stored in a different format and we have to first change it to a human readable format.
Here is @sevinwilson breaking down the math:
Here is me putting that into code:
$reputation=((Math::log10(my_account.reputation.to_f)-9)*9+25)
puts "Reputation: #{$reputation}"
=>51.989230344184215
That looks pretty messy. Let's limit our float to 2 decimal points:
puts "Reputation: %.2f" % $reputation
=>51.99
Much better, we can use the same format for the other puts.
Now we can append a slightly modified followers count from last time. This time we won't print the whole list but rather just grab the number of followers. Remember, we can call the length method on the array of users constructed by map.
followapi = Radiator::FollowApi.new
response = followapi.get_followers('cryptonik', 0, 'blog', 1000)
followers_ary = response.result.map(&:follower)
$sum_followers=followers_ary.length
puts "followers: #{$sum_followers}"
Make a file in your editor of choice and save it with the *.rb extension.
require 'radiator'
options = {
ur: 'https://api.steemit.com',
failover_urls: [
'https://api.steemitstage.com'
]
}
api = Radiator::Api.new(options)
my_account = api.find_account('cryptonik')
puts "Looking up account data..."
puts "Account: #{my_account.name}"
$steem_balance=my_account.balance.to_f
$sbd_balance=my_account.sbd_balance.to_f
$voting_power=(my_account.voting_power/100.to_f)
$reputation=((Math::log10(my_account.reputation.to_f)-9)*9+25)
puts "Reputation: %.2f" % $reputation
puts "Voting power: %.2f" % $voting_power
puts "Balance (STEEM): %.2f" % $steem_balance
puts "Balance (SBD): %.2f" % $sbd_balance
followapi = Radiator::FollowApi.new
response = followapi.get_followers('cryptonik', 0, 'blog', 1000)
followers_ary = response.result.map(&:follower)
$sum_followers=followers_ary.length
puts "followers: #{$sum_followers}"
$ruby myfollowers.rb
Feel free to try this at home and if you get stuck you can always ping me.
Link to Part 1 of this series.
Resources:
Radiator docs
Radiator github
Steem github