Created
February 15, 2017 02:56
-
-
Save sneak/a8da0557bed67f51a044240036b46c60 to your computer and use it in GitHub Desktop.
simple steemd client in Python3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from pprint import pprint | |
from sanelogging import log | |
from uuid import uuid4 as uuid | |
import json | |
import requests | |
import sys | |
import time | |
class steemapi(object): | |
def __init__(self, url='https://steemd.steemitdev.com'): | |
self.url = url | |
self.headers = {'content-type': 'application/json'} | |
def listApiMethods(self): | |
r = self.call('help') | |
return list(r['error']['data']['stack'][0]['data']['api'].keys()) | |
def call(self, method, args=[]): | |
# send only an integer for steemd's delicate sensibilities | |
id = int(str(uuid().int)[:12]) | |
payload = json.dumps({ | |
"method": method, | |
"params": [args], | |
"jsonrpc": "2.0", | |
"id": id, | |
}) | |
t1 = None | |
t2 = None | |
try: | |
t1 = time.clock() | |
resp = requests.post( | |
self.url, | |
data=payload, | |
headers=self.headers | |
) | |
t2 = time.clock() | |
resp = resp.json() | |
except(json.decoder.JSONDecodeError): | |
# this is a thing. | |
# see https://github.com/steemit/steem/issues/868 | |
resp = { | |
"jsonrpc": "2.0", | |
"id": id, | |
"error": { | |
"code": -32603, | |
"message": "service returned invalid json" | |
} | |
} | |
if t2 and ('error' not in resp): | |
msec = float((t2-t1)*1000) | |
formatted = "%.2f" % msec | |
resp['timing'] = float(formatted) | |
log.debug('TIMING %s %.2fms' % (method, resp['timing'])) | |
return resp | |
def main(args): | |
a = steemapi() | |
pprint(a.listApiMethods()) | |
pprint(a.call('get_config')) | |
pprint(a.call('get_dynamic_global_properties')) | |
pprint(a.call('get_accounts', ['sneak'])) | |
if __name__ == "__main__": | |
main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment