In my first TIL (Today I Learned) post I've been testing a python script that runs when Netlify deploys my website. The script creates a published_posts.json
to compare published posts in the /content
directory checking for any new posts in notes, essays and newsletters. New posts will then be posted on my Mastodon feed.
My development skills are firmly in the realm of html, css and javascript. So diving into python was a daunting prospect, but made easier and fun with the help from Claude1 to develop the inital concept. I then fine-tuned this further by pair-programming with GitHub Copilot2 inside Visual Studio Code(GPT 4o model - it was only after completing my experiment you can toggle between models, so I'm now using Claude 3.5 Sonnet preview3 going forward.) Along the way I had a few issues to resolve, such as a caching issue on Netlify, discussed in more detail below.
This is an experiment and part of my TIL(Today I learned posts), so things might be a little rough around the edges. Code should be used at your own risk.
Netlify build file cache
Initial Netlify deployments were publishing multiple content posts to Mastodon. I discovered this was linked to published_posts.json
not being cached during each deployment. A trait of static site generators is to rebuilt during each deployment.
Thanks to Max Bรถck's article4 on persistent build folders in Netlify I was able to cache the published_posts.json
this allowed mastondon_publisher.py
script to check for new posts, rather than a new published_posts.json
file to be written each time, this would ultimately ignore the verification process from the script. I also discovered the Netlify Debug Cache Plugin5 to correctly check if the cache file was being created.
Project gists
Below are the following gists containing the code used in this experiment.
*I've excluded my Zola build files.
Python script
Once I had a working script I prompted copilot to heavily comment to help me understand for future reference and any other readers.
Netlify Toml
The netlify.toml6. is a configuration file that specifies how Netlify builds and deploys the Zola static site files along with the requirements.txt
containing the required Python packages:
requests
toml
Custom Netlify Build Plugin
This was used to cache published_posts.json
file as explained earlier
Netlify environment variables and application token
{MASTODON_ACCESS_TOKEN}
was accesed when creating a new application in the development tab7. Along with the following permission scopes read:statuses
profile
write:media
write:statuses
.
In Netlify control panel, I set the following environment variables. These would then be used in the python script to post my latest posts to Mastodon.
MASTODON_ACCESS_TOKEN = {MASTODON_ACCESS_TOKEN}
SITE_URL = https://mastodon.social
WEBSITE_URL = https://jamieleung.co.uk