Using steemd directly from Python in 60 seconds.steemCreated with Sketch.

in #python8 years ago (edited)

Setup

pip3 install --user sanelogging requests

Example Code

#!/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)
Sort:  

Did you forget about #programming tag? :)

Such good stuff, should be tagged with it for sure! :)

I did, oops.

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.029
BTC 65292.16
ETH 2651.21
USDT 1.00
SBD 2.85