Skip to main content

How to automatically create a website with your newest komoot tour

How to automatically create a website with your newest komoot tour

I wanted to include a komoot tour into a website. For this use case komoot offers to embed a tour as an iframe. Since I don't feel like updating the iframe-link on the website every time there is a new tour I decided to write a small script to automate this task. There are still some details I want to improve in the future that you can find at the end of this post. But for now this script works as intended.

Prerequisite

For this tutorial you should have:
- a linux server with its own IPV4 address
- a domain name under your control

Depending on where you want to embed your iframe it might be enought to have an IPV6 address with no domain name.

Installing

First you have to install some packages:

sudo apt install python3 python3-pip nginx snapd
sudo snap install certbot --classic

Then install the PyPi package komootgpx. This tool is used to get a list of all your planned tours. You can install it either in a python virtual environment or system wide like I did:

pip3 install --break-system-packages komootgpx

Then add komootgpx to your PATH so you can call the command from any directory. To do this check the output from the last command. This should show you where the package was installed to. In my case it was "/home/ubuntu/.local/bin", so I added this path to /etc/environment and sourced it:

sudoedit /etc/environment
. /etc/environment

Configure HTTPS

Next up configure HTTPS for your domain so the connection between the user and the server is encrypted.

Leaving out this step might give you trouble when defining the link in the iframe (some web hosters demand an HTTPS-Link).

  1. Point your domain name to your IPV4 address
  2. Add a dummy nginx config with your domain name as the server_name. Restart nginx with sudo systemctl restart nginx.service
  3. Enable HTTPS for this domain with sudo certbot --nginx

Now you should be able to access your server over you domain name with HTTPS.

The script

Create a new file in your desired directory and change the permission:

touch my-script.sh
chmod +x my-script.sh

Then add the following content to your script:

#!/bin/bash

OUTPUT=$(komootgpx --mail=your-kommot-mail-adress@example.com --pass=your-komoot-password -a -t=planned -l --start-date=$(date -I --date="1 day ago") | tail -1 | cut -c1-10)
re='^[0-9]+$'
if [[ $OUTPUT =~ $re ]]
then
    sudo rm /etc/nginx/sites-available/forward
    echo "server {
        server_name your-website.example.com;
        add_header Cache-Control \"no-store, no-cache\";
    if_modified_since off;
    expires off;
    etag off;
        location / {
            return 301 https://www.komoot.com/tour/$OUTPUT/embed;
        }
        listen 443 ssl; # managed by Certbot
        ssl_certificate /etc/letsencrypt/live/your-website.example.com/fullchain.pem; # managed by Certbot
        ssl_certificate_key /etc/letsencrypt/live/your-website.example.com/privkey.pem; # managed by Certbot
        include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    }
server {
    if (\$host = your-website.example.com) {
        return 301 https://\$host\$request_uri;
    } # managed by Certbot
        listen 80;
        server_name your-website.example.com;
    return 404; # managed by Certbot
}" | sudo tee /etc/nginx/sites-available/forward > /dev/null
    sudo systemctl restart nginx.service
else
    exit 0;
fi

Don't forget to change the login information to your komoot account (--mail and --pass) and the domain name of your website (replace your-website.example.com with your actual domain).

This script does the following:

  1. Get all komoot tours of the specified user that were planned today or yesterday. Only use the last line (the oldest tour) and the first 10 characters (unique id of a komoot tour). Write this to the variable OUTPUT.
  2. Test if OUTPUT is a number. If yes, then:
    1. Remove the current nginx config in /etc/nginx/sites-availabe/ that is called 'forward'
    2. Write a new config in the same place using your new OUTPUT
    3. Restart nginx.service
  3. If there is no new tour (so OUTPUT is not a number), then do nothing and exit script.

To test the script create a new komoot tour and execute the script. It should replace/create your nginx config and reload nginx. If you visit your website you now get redirected to your komoot-tour.

Cronjob

If the last step worked you can add the script as a cron job:

crontab -e

Then add the following line after specifying the script's location:

15 1 * * * /home/ubuntu/my-script.sh

This executes the script at 1:15 in the night every day. You can tweak this value to your likings.

Website integration

On your website add an iframe with the following content (replace the domain with your own):

<iframe src="https://my-website.example.com" width="100%" height="600" frameborder="0" scrolling="no"></iframe>

Your website now redirects you to the komoot-website of your newest tour.

To do

These are things I want to improve in my script in the future:
- [ ] Don't just use the last line of the planned tours list, but use the first with the 10 digit code as this is the most current tour (was out of scope for this project since there's only one new tour every month)
- [ ] Make the domain name a variable in the script
- [ ] Follow best practices for using sudo in a script
- [ ] Install the komootgpx package in virtual env instead of system wide
- [ ] Maybe use a more sophisticated way to find the newest tour instead of just choosing one from the last 1-2 days
- [ ] Private Tours might lead to an error because non-logged in users can't see them. Maybe there is a way to filter them.