This is part of a series of tutorials.
The goal is to produce a hands-on guide for people who want to jump straight into developing tools for STEEM.
This part will cover two loosely related topics at the same time and I will also briefly introduce some new concepts. This part will not be as clean as the beginning and I will skip over a few explanations.
I do this as an attempt to quickly enable anybody with basic Python skills to set up a tool for STEEM that anybody with a browser can use.
Running your own scripts is fun, but most Steemians do not use a Python interpreter.
Also, some scripts require to run 24/7 and running them in a browser might not be the most convenient solution then; You will need a server.
I am running my bots on a second hand lenovo netbook at home. You could run them on an old PC, a smartphone, a raspberry Pi or a rented server. I would recommend the laptop.
They are designed to run energy-efficiently and they come with a screen and a keyboard. I take the batteries out, but with batteries they even have an uninterruptible power supply (UPS).
Once a script is thoroughly tested and stable, I can move it to a server in a 'real' data center later.
Depending on your tool, you might want the user, admin or yourself to somehow interact with it, while it is running.
There are endless methods on how to set this up, from a simple terminal to a 3D VR type of GUI.
I found the easiest method at the moment to be a Discord bot. Discord runs in browsers, is easily accessible for almost anybody on a computer or smartphone and its Python library (discord.py) is pretty straightforward.
- get a cheap, second-hand netbook, connect it to the internet
- install beem on it)
- install discord.py
python3 -m pip install -U discord.py
- get a bot token
- insert your bot token here:
import discord import re from beem.utils import resolve_authorperm client = discord.Client() @client.event async def on_ready(): print('logged in') @client.event async def on_reaction_add(reaction, user): print(reaction.emoji) message = reaction.message if "http" in message.content and reaction.emoji == "🙄": match = re.search("(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\[email protected]?^=%&/~+#-])?", message.content) if hasattr(match, "group"): author, perm = resolve_authorperm(match.group(0)) await message.channel.send('<https://steemit.com/@' + author + '/' + perm + '>') client.run('bot-token')
This script will only do something, when a user reacts with a 🙄 emoji to a discord comment that contains something that resembles a steem link and will then try to convert the first url in that comment into a steemit.com link.
I am not going to try to explain this concept. You do not have to fully understand it to work with it - Just accept that this is how discord.py works.
The API reference comes with many examples:
This is not a discord.py guide.
I will describe how to make it work and then concentrate on beem again.
I just copied this part - it seems to work fine.
This is not a regex guide.
Learning regular expressions can make your life a lot easier and is always recommended.
When I randomly found out that resolve_authorperm from beem.utils can deal with d.tube urls, I had to test in how few lines I can make this bot work.
Eventually, your program will crash for some reason. The most efficient and brutish way to automatically restart your script is by starting it like this:
while :; do python3 script.py; sleep 5; done
It is not elegant but works great. Copied from @fyrstikken
There are many cases, when this code will not do the job.
I smashed this out as quickly as possible and kept it short.
If you just wanted some code snippets to be able to start constructing your own service and learning to operate it, this should be roughly covered now, though.
Interface & Server
You will not need your own Discord server, or dedicated server to keep following this guide. When an example requires an interface or a server, I will assume you have it set up like described above, though.
There are many ways to solve this and I am up for suggestions any time.
You can now define commands for your bot and create and administer your own STEEM service for anyone to use.
The next part will be cleaner again, I promise. I just wanted this sorted and out of the way, before I explore beem's modules further and put them to use.