A python script that streams the new blocks in JSON format - and how to find full documentation for the python steem library

in #steem7 years ago (edited)

I


had been having a bit of trouble trying to get the python steem library to work, but I have made a breakthrough. I have also learned a couple of neat python tricks for finding out things about libraries you have imported what is inside them and even their documentation (it even is displayed in a pager so you can browse it).

Here is the code that will print out the data of each new block as it comes in:

#!/usr/bin/python3
import steem
from pprint import pprint

st = steem.blockchain.Blockchain ( )

for a in st.blocks():
    pprint(a)

You can browse the help within the python shell like so:

 ✘ loki@vaioe  ~  python3
Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import steem
>>> help (steem)

>>> help (steem.blockchain)

In the first you see a list of all the submodules, and then you can add that with a '.' between and get more info.

Update:

This was what I am working towards, now I can grab the current data, so I can process it and generate something from it:

#!/usr/bin/python3
import requests
import json

r = requests.post ( 'https://node.steem.ws', data = '{"id":8,"method":"get_witnesses_by_vote","params":["","100"]}' )
output = json.loads ( r.text )
print ( output )

The output is a bit different to using curl to make the same request:

curl https://node.steem.ws --data '{"id":8,"method":"get_witnesses_by_vote","params":["","100"]}'

But the content is the same otherwise.

This particular function is missing from @xeroc's steem (formerly piston) library, this is why I have had to learn how to write the requests. This site helped a lot:

http://steemroll.io/api-explorer/#method=get_witnesses_by_vote&params=["","100"]

Yes, that is the same query but through the api explorer. Nice huh?

😎


We can't code here! This is Whale country!

Vote #1 l0k1

Go to steemit.com/~witnesses to cast your vote by typing l0k1 into the text entry at the bottom of the leaderboard.

(note, my username is spelled El Zero Kay One or Lima Zero Kilo One, all lower case)

Sort:  

Nifty! :) I just saw the We can't code here thing and I had to agree completely. :)

I will be publishing soon the info on how to bypass piston because it is missing some vital bit of code that stops it running (and you can't set default_author with it). I dug back into my old posts and found that I had already partly worked out this problem and I have gone further now, setting a specified RPC node to connect to. The code looks like this:

 st = Steem ( wif = 'aoeuaoeuaoeuaoeuaoeuaoeuaoeuaoeuaoeuaoeuaoeuaoeuaoe', node = 'wss://node.steem.ws')

This sets the account that it requires to have at least one set, that lets you do broadcasts (so you could use a posting key here rather than master or active, possibly even memo only for read only functions). I think it needs this for the custom_json function in Steem, I am writing a script to grab the data of the active witnesses so I can process the data in the output.

Hmmm, I don't think it's because of Piston, in this case. If different nodes are allowing you to do different things, it must be from the nodes themselves, not from Piston.

My guess is that node.steem.ws runs a broader set of APIs, possibly some related to accounts (don't have time to dig to see which plugins are related to accounts, but I know it was a discussion on github about this). That means you can set up your own Steem seed node, enable the plugins related to accounts, and interact with it instead of sending transactions to node.steem.ws (and increasing the load on it). If you configure your seed node properly it will take care of all the necessary operations, including broadcasting transactions, etc.

Hope it helps.

Yes, I have my own RPC and just finished pulling down from my server a snapshot that I will also be able to run on my laptop and when offline still test code that talks to it.

No, there seems to be a bug in a fresh clean install of python with the steem library... the names are confusing me. It is called 'steem' now, just steem: pip install steem. But piston does not work saying it is missing an import of some exception definition.

I am writing code for automating blockchain related things so the model with the piston wallet doesn't really suit my purposes, I have to create instances of the Steem class that define the RPC node and a key for something. I am not sure what to use but it accepts my memo key.

I actually just discovered I can pull the data I am looking for with a simple curl command:

curl https://node.steem.ws --data '{"id":8,"method":"get_witnesses_by_vote","params":["","100"]}'

This spits forth the entire data for all 100 current witnesses in JSON format. I think the same parameter can be used with python requests library as well.

I think piston library was renamed as pysteem. http://pysteem.com. Piston, as a separate project, seems to be a set of tools for pysteem. I'm using pysteem on a Steem reated projects and it works out of the box (sometimes it's broken, I admit it, but now seems to be one of those times where it seems to just work).

pip3 install pysteem, or just read the docs at the site above.

The library originally was part of piston, then became pysteem. However, due to trademakr limitations, I have just recently moved over to piston-lib which will be a distinct branding
https://steemit.com/chainsquad/@chainsquad/python-steem-rebranding-to-piston-libpiston-cli

✘ loki@vaioe  ~  sudo -H pip3 install pysteem
Collecting pysteem
Could not find a version that satisfies the requirement pysteem (from versions: )
No matching distribution found for pysteem

it is called piston-lib possibly. Or something. I've poked around and I have piston now but it is flagging another error:

  File "/usr/local/lib/python3.4/dist-packages/piston/steem.py", line 2, in <module>
    from steem.steem import Steem as SteemSteem
ImportError: cannot import name 'Steem'

steem is installed and I can use it in scripts like the ones above but I am just going to stick with requests because the function I require is missing from @xeroc's library.

I have seen that error aswell, but it should be fixed by now.
Make sure follow the instructions here:

@l0k1

Coin Marketplace

STEEM 0.16
TRX 0.16
JST 0.030
BTC 59228.82
ETH 2524.07
USDT 1.00
SBD 2.52