Learning Python with @felixxx #1: Setup and First Script

in #steem-python6 years ago (edited)

Steem-Python is the official Python library for STEEM.

https://github.com/steemit/steem-python

In this tutorial series I would like to introduce you to Python and Steem-Python at the same time.

This part of the tutorial deals with setting up the environment and the Python library.
Steem-Python also comes with a wallet called steempy.
If you only want to set up the wallet, you can also use this guide and leave out the second step.

I am not a good programmer.
If you want to learn good Python, you should use a different guide.
This will be an easy to follow, practical guide to use Python with STEEM.
If you know what functions and objects are, you should be able to follow this guide.
If you have no prior knowledge about programming at all, I would recommend you spend an afternoon or so with a Python beginners tutorial and return here.


Note:

The library is under constant development, while I will not be able to update this post.
If anything changes in the library, I will have to write a new post, or comment on this one.


1. Environment

First, we need an environment to develop in.
I choose Ubuntu 17.10 because it is easy to install Steem-Python on it.
If you already have an Ubuntu running, you can skip this part.

I assume, you are on Windows.
If you are on iOS: your problem.

Simply download a copy of Ubuntu 17.10 here:

https://www.ubuntu.com/desktop/1710

Then download VMWare Player here:

https://my.vmware.com/en/web/vmware/free#desktop_end_user_computing/vmware_workstation_player/14_0

Start VMWare Player and set up a new virtual machine with Ubuntu 17.10 on it.
You need very little RAM for this machine. 2 GB is plenty.

I assume you can handle this step. Search the web for help, if you have to.

Once Ubuntu 17.10 is up and running, log in.

This is much like Windows, so I assume you can deal with it ...

Set the timezone to your local time, or to UTC ( the timezone the STEEM blockchain uses ).
Set your keyboard layout to your language, if you have to.

Familiarize yourself with Ubuntu a little, if you have never used it before.

With ctrl + T you can open a new terminal window.

It should look something like this:

felixxx@ubuntu:~$


Note:

The Windows commands ctrl + C and ctrl + V do not work in the terminal.
Use right mouse-button + paste instead.


Type ( or paste :P ):

sudo apt-get update

It will update your Ubuntu.

sudo apt install python3-pip
pip3 install setuptools
pip3 install scrypt
pip3 install wheel
pip3 install pytest
sudo apt-get install libssl-dev
pip3 install steem

Will install Steem-Python and all the stuff it needs to run.

Congratulations !

You have successfully set up your development environment.

2. First Script

Since this is a hands-on guide and I want you to have instant results, we will just skip ahead and try the first script.
I found a nice little example in the documentation ...
http://steem.readthedocs.io/en/latest/

In the terminal, open a new file:

gedit myfirstscript.py

gedit is a text editor.
You can use any other editor.
It will create a file called myfirstscript.py in the directory you are currently in.
You can navigate directories with cd - search the web if you have to.

For the first script, I will take some bits from the 'Simple Voting Bot' example.
http://steem.readthedocs.io/en/latest/examples.html ( bottom of the page ).

Copy these 7 lines of code into the editor-window:

from steem.blockchain import Blockchain
from steem.post import Post

b = Blockchain()
s = map(Post, b.stream(filter_by=['comment']))

for post in s:

    if post.is_main_post():

        print('https://steemit.com/tag/@' + post['author'] + '/' + post['permlink'])

To see an instant result, save the file and close gedit.
Back in the terminal, type:

python3 myfirstscript.py

Voila !

You should see something like this ( slowly building up ) :

The code contains an open loop.
Press ctrl + c to stop it.


Breakdown:

from steem.blockchain import Blockchain
from steem.post import Post

It will import the Blockchain object from the blockchain module of the steem library.
( Guess what the second line does ... )

b = Blockchain()

Will initialize 'b' as a Blockchain() object.
The blockchain module has a stream-function, which allows us to 'stream' block-wise through the chain or go through its history.

s = map(Post, b.stream(filter_by=['comment']))

Will initialize a stream called 's'.


Note:

This map move is pretty cool, as it will create a Post() object from every item that goes through our stream.
Don't worry, if you don't understand it right now.
This code doesn't work very well; Every comment forces a new API call to the node, which takes capacities off the node and costs you time, also.
Your script will not be able to keep up with the blockchain usually and crash after a while with an exception.
I will get to this in part 2 ...

Also note, that on blockchain level, there is no real difference between a comment and a post.
You will see a lot of different names for it.
Comments have a 'parent_post' and posts ( also: 'main-posts' or 'root-posts' ) do not.


Since this is just a quick example for testing purposes, we will keep that map-trick.

Now all that is left is to set up a loop through all items in the stream and then do something.
How about this:

for post in s:

    if post.is_main_post():

        print('https://steemit.com/tag/@' + post['author'] + '/' + post['permlink'])

For every post in the stream 's', which is also a main-post, this will construct the weblink to the post on steemit.com.
is_main_post() is a handy function from the post-module, which I think simply tests if the post has a parent, or not. I was too lazy to look that up.
The same module also has a 'construct_weblink'-function or something similar, which does exactly the same as the 3rd line of code above.

This was just a quick example to get you going.

I hope it works for you.


To be continued ...

You can now play around with the examples.

This tutorial series will probably work best, if I get some feedback.
Please feel free to comment if you have any questions or scripts you would like to see here.

Please do not use this knowledge to make annoying spam-bots !

It will not pay off anyways.

Peace,

@felixxx

Sort:  

4-23 I kept getting

error: Setup script exited with error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

when trying to update

pip3 install wheel

Was able to install the wheel after typing

sudo apt-get install libssl-dev

and it worked.

brb, making annoying spam-bots

I appreciate you teaching us the foundations of steem-python. Subscribed for more!

The first reply was gold :D

Since steemit.com disabled their legacy steemd node (https://steemd.steemit.com), your "firstscript.py" is not longer working.

I made an update of your tutorial https://steemit.com/steem/@botsultant/let-s-build-bots-for-steem-part-03-install-steem-python-on-ubuntu-17-10

Steem-Python has been updated and the default nodes have been changed.
Also, a bug that kept the steempy from switching to the next node on the list in case of bad requests was fixed.

As of now, the script above should work again, with a fresh Steem-Python.

Hey Felix, hatte nicht die Zeit aber wollte dein Tutorial mal ausführen. Sehe ich das richtig, dass ich bei diesem Test schon mit der live Blockchain interagiere?

Ist live, ja.
Du liest mit diesem Test aber nur, also die 'Interaktion' hält sich in Grenzen.

achso, ich dachte beim überfliegen dass man da direkt voted. Ich schmeiß gleich mal meine Ubuntu Partition an.

Muss 17.10 sein, bzw was mit Python 3.6 (or higher).

Jupp, habs gerade mit 14.02 probiert und das klappt schonmal nicht :D. Ich bin erstmal n bisschen unterwegs und mach mir heut abend mal die neue Version von Ubuntu klar. Meine Ubuntu hat so wie so Probleme, startet Chrome net und zeigt keine Bilder im Firefox. Keine Ahnung was ich mit dem Ding gemacht hab dass es so vermurkst ist :D.

If I use like this it will extract the tag from the post?

print('https://steemit.com/' + post['tag'] +'/@' + post['author'] + '/' + post['permlink']

No.

print('https://steemit.com/' + post.category + '/@' + post['author'] + '/' + post['permlink'])

this will.

Nice advocacy..... am still battling to learn HTML and CSS for now...lol
keep the good work.

This is easier than HTML and CSS :)

I really like your approach of sharing knowledge and educating people. Another reason to vote for you being a witness ;-)

This is a great guide to get you going. There are so many useful things you can do when you unleash the power of python on the Steem Blockchain.

Its is perfect for apply in mi proyect connect extension

Any chance I could talk you into getting us on steemit a reddit bot that replies to people to move to steemit.com - would be a great campaign to raise awareness of the platform en mass,Let's take it down

I do not understand, sorry.

Coin Marketplace

STEEM 0.31
TRX 0.11
JST 0.033
BTC 64550.89
ETH 3156.32
USDT 1.00
SBD 4.30