moddedbear.com

Mastodon Blog Comments Automation

When I post here I usually also put the link out on Mastodon where people will sometimes comment. I’ve always done this by hand but, since I know just enough bash to be dangerous, I recently started thinking of ways to automate this step for me and also link back to the Mastodon post from the original blog post while I’m at it.

So I wrote a bash script. It’s not very good and is pretty specific to my use case, but it gets the job done. Or at least I think it will. My first time testing it out fully will be after I publish the post. If you see a link to the Mastodon post at the bottom of this page, it worked!

The usage is pretty simple. I call the script with the paths to my blog and gemini posts, then it prompts me for the links to the posts and a message to go with them. It also keeps track of which day of #100DaysToOffload I’m on and adds that to the post. It posts to Mastodon using the toot command line client and notes the URL of the Mastodon post. The last steps are appending links to the Mastodon post to my blog and gemini posts and pushing the changes.

Here’s the script. Maybe it’ll give someone ideas for a similar automation. If there’s anything horribly wrong with it please yell at me so I can fix it.

#!/usr/bin/env bash
set -eo pipefail

blog_path=~/Documents/blog

if [ $# -ne 2 ]; then
    echo "Usage: $0 <blog post> <gemini post>"
    exit 1
fi
config_path="${XDG_CONFIG_HOME:-$HOME/.config}"
if [ -f ${config_path}/blog-toot/day ]; then
    day=$(head -n 1 ${config_path}/blog-toot/day)
else
    day=1
    mkdir -p ${config_path}/blog-toot
fi

# collect links and assemble post
read -p "web link: " web
read -p "gemini link: " gemini
read -p "message: " message
post="${message}

#100DaysToOffload Day ${day}

${web}
${gemini}"

# post to mastodon
url=$(toot post --no-color "$post" | awk '{print $NF}')
echo $url

# update blogs with comment link
echo -e "\n[Comment via Fediverse]($url)" >> $1
echo -e "=> $url Comment via Fediverse" >> $2

# push blog changes
git -C $blog_path add $(realpath $1)
git -C $blog_path commit -m "Add comment link"
git -C $blog_path push

# push gemini changes
upgem  # this is a script that rsyncs my gemini directory

echo "$(($day + 1))" > $config_path/blog-toot/day

Comment via Fediverse

#100DaysToOffload #linux #programming