Twisted based asynchonous python steem API.
This project aims to implement an asynchronous python library for the STEEM JSON-RPC API.
Eventually asyncsteem aims to become a complete asynchronous alternative to steem-python that runs under both Python 2 and Python 3.
The current beta however runs only on Python 2 and does not yet implement cryptographic signing operations that would be needed to, post, vote, etc. There curently is a Python 3 port of this Beta written by @scottyeager.
The asyncsteem library is a asynchonous library, designed to woth with the defacto Python asynchonous networking framework twisted.
If you wish to stay informed on my progress on this library, please follow @mattockfs on steemit or join this discord server. I'll try to blog regularily on my progress. 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 featyres or bug fixes.
Please note that there currently is no install script or pip install available yet, the code is currently in early beta, and ease of install is something I havent gotten to yet at the moment.
In order to use the asyncsteem library, you should define a Python class that implements one or more of the following methods:
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:
An example :
class DemoBot: 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" bot = DemoBot()
In order to use the bot, you will need to use Twisted:
from twisted.internet import reactor ... blockchain = ActiveBlockChain(reactor) blockchain.register_bot(bot,"demobot") reactor.run()
This will let the bot start listening to new events from the STEEM blockchain.
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)
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:
You will need to look elsewhere for now for documentation on the above API calls. When unsure about the exact syntax, please make sure to use an on_error callback. Often the error messages can provide some info on correct usage.
As stated, the library is in early beta now. I would very much appreciate any feedback on my work so far, so please test out what there is and let me know what needs improvement.