Auto upload from shell

Naturally, first thing's first - ability to actually upload content to this blog. Pelican (link in footer) generates a bunch of static HTML files perfect for neocities but there's not an easy way to upload them programatically via the shell.

Of course I'm aware that neocities provides a cli uploader, but unfortunately it's ruby garbage and I'm not willing to pollute my system with that. Luckily they also provide an api and even a curl example. Good, I can work with this.

Pelican, if you're not aware, comes with makefile with a bunch of targets to compile html, publish, run a local server, etc. First idea is to simply upload files one by one with curl upon running make publish. You can stick the following bit inside the publish target.:

cd "$(OUTPUTDIR)"; \
find . -type f -exec curl -H "$(AUTH_HEADER)" https://neocities.org/api/upload -F "{}=@{}" \;

AUTH_HEADER is simply the api key header defined above. This will upload files one by one from the output directory even if nothing has changed. Luckily find can use a file as a reference to find all files modified since. With that improvement the command becomes:

cd "$(OUTPUTDIR)"; \
find . -type f -newer "$(REFERENCE)" -exec curl -H "$(AUTH_HEADER)" https://neocities.org/api/upload -F "{}=@{}" \; ; \
touch "$(REFERENCE)"

REFERENCE is just a path to a file (in my case .reference) that I have to touch manually the first time and that I added to .gitignore. While HTML files are all generated every time and all have to be re-uploaded, at least static files will now only be uploaded when changed.

Tagged under neocities pelican cli automation