Mastodon Blog Comments Automation
Update 2024-05-08
My setup and script has changed somewhat since writing this. You can find an updated version of the script at this GitHub gist, but the general idea is the same.
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.
1#!/usr/bin/env bash
2set -eo pipefail
3
4blog_path=~/Documents/blog
5
6if [ $# -ne 2 ]; then
7 echo "Usage: $0 <blog post> <gemini post>"
8 exit 1
9fi
10config_path="${XDG_CONFIG_HOME:-$HOME/.config}"
11if [ -f ${config_path}/blog-toot/day ]; then
12 day=$(head -n 1 ${config_path}/blog-toot/day)
13else
14 day=1
15 mkdir -p ${config_path}/blog-toot
16fi
17
18# collect links and assemble post
19read -p "web link: " web
20read -p "gemini link: " gemini
21read -p "message: " message
22post="${message}
23
24#100DaysToOffload Day ${day}
25
26${web}
27${gemini}"
28
29# post to mastodon
30url=$(toot post --no-color "$post" | awk '{print $NF}')
31echo $url
32
33# update blogs with comment link
34echo -e "\n[Comment via Fediverse]($url)" >> $1
35echo -e "=> $url Comment via Fediverse" >> $2
36
37# push blog changes
38git -C $blog_path add $(realpath $1)
39git -C $blog_path commit -m "Add comment link"
40git -C $blog_path push
41
42# push gemini changes
43upgem # this is a script that rsyncs my gemini directory
44
45echo "$(($day + 1))" > $config_path/blog-toot/day