Python for STEEM #4: Excursion: Interface & Server

in #beem5 years ago

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.

Introduction

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 Scripts'

Running your own scripts is fun, but most Steemians do not use a Python interpreter.
While Javascript scripts could be executed in a browser and are usable for almost anybody, the Python scripts that I will publish here will not be as easily usable for a 'normal' user.
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.

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.

Interface

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.

Jump Forward

  • 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.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?", 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')

discord-steemit-url-sanitizer.py

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.

image.png

async

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.

Discord Events

The API reference comes with many examples:
https://discordpy.readthedocs.io/en/latest/api.html
This is not a discord.py guide.
I will describe how to make it work and then concentrate on beem again.

regex

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.

resolve_authorperm

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.

Program Crashes

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

sloppy code

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.

Congratulations

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.

Sort:  

WOW, mostly way over my head, but if I am reading the code properly are you the only one that the script will run for?

Will run for anybody. How would it know who I am ? :P
Give it a try here https://discord.gg/u36fM8t

Next parts will be more conclusive and focused.

I did not understand the use of resolve_authorperm

Thanks for the feedback, honestly.
I have a hard time finding the balance here.
I will take a step back and explain the 'post' module next chapter - that will explain 'resolve_authorperm', too.

Your Welcome, I have done basic level programing in about a dozen languages over the last 30 years, I know the basic concepts like conditionals and loops, but OOP is way above me. maybe I should just sit down and go thru some learning to program Python books from cover to cover and focus on the more in-depth concepts.

Just read the chapters about functions and classes from the official documentation:

https://docs.python.org/3/

Thanks I will give that a go. I am currently studying for my Amateur Radio license.

bin am aufarbeiten

Gib alles XD

Hello,

Your post has been manually curated by a @stem.steem curator.

FA8866FD-F2C3-43B3-A5A5-E0324BA4BB47.jpeg
Supporting Steemians on STEMGeeks

We are dedicated to supporting great content, like yours on the STEMGeeks tribe.

Please join us on discord.