Twisted based asynchonous python STEEM JSON-RPC API.
This project is a Python 3 port of asyncsteem, which aims to implement a complete asynchronous alternative to steem-python. It is designed to work with the defacto Python asynchonous networking framework twisted. Twisted is the only dependency for asyncsteem3.
The current beta does not yet implement cryptographic signing operations that would be needed to, post, vote, etc. However, it is easy to create bots for collecting stats or dispatching alerts by monitoring the blockchain with minimal boilerplate code.
If you wish to stay informed on the progress of asyncsteem, please follow @mattockfs on steemit or join this discord server. Donations for this project in the form of STEEM or in the form of steemit post upvotes are very much welcomed, as are pull requests with features or bug fixes.
A setup.py file is included, if you wish to install the package.
python setup.py install
See examples/print_votes.py for a simple bot that spits out info about votes happening on the Steem blockchain.
class DemoBot(object): def vote(self,tm,vote_event,client): w = vote_event["weight"] if w > 0: print("Vote by",vote_event["voter"],"for",vote_event["author"]) else: if w < 0: print("Downvote by",vote_event["voter"],"for",vote_event["author"]) else: print("(Down)vote by",vote_event["voter"],"for",vote_event["author"],"CANCELED")
A bot class can implement one or more of the following methods, which are called whenever a new event of that type arrives:
These methods map one on one to the operation types that are found on the blockchain. For convenience, the following methods may also be implemented:
You may also instead opt to pick a day in the past where the bot should start streaming. This could come in handy if you want to test your code, or if you want to limit your bot's online time.
blockchain = ActiveBlockChain(reactor,rewind_days=7)
Other API Methods
While the core of the library is aimed at streaming operations from the blockchain, it is likely your bot will need to query other JSON-RPC API's as well. For this, the client argument of the bots methods provides the entry point. But note, the API is asynchonous and works through a command queue and a client pool. Let us zoom in a bit on how to use the client argument in our code.
def vote(self,tm,vote_event,client): def process_vote_content(event, client): for vote in event["active_votes"]: if vote["voter"] == vote_event["voter"] and vote["rshares"] != 0: print( vote["time"],vote["voter"],"=>",vote_event["author"],vote["rshares"]) opp = client.get_content(vote_event["author"],vote_event["permlink"]) opp.on_result(process_vote_content)
Basically, you define a closure for handling the additional API query, you put the command you wish to invoke on the asynchonous command queue and then you bind the result to your closure that will be invoked asynchonously when the command on the queue has been handled by the HTTPS client pool.
In some cases, a JSON-RPC call may return an error for your command. You may create an other callback for the error situation:
def err_handler(errno, msg, rpcclient): print("OOPS:",msg,"(",errno,")") opp.on_error(err_handler)
You've seen the example using get_content, this is one of a wide range of JSON-RPC API calls available through the API. The API is fully transperant, so any silly typo you make will result in a bogus JSON-RPC call to one of the STEEM API nodes. For convenience, here is a list of currently commonly available valid API method names:
Documentation for steemd API calls can be found on GitHub. Often the error messages can provide some info on correct usage—use an on_error callback to print the error.
Both asyncsteem and asyncsteem3 are beta releases. Please report any issues with asyncsteem3 to its own GitHub repository so they can be addressed or forwarded to asyncsteem as appropriate.