Converting Google Calendar Links to iCal

Recently I stumbled upon a Google support thread that outlines how to convert a Google calendar link into an iCal subscription link. The reason this is so important is that it allows you to subscribe to a Google calendar via any other sort of caldav client including Nextcloud or the iOS/macOS' native Calendar app. As an example, let's take the Jupiter Broadcasting calendar and convert it into an iCal link to subscribe to via Nextcloud. Here's the link: https://calendar.google.com/calendar/embed?src=jalb5frk4cunnaedbfemuqbhv4%40group.calendar.google.com&ctz=America%2FPhoenix. Notice the src= portion of it? We're going to copy the part directly after that, so in this case it's jalb5frk4cunnaedbfemuqbhv4%40group.calendar.google.com.

We're going to take this section, and wrap it with this: https://calendar.google.com/calendar/ical/<src>/public/basic.ics, where <src> is the part from the previous section. The final result ends up looking like this: https://calendar.google.com/calendar/ical/jalb5frk4cunnaedbfemuqbhv4%40group.calendar.google.com/public/basic.ics. If you copy that into your Nextcloud calendar subscriptions, you'll now have access to the Jupiter Broadcasting calendar from there.

To simplify this process, I've written a quick Python script to do this for you. Here's the source for it:

#!/usr/bin/env python3

from urllib.parse import urlparse,parse_qsl
import sys
from pprint import pprint

ICAL_FORMAT = 'https://calendar.google.com/calendar/ical/{}/public/basic.ics'

def main(cli_input):
    gcal_url = o = urlparse(cli_input)
    query_string = gcal_url.query
    src = parse_qsl(query_string)
    for pair in parse_qsl(query_string):
        if pair[0] == 'src':
            print(ICAL_FORMAT.format(pair[1]))
            return

if __name__ == '__main__':
    main(sys.argv[1])

Copy that into a file, like gcal-to-ical.py, give it executable permissions, then run it like so:

./gcal-to-ical.py https://calendar.google.com/calendar/embed?src=jalb5frk4cunnaedbfemuqbhv4%40group.calendar.google.com

That will produce our ical URL, https://calendar.google.com/calendar/ical/jalb5frk4cunnaedbfemuqbhv4@group.calendar.google.com/public/basic.ics, which we can directly paste into Nextcloud or any other caldav client which supports ical calendar subscriptions.

I've also added this script to a git repo, so be sure to check there in case the source has updated.