find Current Voting Power with some tcl code to interact with the api.hive.blog

in #hivedevs3 years ago

20220208_170717.jpg

inspired by @condigital and helped by his generous code to show me how it all works using php, I was able to code a class in TCL to work with the json api and have it give me results that I can use with a little digital screen I have.

lcd_display.jpg

the 2nd line shows 88.50% of my voting power, and 13.80 hours until it is full

the 3rd line show 0.109 HP and 0.00 HBD is available to be claimed


Requirements:
tcl8.6
tcllib be installed for json requirement

below is the code interact with the hive json api.

#!/usr/pkg/bin/tclsh
# Marc Bookmeyer 2022
# Class for interfacing api.hive.blog json interface
# voting_power'current -- this calculates the current voting power based upon current time - voting_manabar.last_update_time  
# which may be incorrect, but it works .. until I find a better way and fixing it is simple enough
# voting_power'timeTillFull -- based upon the above calculation to figure how long it would be to fill the voting power bar
# all the below values listed are available in a dictionary or list and simply needs to be pulled for use
# I could have one method for every variable available, and may at some time, but for now it is as needed
# the next part I'd like to see is the mana and downvote

#
proc pdict {dict {pattern *}} {
        set longest 0
        set keys [dict keys $dict $pattern] 
        foreach key $keys {
                set l [string length $key]
                if {$l > $longest} {set longest $l}
        }
        foreach key $keys {
                puts [format "%-${longest}s = %s" $key [dict get $dict $key]]
        }
}
package require json
oo::class create hiveBlog {
        variable userName results rawData dictData regenRate votingPower
        constructor { _userName  } {
                set userName $_userName
                
                #                                20% / ( H *  M * S )      -- Calculate 20% / seconds in a day
                set regenRate [ expr {  20. / ( 24 * 60 * 60 ) } ]
                
        }
        method update {} {
                my _update
                my _parse
        }
        method _update { } {
                set jsonCmd "\{\"jsonrpc\":\"2.0\", \"method\":\"condenser_api.get_accounts\", \"params\":\[\[\"$userName\"\]\], \"id\":1\}"
                set rawData [exec curl -s --data $jsonCmd https://api.hive.blog]
        }
        method _parse {} {
                set dictData [json::json2dict $rawData]
                #puts "DATA\n[dict keys $dictData]"
               #jsonrpc result id -- KEYS 
                set tempResult [my result]
               # strip {} chars, it is a dict now
                set results [string range $tempResult 1 end-1]
               # puts "RESULT\n[dict keys $results]"
                 set votingPower [expr { [dict get $results voting_power]/ 100. }]
      }
        method result {} {
                dict get $dictData result
        }
         method result_keys {} {
                dict keys $dictData result
        }
       method reward_vesting_hive {} {
                lindex [dict get $results reward_vesting_hive] 0
        }
       method reward_hbd_balance {} {
                dict get $results reward_hbd_balance
        }
        method balance {} {
                dict get $results balance
        }
        method hbd_balance {} {
                dict get $results hbd_balance
        }
        method voting_manabar {} {
                dict get $results voting_manabar
        }
        method voting_manabar'last_update_time {} {
#voting_manabar                    = current_mana 2322147643318 last_update_time 1644350472
                lindex [my voting_manabar] 3
        }
        method voting_manabar'offsetSinceLastUpdate {} {
                expr { [clock seconds] - [my voting_manabar'last_update_time] }
        }
        method voting_power'calculatedRegenSinceOffset {} {
                expr { $regenRate * [my voting_manabar'offsetSinceLastUpdate] }
        }
        method voting_power'current {} {
                format %.2f [expr { [my voting_power] + [my voting_power'calculatedRegenSinceOffset] }]
        }
        method voting_power'timeTillFull {} {
                format %.2f [expr { ((100 - [my voting_power'current]) / 20.)  * 24 }]
        }
        method voting_power {} {
                return $votingPower
        }
        method print:test {} {
                puts "[my voting_power] [my reward_vesting_hive] [my reward_hbd_balance] \n \
                [my voting_manabar] \nLast Update time:[my voting_manabar'last_update_time] \nvoting_manabar'offsetSinceLastUpdate:[my voting_manabar'offsetSinceLastUpdate]\n\
                regenRate: $regenRate \n\
                calculatedRegenSinceOffset:[my voting_power'calculatedRegenSinceOffset]\n\
                voting_power'current: [my voting_power'current]%\n\
                voting_power'timeTillFull:[my voting_power'timeTillFull]"
        }
        method print:lcd {} {
                return "[my voting_power'current]% full:[my voting_power'timeTillFull] hr"
        }
         method print:lcd2 {} {
                return "[my reward_vesting_hive] HP  [my reward_hbd_balance]"
        }
       method print:rawData {} {
                puts $rawData
        }
        method print:results {} {
                pdict $results
        }
            
}

#Example
set x [hiveBlog new klevn]
$x update

puts [$x print:lcd]
puts [$x print:lcd2]

here are the results from running the print:results

print:results makes it easy to find wanted variables 
$x print:results
#~ #example output from print:results
#~ id                                = 369459
#~ name                              = klevn
#~ owner                             = weight_threshold 1 account_auths {} key_auths {{STM5zbFmxSgHYAFY8LN2chGFHqCeDvJ1zQ5ngbdKNEikYJx8L6gwT 1}}
#~ active                            = weight_threshold 1 account_auths {} key_auths {{STM7oCAjBQGm1KRuTx5KrY8o5wUDjo9f3fHR3n94x3xCWsfPVUYEn 1}}
#~ posting                           = weight_threshold 1 account_auths {{busy.app 1} {dmania.app 1} {dtube.app 1} {hive.blog 1} {steempeak.app 1} {threespeak 1} {utopian.app 1}} key_auths {{STM89W4ji9BKfbypGpxv8UeNbUhvdXGk5VU5X61e7JKx84bHjqcPQ 1}}
#~ memo_key                          = STM8YpbQGWPuPwVPw2pqgWUTubemiV9HFSjjw8CVpk3mDhpqPqE8q
#~ json_metadata                     = {"profile":{"name":"marcDouglas","about":"love life.  expose truth.  leave the rest behind.","location":"lawrence, kansas","cover_image":"https://steemitimages.com/DQmSkzSz57kmiLpyUcvqQQb7xeUZq4jhvQwDmuirVXb2wB2/IMG_0056.jpg","profile_image":"https://steemitimages.com/DQme8o4PRhqhRDa3epDoKZnrie5Fh8bU2tk8h9BWzhQj9D4/83fcd15c4cd3c0a278c6d278a5f6c85e.jpg"}}
#~ posting_json_metadata             = {"profile":{"name":"marcDouglas","about":"love life.  expose truth.  leave the rest behind.","location":"lawrence, kansas","cover_image":"https://steemitimages.com/DQmSkzSz57kmiLpyUcvqQQb7xeUZq4jhvQwDmuirVXb2wB2/IMG_0056.jpg","profile_image":"https://steemitimages.com/DQme8o4PRhqhRDa3epDoKZnrie5Fh8bU2tk8h9BWzhQj9D4/83fcd15c4cd3c0a278c6d278a5f6c85e.jpg"}}
#~ proxy                             = 
#~ last_owner_update                 = 2017-09-25T14:28:27
#~ last_account_update               = 2020-09-18T00:11:39
#~ created                           = 2017-09-14T10:43:15
#~ mined                             = false
#~ recovery_account                  = steem
#~ last_account_recovery             = 1970-01-01T00:00:00
#~ reset_account                     = null
#~ comment_count                     = 0
#~ lifetime_vote_count               = 0
#~ post_count                        = 2265
#~ can_vote                          = true
#~ voting_manabar                    = current_mana 2322147643318 last_update_time 1644350472
#~ downvote_manabar                  = current_mana 776854223097 last_update_time 1644350472
#~ voting_power                      = 7472
#~ balance                           = 0.020 HIVE
#~ savings_balance                   = 0.000 HIVE
#~ hbd_balance                       = 2.014 HBD
#~ hbd_seconds                       = 1245592101
#~ hbd_seconds_last_update           = 2021-06-28T02:16:21
#~ hbd_last_interest_payment         = 2021-06-11T01:53:36
#~ savings_hbd_balance               = 0.000 HBD
#~ savings_hbd_seconds               = 0
#~ savings_hbd_seconds_last_update   = 1970-01-01T00:00:00
#~ savings_hbd_last_interest_payment = 1970-01-01T00:00:00
#~ savings_withdraw_requests         = 0
#~ reward_hbd_balance                = 0.000 HBD
#~ reward_hive_balance               = 0.000 HIVE
#~ reward_vesting_balance            = 201.024252 VESTS
#~ reward_vesting_hive               = 0.109 HIVE
#~ vesting_shares                    = 3107416.892390 VESTS
#~ delegated_vesting_shares          = 0.000000 VESTS
#~ received_vesting_shares           = 0.000000 VESTS
#~ vesting_withdraw_rate             = 0.000000 VESTS
#~ post_voting_power                 = 3107416.892390 VESTS
#~ next_vesting_withdrawal           = 1969-12-31T23:59:59
#~ withdrawn                         = 0
#~ to_withdraw                       = 0
#~ withdraw_routes                   = 0
#~ pending_transfers                 = 0
#~ curation_rewards                  = 145416
#~ posting_rewards                   = 117666
#~ proxied_vsf_votes                 = 0 0 0 0
#~ witnesses_voted_for               = 30
#~ last_post                         = 2022-02-07T19:38:21
#~ last_root_post                    = 2022-01-25T14:24:39
#~ last_vote_time                    = 2022-02-08T20:01:12
#~ post_bandwidth                    = 0
#~ pending_claimed_accounts          = 0
#~ governance_vote_expiration_ts     = 2022-08-18T17:25:45
#~ delayed_votes                     = 
#~ open_recurrent_transfers          = 0
#~ vesting_balance                   = 0.000 HIVE
#~ reputation                        = 521538790298
#~ transfer_history                  = 
#~ market_history                    = 
#~ post_history                      = 
#~ vote_history                      = 
#~ other_history                     = 
#~ witness_votes                     = aggroed anyx arcange ats-witness ausbitbank blocktrades curie drakos good-karma gtg jesta leofinance lukestokes.mhth netuoso ocd-witness pfunk pharesim roelandp someguy123 steemgigs steemitboard steempeak steempress thecryptodrive themarkymark therealwolf threespeak timcliff ura-soul yabapmatt
#~ tags_usage                        = 
#~ guest_bloggers                    = 

here is a fossil with the entire project with the LCD screen. It is running on a pi4.

if you don't know about fossil .. download it here

then the easiest thing, once you have it installed is to do a

fossil clone https://you.webhop.me/cgi/lcd_sunstar.ui <path to save location for fossil, if not used it will populated in the folder ran in with all the files in the fossil!  yikes>

so

fossil clone https://you.webhop.me/cgi/lcd_sunstar.ui /home/<username>/documents/fossils/lcd_sunstar.fossil

then you open the 'fossil' in a empty folder of your choice

once in the empty folder

fossil open /home/<username>/documents/fossils/lcd_sunstar.fossil

then you have it. if you changed the code, be careful running update, tho it would warn you if a conflict arose.

fossil is a great all around tool for keeping a small project the big project treatment.

Sort:  

Congratulations @klevn! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s):

You distributed more than 11000 upvotes.
Your next target is to reach 12000 upvotes.

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

Check out the last post from @hivebuzz:

Hive Power Up Month - Feedback from February day 22
Support the HiveBuzz project. Vote for our proposal!