Creating Your First Program On The Steemit Network Using Steem-Python #BeginnerMode - TUTORIAL

in #steemdev8 years ago (edited)

A few days ago I made a little cryptoBot application using the steem-python library which I shared with the community. You can check it out here . I got some good feedback on the post and some requests from the minnow support crowd to make a tutorial on setting yourself up to create similar programs so I decided to write this tutorial where I will be taking you through the entire setup up until where we will be playing around with the steem-python library in order to teach you the basics and have you create your own awesome programs built on the Steemit network.

wallie.jpg

My Assumptions

  1. You have basic knowledge of Python

  2. You are running a version of Ubuntu. I'm currently using Ubuntu 16.04

  3. Your distribution is up to date

What I Will Not Be Doing In This Tutorial

  1. Teach you Python

  2. Go over every method in the steem-python library

This tutorial will be broken up into 2 Parts. In Part 1 we'll be setting up our Python development environment and in Part 2 we'll be playing around with the steem-python library. If you're already familiar with setting up and working with Python environments you can skip to Part 2.

Part 1 - Setting up our environment

In order to use steem-python we need to have at least Python version 3.5 or higher. We can check our version by running the following command

$ python3 -V

Which should give you an output similar to this:

$ Python 3.5.2

Your version number might be different but as long as we have version 3.5 or higher we're good to go. If you get an error or your version is lower than 3.5 then you'll need to get the correct version first before continuing.

Next we'll be installing pip which we'll use later to install the steem-python library.

$ apt-get install -y python3-pip

The -y flag just means that we automatically agree to all the things that need to be installed. Pip can be used to install packages or libraries with the following command:

$ pip3 install package_name

Now we need to install some extra packages in order to create a nice robust programming environment:

$ sudo apt-get install build-essential libssl-dev libffi-dev python-dev

Now we'll be setting up our virtual environment where we'll be playing around with the steem-python library. A virtual environment will allow us to develop our programs in an isolated safe space where we can play around in and brake things without breaking the things outside of the environment. You can have as many virtual environments as you want. We can install the virtual environment module with the following command:

$ sudo apt-get install -y python3-venv

Now that we have the module installed we can start creating our virtual environment. Navigate to the folder where you would like to keep all your environments, I'll be keeping mine in my Documents folder so I'll be navigating there with the following command:

$ cd Documents/

Now we'll create our folder where we'll be keeping our virtual environments. I'll be naming mine "environments" but you can name it whatever you want, just remember what you named it:

$ mkdir environments

Now we need to navigate into our created folder:

$ cd environments

Now we can create our virtual environment in here with the following command. I'll be naming mine "steemit_env":

$ python3 -m venv steemit_env

Now that we have created our virtual environment we can run the following command to get the contents of our environment:

$ ls steemit_env

and we should get the following output:

bin include lib lib64 pyvenv.cfg share

In order to use our environment we need to activate it first so that Python knows which environment we're using:

$ source steemit_env/bin/activate

your command line should look similar to this after activating your environment:

(steemit_env) root@BennieBanana-TheBestLaptop:~/Documents/environments# 

Sweet! Our virtual environment is set up and activated so we can start playing around. The prefix "(steemit_env)" means that we're currently using our created virtual environment. Lets create a little demo program to test out our virtual environment. I'll be using Notepadqq which you can get by running the following commands:

$ sudo add-apt-repository ppa:notepadqq-team/notepadqq
$ sudo apt-get update
$ sudo apt-get install notepadqq

or you can use gedit or nano which comes packed with Ubuntu by default. Open up a new python file in your editor of choice by running the following command. I'll be calling mine "steemit.py":

$ notepaddqq steemit.py

Now that we have our file opened up we can start coding. Add in the following line to the file:

print("Steemit is Awesome!")

Save it and close it. We can now check if everything is working properly by running our program with the following command:

$ python steemit.py

and we should get the following output:

$ Steemit is Awesome!


Awesome, now we can start with part 2 where we'll be installing the steem-python library and start playing around with it.

Part 2 - Playing with Steem-Python

Lets install the steem-python library using pip:

$ pip install steem

Once the library is installed we can create a new file where we will be playing. I'll be opening the previous file we created and I'll delete the print statement so that we can have a blank file to work with:

$ notepadqq steemit.py

The first line we need to add to our file will be our import statement:

from steem import Steem

The second line we need to add is our instance of Steem where we will be adding in our private posting key as well as our private active keys:

s = Steem(keys='your-private-posting-key', 'your-private-active-key')

You can get your private posting and active keys by going to your Wallet on Steemit, clicking on Permissions and then clicking on "SHOW PRIVATE KEY" next to your posting and active keys.

keys.png

Now we can start doing cool stuff with the API. Our first little experiment will be to upvote this post by adding the following lines to our file:

s.vote('@benniebanana/creating-your-first-program-on-the-steemit-network-using-steem-python-beginnermode-tutorial', 100.0)
print('Yay we just upvoted Bennies post!')

Your file should look this by now:

lastTry.png

The first argument is the posts identifier and the second is the weight at which we would like to vote. Save the file, exit and run the program with the following command:

$ python steemit.py

we should get the following output:

$ Yay we just upvoted Bennies post!

Well done! You just wrote your first little program using the steem-python library!

Our next little experiment will be to resteem this post. First we have to remove or comment out the s.vote... statement since we can't upvote the same post twice. We'll comment out the vote and print statement and replace them with the following:

s.resteem('@benniebanana/creating-your-first-program-on-the-steemit-network-using-steem-python-beginnermode-tutorial')
print('Yay we just resteemed Bennies post!') 

Save, exit and run your file like you did previously and you should get the following output:

Yay we just resteemed Bennies post!

These are just 2 small examples of what you can do with the steem-python library. The library allows you to do lots of other cool stuff such as writing posts, commenting on posts, replying to comments, getting profile information and lots and lots of other cool stuff. To see all the cool stuff you can do with the library you can visit their page and read the documentation here.

Here's a reference to the original setup post I used.


If you enjoyed this tutorial and found it helpful be sure to upvote and resteem as well as follow me if you'd like to see more. Please let me know in the comments what you think and if I might have missed something or if I should've added something.

Enjoy And Thanks For The Support!

Sort:  

Very nice thanks. With this and your latest post I am looking forward to making my own programs & bots :)

Awesome, would love to check them out when you're done

Upped. Thanks. Where's the best place for a guy who had a half semester of Turbo Pascal go to learn the basics of all of these new languages? Python, Ruby? Help guide me!

Codecademy is definitely a great place to start as well as TutorialsPoint

I'll be trying to make sense of this, this week and then Rip your bot's beating me ;) haha

i know nothing about programming, but just dropping by to say hi :)

#minnowsupport

haha thanks

Not ready to go through this myself as I'm a Python newb but looks like a great tutorial, keen to see more code tuts on here especially for beginners and Steem related! Nice one mate!

Thank you for the feedback, glad I could provide something of value. I'll be doing more of these tutorials in the future after seeing the response to my posts so keep an eye on my blog for more in the future.

Congratulations @benniebanana! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes

Click on any badge to view your own Board of Honnor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

This is awesome :D

Following you now. +1 for the Supa Hot fire gif at the end :P

haha thanks, Supa Hot Fire is the boss

Hey @benniebanana, there's a typo in your code within the post (not the code that's in the image, fortunately) -- you need "[" and "]" around the keys themselves, so it should read:

s = Steem(keys=['your-private-posting-key', 'your-private-active-key'])

instead of:
s = Steem(keys='your-private-posting-key', 'your-private-active-key')

Thanks again for your help! I'm slowly making progress. :)

Once I got past that, it gave this error:

ValueError: You need to provide a voter account

I checked back all the steps above, in this post, to ensure I didn't make a mistake. Looks like I'm going to need to read the documentation... :)

Did you ever get this figured out? Thats the step I am stuck on!

Yeah, but I've forgotten most of it (still recovering from multiple concussions, although doing better lately). Plus I reinstalled the tower I had it on, so it's gone.

Thanks for pointing it out. When I created this I was still in the learning phase so mistakes were inevitable haha. Think I'll be doing an updated one today. Will tag you when I'm done.

Cool thanks!

You are welcome Master.

Am I talking to two different people, or merely two different accounts? :)

Um no I'm only talkong to you.

ValueError: You need to provide a voter account

Solution:

vote(self, identifier, weight, account=None):

Vote function requires the account name, so modify the code as shown below,

s.vote('@benniebanana/creating-your-first-program-on-the-steemit-network-using-steem-python-beginnermode-tutorial', 100.0,'xyz')

where 'xyz' is the voter's account name.

Is this code still viable? For some reason
s = Steem(keys=['####', '####'])
doesn't want to work. It caused a couple of exceptions on my machine.

steem modual install for you ?
I have installed python 3.6
I get this:

Using cached steem-0.18.103.tar.gz
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "string", line 1, in module
File "/tmp/pip-build-p7hg6puo/steem/setup.py", line 10, in module
assert sys.version_info[0] == package_meta.required_python_major and sys.version_info[1] >= package_meta.required_python_minor, "%s requires Python %s or newer" % (package_meta.package_name, package_meta.required_python_ver)
AssertionError: steem requires Python 3.6 or newer

----------------------------------------

Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-p7hg6puo/steem/

this error
File "steemit.py", line 4, in module
s.resteem('@benniebanana/creating-your-first-program-on-the-steemit-network-using-steem-python-beginnermode-tutorial')
File "/home/telesti/.local/lib/python3.5/site-packages/steem/commit.py", line 1203, in resteem
raise ValueError("You need to provide an account")
ValueError: You need to provide an account

Good day. Yeah I should've updated this post long ago so my apologies. I've been inactive for a while now due to a new job I recently got.

damn getting tired of chasing these smoke and mirror scripts, did it ever work?