What I built
I built a Mastodon bot that posts a a different bit of Pluto every six hours.
Category Submission
Wacky Wildcard
App Link
https://botsin.space/@bitsofpluto
Screenshots
Description
Bits of Pluto posts a different bit of Pluto to Mastodon every six hours. Each is a crop from an image by NASA's New Horizons spacecraft.
Link to Source Code
https://github.com/hugovk/bitsofpluto
Permissive License
MIT
Background
For decades, Pluto had only been a speck of light until the NASA's Hubble Space Telescope captured it in never-before-seen detail:
Just a few years later, I followed the journey of NASA's New Horizons spacecraft in awe as it flew past Pluto, in the far depths of our solar system.
Like many others I was especially struck by the beautiful photographs it shot, especially this large, detailed image:
As NASA describe it:
NASA’s New Horizons spacecraft captured this high-resolution enhanced color view of Pluto on July 14, 2015. The image combines blue, red and infrared images taken by the Ralph/Multispectral Visual Imaging Camera (MVIC). Pluto’s surface sports a remarkable range of subtle colors, enhanced in this view to a rainbow of pale blues, yellows, oranges, and deep reds. Many landforms have their own distinct colors, telling a complex geological and climatological story that scientists have only just begun to decode. The image resolves details and colors on scales as small as 0.8 miles (1.3 kilometers). The viewer is encouraged to zoom in on the full resolution image on a larger screen to fully appreciate the complexity of Pluto’s surface features.
I second the recommendation to zoom in on the full resolution! The detail and is astounding, especially compared to the Hubble image.
I thought it would be fascinating to chop this up and put it in a social media feed to enjoy different aspects of the detail, to punctuate the doomscrolling with a moment of wonder.
How I built it
I built the bot in Python. Originally it posted to Twitter, but as the future of bots on Twitter is less than certain, the time was ready to port it over to Mastodon where bots are welcome.
On the way, I learned how to calculate brightness in images, how to use the Mastodon API, and how to set up a bot on Linode and (the appropriately-named!) botsin.space.
The bot does two things:
- Get a bit of Pluto
- Post to Mastodon
1. Get a bit of Pluto
The bitsofpluto()
function takes a single parameter, the path to the full resolution Pluto image.
The image is opened using Pillow.
We then choose a random width for the image, somewhere between 800 and 2000 pixels, with height is set to ¾ the width. This is to give a different zoom level each time. We then randomly select a window to crop. This is our potential bit of Pluto.
But we don't stop there. The image has quite a large background of empty black (or nearly black) space. We don't want to post that. So we sample 9 points from the image (corners, centre point, and bisecting points) and measure the brightness where 0 is black and 255 is white. If brightness is under 10, we count it as a dark point. If there are 6 or less, we save the image to a temporary directory.
Otherwise if there are too many dark points, we discard this crop, and start again from the top. I came up with these figures of 10 and 7 through trial-and-error. It's quite nice to sometimes get an image with a corner of Pluto's curvature against the darkness of space:
2. Post to Mastodon
We use the Mastodon.py library to via the Mastodon API.
The toot_it() function
first reads in the credentials for posting. I generated them using a helper script I wrote based upon Allison Parrish's instructions.
Once authenticated, there's a two-step process for posting:
- Upload the image using
api.media_post()
, which returns a reference to the media - Create the Mastodon post, using the media reference
Hosting
Linode
The bot code is hosted on Linode. The nice thing about Mastodon bots is that they rarely need expensive or high-end hosting. I created a Linode using the cheapest option: for $5/month the "Nanode 1 GB" plan gives 1 GB RAM, 1 CPU and 25 GB storage, more than enough for our needs.
I chose a Ubuntu 22.04 LTS image: LTS means "long-term support", it will be supported for 5 years, until April 2027. And I chose the Frankfurt, DE region because it's closest to me.
After creating a user, I logged in via SSH, and cloned the bitsofpluto repo.
I ran crontab -e
to schedule it to run once every six hours:
0 */6 * * * /home/botuser/bin/scheduled/0000-06-repeat.sh > /tmp/logs/0000-06-repeat.log
Where 0000-06-repeat.sh
contains:
#!/bin/bash
#set -e
mkdir -p /tmp/logs/
~/github/bitsofpluto/crontask.sh > /tmp/logs/bitsofpluto.log 2>&1
Which, when the cron triggers, switches to the repo, fetches any recent changes, and then runs the bot to make a post:
python3 bitsofpluto.py --yaml ~/bin/data/bitsofpluto.yaml --no-web
botsin.space
The bot's Mastodon account is on Colin Mitchell's botsin.space instance, home to many other excellent bots.
Additional Resources/Info
I've made two other Mastodon bots:
Enjoy!
Top comments (0)