Ben Tsai

Kinopio backup one-liner

I’ve been using Kinopio for all sorts of things, so it’s important to me I don’t lose any of my work.

Kinopio’s got an API which enables me to program this. I decided to write a shell script because I didn’t want to introduce any other languages to this (Python or JavaScript would have been my other choices), hoping to keep this accessible and not making folks deal with setting up their environment. I’m not sure how well I’ve achieved this, but I did end up using awk for the first time, so a small win there.

Here it is, allow me to explain:

KINOPIO_API_KEY=<your key> \
curl -H "Authorization: $KINOPIO_API_KEY" | \
jq -r ".[] | .id + \",\" + (.name | tojson)" | \
awk -v key="$KINOPIO_API_KEY" '{ print "curl -H \"Authorization: "key"\""$1" > "$2".json; sleep 1;" }' FS=, OFS=, | \
xargs -0 -n1 bash -c

It took me many hours to figure this out. I got stuck for a while when I had gotten the list of spaces and their ids, but I didn’t know how to take that output to create the final command. I was trying to manipulate stdin, then thought I should use variables instead. I don’t remember my search terms, but I eventually stumbled on using awk to write the new command, which is the insight I needed to finish the job.

I’ve got a private GitHub repo where I’ve added this as an Action, so now every hour, this runs and commits any changes to the repo.

Sometimes I’m getting a 401 from the Kinopio API, so I may have to tweak the timing. I wonder if I’m being throttled :)

Update at 11:03p: The 401’s were because I wasn’t expanding the API key environment variable in the awk command. I fixed that, which then led to getting throttled by the API. So then I added a one second sleep.

Tuesday, September 22, 2020