My Progress to Orca in Python (using JupyterLab)

in HiveDevs4 years ago (edited)

It has been a while!

Since my last post... I have been lacking the necessary time and disposition to share some interesting stuff. To add on that, life has not been easy lately, so, yep... I was just enjoying watching things around. Mostly playing #HIVEGames and watching the progress of the HIVE dev activity.

Disclaimer: this is a really basic example, but just felt like a good simple one on how to show everyone an easy way to play with the HIVE API.

Note: This was driven from Windows (using JupyterLab from Anaconda) but you can do the same from any Linux machine. The idea was to show how that in the Windows platform you can do the same experiments.

The main part of the code

This includes the url to the API node you are requesting the information. Change that to another one if that's your intention. Then, there is some encoding format of the message being sent, and finaly the API call documented here. Lastly, you send the request, by post method.

import requests
import json from requests.structures import CaseInsensitiveDict

#url = "https://rpc.esteem.app"
url = "https://api.hive.blog"

headers = CaseInsensitiveDict()
headers["Content-Type"] = "application/x-www-form-urlencoded"

# API request for the get_accounts function for the <user> in "params":[["<user>"]]
data = '{"jsonrpc":"2.0", "method":"condenser_api.get_accounts", "params":[["forykw"]], "id":1}'

# Sending the API request
resp = requests.post(url, headers=headers, data=data)

This output is just to understand how the communication goes

# Print the status and reason of the request
print("Status Code:",resp.status_code,"Reason:",resp.reason)

# Print how long took for the request to be received
print("Request time:",resp.elapsed)

In this case all was good (quite fast response too)

Status Code: 200 Reason: OK
Request time: 0:00:01.054829

I usually get under the 1 second of response time. This one was one of the worst ones.

If you need to see all the output you are getting

Uncomment this line and you will get the entire response in JSON format.

# If you need to see the detailed output of the request in JSON format, uncomment this
#json.loads(resp.text)

Then the actual requests

I have added a couple more things just to exemplify how can you get other things form the JSON output.

# Save the request output into a variable
json_data = json.loads(resp.text)

# Print the account HIVE Balance
print("HIVE Balance:",json_data['result'][0]['balance'])

# Print the account Voting Power in %
print("Voting Power:","{:.2%}".format(json_data['result'][0]['voting_power'] / 10000))

# Print the account progress in % to Orca status (100 Million VESTS)
print("Progress to Orca:","{:.12%}".format(float(json_data['result'][0]['vesting_shares'].split()[0]) / 100000000))

The result 🙃😎

HIVE Balance: 41.460 HIVE
Voting Power: 91.44%
Progress to Orca: 63.878949907716%

#HIVEGames

This hashtag is valid both on Hive and Twitter. So, if you have an account on either and you are up to post about dCity, make sure you use both #dCity and #HIVEGames. If you wish, also tag me on Twitter, the account I use there is mentioned bellow.

(credit @doze)

Supporting this initiative will be a tremendous help to the #HIVE ecosystem, especially if your area is into the #Blockchain #Games world. For more details of how this started, check the side image that links to the post.

Any questions, advice requests or comments are welcome, either here or anywhere else you find me.

🖐️HIVE5's!

Have fun, good luck on all the gaming and lots of teaching "how to play" to new players.

Do you know about my new Twitter account?

If you have an account on Twitter (follow @forkyishere) and you enjoy #news about the #HIVE blockchain, make sure you tweet about any reference to your account here. I am following all HIVE users!


Check these Cryptocurrency 💳Cards

by @forykw

Sort:  

Hi @forykw, I just came across this post of yours, and I thought it might be able to help me get started with using the HIVE API from the web browser address bar, as a stepping-stone towards learning how to do the same from within gsheets_18px.png Google Sheets. Can you format the "main part of the code" here, as a plain HTML query, which can be issued directly from the web browser's address bar? I think that would help me hugely with understanding how this is supposed to work. Thanks in advance, for this.

I think there is a way to do it, yes, but it's quite messy and erroneous in my view. You would be better off using curl or something like https://www.postman.com or similar webtools that provide you with all sorts of support against common API's, using whatever codes, JSON, HTML, etc...

But if the intention is to run from Google Sheets for any other reason, I think they have extensions and even scripting plugins that allow you to run from within the Sheets document.

I just noticed the link above for the https://developers.hive.io/apidefinitions changed (no more "docs" separator, so I am going to fix that), but you should have curl examples in there.

You can also get (download) the cli_wallet binary from HIVE repo and play with the wallet directly. In that case you just need to import private keys into the wallet and then you can use the same curl commands to interrogate any HIVE API.

If my answer is shooting somewhere sideways, let me know more. Happy to help.

Thanks for the reply on this.

"But if the intention is to run from Google Sheets for any other reason, I think they have extensions and even scripting plugins that allow you to run from within the Sheets document."

I plan to use gsheets_18px.png Google Sheets so that I can also make use of other built-in functions, such as the =TREND() function in GSheets. To do this, I need to know how to sew together the various parts of your example API call, into a single URI, which I can paste into my web broswer's address bar, and which will return a JSON object from the HIVE API.

As an example of what I have achieved in this space previously, have a look at this how-to guide that I published three years ago, when I was last on the social blockchain. I hope to use the same techniques for leveraging the main HIVE API this time around.

Yeah... in that case it's the "𝙸𝚖𝚙𝚘𝚛𝚝𝙹𝚂𝙾𝙽" function that was doing the same as curl for example. Might even work with that... give it a try.

Quickly googled, found some that might be what you are looking...

Found this method also interesting...

Cheers

I've finally found an API endpoint that I can leverage, but it is quite limited in its scope. Check out https://hive.blog/@frittro.json which allows me to pull in my own user / account data. Replacing the username, obviously, allows you to poll anyone's user data on the blockchain.

Humm... I didn't know the condenser did have that option. Interesting. Just tested on other front ends and not everyone implements it. But cheers for sharing it.

Thanks for using Ecency!
Your post has been voted as a part of Encouragement program. Keep up the good work!
Dear readers, follow and support this author, Install Android: https://android.ecency.com, iOS: https://ios.ecency.com mobile app or desktop app for Windows, Mac, Linux: https://desktop.ecency.com
Learn more: https://ecency.com
Join our discord: https://discord.me/ecency