KISS

Keep It Simple Stupid

Automate saving Glacier webcam pictures

| comments

Glacier National Park is really beautiful! The website has a dozen of webcams in different parts of the park and winter views are amazing (from the working cameras). All the webcams with their descriptions are on this page, but I like the current thumbnails page more because it provides a compact view of the current webcam images. Sometimes I want to save a picture, but I have to manually change the filename or append a suffix so that previous images from the same webcam aren’t overwritten — this is annoying. How could we automate this?

Opening the image

I use the great Tridactyl Firefox extension so that I have some more vim-like navigation (vimperator was even more powerful). The vimperator-like hint mode is on in ~/.tridactylrc (type :tutor in Firefox to see what it means):

1
2
3
" Sane hinting mode
set hintfiltermode vimperator-reflow
set hintnames numeric

So on the webcams page, to open an image I can type ;i and then a number of the image.

Saving the image

Ideally I’d save the currently open image to a known directory with an automatically unique filename (the base filename + autoincrementing counter). I couldn’t find anything that could do that in Firefox directly, and also javascript can’t access the local filesystem. The next best thing is to trigger a shell script to download and save the image, but how to notify it about the current image URL?

Tridactyl has a native messenger that allows it to communicate with the local host (beware of the security implications), e.g. to be able to :source your ~/.tridactylrc file. Well, it can also run arbitrary shell commands (:h exclaim), which is exactly what we need.

I’ve written a zsh script that uses z-shell’s advanced expressions to figure out the next filename:

(save_glacier_image.sh) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/env zsh

set -euo pipefail
IFS=$'\n\t'

URL="$1"

cd ~/Pictures/glacier

BASENAME="${URL:t:r}"
LATEST_FILE="$( ls $BASENAME*(.On[1]) )"
PADDED_NEW_NUMBER="${(l:3::0:)$(( ${LATEST_FILE:${#BASENAME}:r} + 1 ))}"

curl -Ss -o "${BASENAME}${PADDED_NEW_NUMBER}.${URL:e}" "$URL"
echo 'done'

URL is the parameter. BASENAME is the base filename, say smptz. LATEST_FILE is the newest file with this basename, say smptz031.jpg. The magic in PADDED_NEW_NUMBER extracts the number by removing the length of the basename and removing the extension, increments it by one, and pads it to three decimal places. Then curl downloads the image by the URL and saves to the correct filename. You can man zshexpn for all the details on the variable expansions.

Note: I haven’t tested this script in the case when you don’t have any of these images saved yet, it’ll likely fail. You can manually save the first image by appending 001 to the filename.

This is the line I added to my ~/.tridactylrc file:

1
bindurl ^https://www.nps.gov/webcams-glac/ zs composite get_current_url | exclaim ~/bin/save_glacier_image.sh

Now I can type zs on any of the opened webcam images (all of them are hosted on https://www.nps.gov/webcams-glac/), which will pass the current URL to the shell script.

Organizing the existing filenames

Initially I had been saving the images, appending a suffix manually. I wanted the names to be consistent with the script above. To rename them, I used zmv from zsh to append a counter to the base filename:

1
2
3
$ autoload zmv
$ c=1 zmv -Q '(logan1)*(Om)' '${1}${(l:3::0:)$((c++))}.jpg'
$ c=1 zmv -Q '(logan2)*(Om)' '${1}${(l:3::0:)$((c++))}.jpg'

and so on (this could be generalized for all the base names, but wasn’t practical for one time). zmv is a complicated tool, there are useful examples in the gist.

The end

I’m happy with the solution overall, the friction to save a beautiful image is much lower now.

Of course, visiting the park in person is magnitudes better than just looking at the images. So go there if you can!

Comments