Hacking Steem with Python in the Cloud

in #python7 years ago (edited)

Like the ocean...

One of the coolest (and hardest to comprehend) things about Steem is that it is an ecosystem. There are tons of different tools and platforms that all plug into the same fundamental features offered by the blockchain. Anyone can communicate with the network's public nodes or run their own, to access posts, make votes, transfer Steem/SBD, etc., all without using a front end like Steemit.

DeepinScreenshot_select-area_20180206193121.png

This enables projects like Busy, ChainBB, Dtube, and Utopian, just to name a few. Learning about new projects like SteemPress through the Utopian feed got me inspired to try writing a bit of code that interfaces with Steem. Getting started involved a few challenges, so I decided to write this guide to show how to get started programming Steem with nothing more than a web browser and some free tools.

Python

Python is my weapon of choice for programming. If you're thinking about learning, it's a great place to start. If you already know some, this should be easy to follow along with. Just about everything has a library for Python (to be specific, we'll be talking about Python 3 throughout), and Steem is no exception. With the library installed, we can use Python to run commands that interact with the blockchain via Steem's REST API. Don't freak out if things are getting a little technical here, we'll do this all in a web browser with step by step instructions.

Codenvy

Codenvy is a cloud based development platform that offers a nice free plan. Creating an account is simple—go ahead and make one now. We'll need a workspace to hold our files and provide the underlying operating system to run Python on. Go to your dashboard on Codenvy and select the "Create Workspace" button. Then select "Single Machine," scroll down the list and select "Debian," then press the green "Create" button.

DeepinScreenshot_select-area_20180211155006.png

Install

Now we need to install Python and steem-python. We'll use Miniconda to provide a complete and up to date Python installation, then use pip to install steem-python.

Miniconda

Once your workspace has loaded up in Codenvy, you should have a terminal window at the bottow. Run the following commands in the terminal to download and run the Miniconda installer:

wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
chmod u+x Miniconda3-latest-Linux-x86_64.sh
./Miniconda3-latest-Linux-x86_64.sh

Scrypt Dependencies

Scrypt is one of the libraries that steem-python depends on. It in turn has a couple dependencies we'll need to install with Debian's package manager, apt-get:

sudo apt-get install build-essential libssl-dev

steem-python

Finally, we can install steem-python using pip:

pip install steem

Patch METADATA

There's a little hickup in steem-python to do with the version number of one of its dependencies, toml. To see the problem in action, try running the command line tool, steempy:

DeepinScreenshot_select-area_20180211165234.png

To work around this, we just need to make a small change to steem-python's METADATA file. I've uploaded a patched version to Github to make things easy. Clone the repository, back up your METADATA file, then copy the new one over the old, by running the following commands in the terminal:

cd /projects
git clone https://github.com/scottyeager/steem-python-setup.git
mv /home/user/miniconda3/lib/python3.6/site-packages/steem-0.18.103.dist-info/METADATA /home/user/miniconda3/lib/python3.6/site-packages/steem-0.18.103.dist-info/METADATA.bak
cp /projects/steem-python-setup/METADATA /home/user/miniconda3/lib/python3.6/site-packages/steem-0.18.103.dist-info/METADATA



Note that this may not be necessary, if steem-python is updated at some point in the future. Try running steempy to see if there's a problem.

Set Node

The default nodes inlcuded with steem-python are no longer active. You can find the closest public nodes using geo.steem.pl. Then use steempy to set the node of your choice. For example, the Steemit API node over https:

steempy set node https://api.steemit.com:443

Have some fun

Now we can launch python and play with the library a bit.

python
>>> from steem import Steem
>>> s = Steem()
>>> s.get_account('ned')['sbd_balance']
'5750.351 SBD'

>>> s.get_feed_entries('ned', 0, 5)
[{'author': 'ezzy', 'permlink': 'adventures-of-a-young-stallion-not', 'reblog_by': [], 'reblog_o
tside-my-kitchen-window', 'reblog_by': [], 'reblog_on': '1970-01-01T00:00:00', 'entry_id': 34072
 {'author': 'dreamrafa', 'permlink': 'concurso-disena-mi-logo-semana-3-desing-my-logo-contest',
blog_by': [], 'reblog_on': '1970-01-01T00:00:00', 'entry_id': 34069}]



I hope that's sparked your imagination a bit about how it's possible to interact with the Steem blockchain using code. There's much more we could do, including setting up our account to do voting and other actions that require authentication. Check out the documentation for steem-python to get a complete list of commands and much more information about the library.

The goal of this guide was to make it possible for anyone to start toying with a little bit of Python to interact with Steem. How did it go? Is anything broken or confusing? Let me know in the comments.

Sort:  

This post received a 2.100 SBD (60.77%) upvote from @upvotewhale thanks to @scottyeager! For more information, check out my profile!

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by scott from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows. Please find us at the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.