arrow-down coffee engineering consultancy development remote-management support linkedin twitter youtube email phone gitlab github

Automating yourself out of a job you accidentally gave yourself

Submitted by oliver on August 22, 2018

“Can you schedule the next round of sessions, Olly?”

Ugh. What a pain to have to put all that stuff into the calendar again. Every Friday one of the OlinData team hosts what we call a knowledge sharing session.

We keep the schedule in Google Calendar. I put the schedule in there as an escape from a spreadsheet. Inadvertently I’ve become the defacto knowledge sharing calendar scheduler person. To save myself the pain of pointing and clicking around a laggy javascript web interface for an hour, I decided to find a way to automate myself out of the job I created.

Google Calendar is a web application, so it probably has some HTTP API that you exchange JSON-like data with. And it does. Then you need to do authentication, format the data… Maybe someone has done something like this already; there’s probably a library you can just import into your little program. You do a search with Google, and find yourself on GitHub. The dependencies and complexity of the project grows right in front of your eyes. Perhaps it would be easier to just hop on to Google Calendar and do it all manually.

Then I remembered that Google Calendar speaks CalDAV. I started looking for a CalDAV command line client, which I could just script with the shell. Nothing particularly elegant grabbed me. Through reading what a CalDAV client really is, I discovered the iCalendar file format. It’s a text file. No dependencies, no libraries. Write a file to a filesystem. Any programming language can do that. At that moment I knew my calendar scheduling job was about obsolete. Now to do the automation.

Here’s an iCalendar file for my 1-hour knowledge sharing session for the 24th of August at midday:

SUMMARY:Olly knowledge sharing

I need to programatically write this data with varying values for the name (“Olly”) and day (“20180824”). I already have my schedule:

Olly 20180824
Marcello 20180831
Walter 20180907

That information needs to be fed into my program somehow, and I want an iCalendar file to be spat out at the end. There’s a million ways to do this. Here’s one way, in a whopping 25 lines.

#!/usr/bin/awk -f

    print "VERSION:2.0"
    print "PRODID:-//kshare"
    name = $1
    day = $2
    if(NF != 2) {
        print FILENAME ":" NR " invalid date line" > "/dev/stderr"
    print "BEGIN:VEVENT"
    print "DTSTART:" day "T120000Z"
    print "DTEND:" day "T130000Z"
    print "SUMMARY:" name " knowledge sharing"
    print "END:VEVENT"
    print "END:VCALENDAR"

It’s written in awk, and I’ll call it kshare because there’s no point coming up with a better name for such a trivial thing. It assumes my schedule is formatted as above; a line with a name and date separated by a space. There’s even some basic error checking with diagnostics. Assuming my schedule is in the file schedule, run:

kshare schedule

And you get an iCalendar file written to the screen. To save it as an iCalendar file with the traditional .ics file extension:

kshare schedule > schedule.ics

Now upload schedule.ics to Google Calendar. Or email it to friends and they can tap it so it magically appears in their calendar on their iPhones. Or anything.