Hackety hack hack

Atypisches Nutzerverhalten mit Rat und Tat.

So I wrote a bot for Mastodon and learned some things

Everything becomes hip and modern when you put the prefix “cyber” to it. And what could be more hip and modern than the wonderful world of governance and administration in Germany? You may (cynically!) think otherwise, but the German government really keeps up with the cyber. Case in point: It regularily publishes openings for dream jobs that have the word “cyber” in them. Let’s keep track of those and post them through a bot! (This wonderful idea originally comes from @didumdida).

Writing a bot for Mastodon is delightfully simple. There is a special instance that welcomes bots — botsin.space — and this is where the bot for federal cyber job offers or “Bundescyberjobangebote” in German (it’s a long word, but that’s kind of a German thing) currently lives as @bundescyberjob.

The listing of job offers from the federal government with the word “cyber” in it lives here as an RSS feed.

Reading in the RSS feed is pretty straight-forward with the feedparser Python module. Writing it to Mastodon is easy with the Python wrapper for the Mastodon API.

To keep everything clean and use the installed libraries like feedreader and Mastodon with exactly the version I needed, I used modern Python’s way to go: venv. But that gave me another problem: How can I call the Python script from cron now to make a daily post? After all, cron’s environment doesn’t really know about venv.

And another thing puzzled me about cron: I don’t want posts to be made at the exact time every day, that just looks ugly and automated. But cron doesn’t have a concept of “execute at a random time”.

To solve both problems, I don’t call the Python script directly from cron, but rather a wrapper written in bash. This thing only does two things: First, it sleeps up to twelve hours before it continues, spreading the actual posting at some time between 9am and 9pm in my time zone.

Then it just sources the venv activate script to use venv’s settings and lastly executes the Python script to post. It looks a little like this:

#!/usr/bin/env bash

# Sleep for up to 12 hours
sleep $((RANDOM % 43200))

# Run the bot
source /home/johl/bundescyberjobbot/bin/activate
python /home/johl/bundescyberjobbot/bundescyberjobbot.py

With all these pieces in place, I finally had a wonderful little bot to inform the world about cyber-related jobs. CYBER!