Automatically posting to Mastodon
A few random notes and observations about how this weblog toots.
A week or so ago, when I decided to tidy up a bit, I automated posting a Mastodon update when a new post is made to this weblog. In particular, I did this because I wanted to make it possible to automatically post photographs to Mastodon by adding them to my weblog.
For a normal post, the weblog “toot” is just the title of the post, the abstract, and a link to the post. Easy peasy. To post a photograph, and have the photograph appear in the toot, I have to work a little harder.
I download the image from photos.nwalsh.com, extract the alt text, and send it to Mastodon with the toot as a media attachment. Except sometimes it doesn’t work and that’s really frustrating.
I assumed it was a bug in toot, even though I’d written a patch that made sure the media was successfully uploaded. I decided it would be easier to just use Mastodon.py directly than it would be to debug toot. Besides which, writing a Python script to do some work and then execute another Python script as a subprocess seemed kind of silly.
I used mastodon-local to spin up my own instance. That way I wouldn’t be spamming my account with dozens of test toots. The first time I tried to spin up the docker containers, redis fell over, but it worked fine the second time.
I wanted to create a second account, a non-administrator account, for testing but
my laptop isn’t configured with an MTA listening on port 25. I worked around
this by grabbing the Sendria container and adding it to docker-compose.yml
:
mastomail:
image: msztolcman/sendria:v2.2.2.0
ports:
- "1025:1025"
- "1080:1080"
networks:
- external_network
- internal_network
By configuring Mastodon to use mastomail:1025
to send mail, I could
capture the “verify email address” message and complete the account
creation dance.
I discovered a couple of interesting things. First, I don’t think missing attachments is a bug in the command line client. The Mastodon API just drops media attachments sometimes. I can see from the logs that I successfully uploaded an image and got back an image ID. I can see that I successfully used that image ID in the toot. And I can see from the reply that the toot has no media attachments. 🙁
I changed my script so that it compares the media attachments it thought it was making with the media actually attached. If they don’t match, it immediately deletes the toot. Then it waits two seconds and tries again. It’ll do this three times before giving up. Hopefully that’s the problem sorted.
The other interesting thing is that you can’t reuse media ids. I figured if I knew that I’d successfully uploaded an image, I would keep the ID around and reuse it rather than clogging up the server with another upload of the same image. Apparently not. AFAICT, attempting to use the same media ID in more than one toot just doesn’t work. The server ignores the ID.
Anyway. I think it all works now. If it does the right thing with this post, I’m going to go take a walk on the beach, photograph some things, and see if those get posted correctly too!