No-Good Very Bad GCal Link Generator with Python

Questions, feedback, suggestions? Check out the comments

I was working on a webinar implementation for a client who wanted to share add-to-calendar’ links with prospects who registered. The wrinkle: they wanted to send these emails through HubSpot instead of through the webinar platform and they wanted the calendar event to include the personalized click-to-join’ link somewhere within it. I’m not actually done yet, but in the process I slammed together some Python code to better understand how these kinds of Google Calendar links get generated. Here’s what that looks like.

import clipboard

url = "https://www.google.com/calendar/render?"

action = "action=TEMPLATE"
title = input("Event Title: ")
text = "text="+title
startDate = input("Start DateTime: ")
endDate = input("End DateTime: ")
dates = "dates="+str(startDate)+"/"+str(endDate)
link = str(input("Link: "))
description = str(input("Description: "))
location = "location="+link
details = "details="+description
sf = "sf=true"
output = "output=xml"


parameters = "&".join([
    action,
    text,
    dates,
    details,
    location,
    sf,
    output])
finalURL = url+parameters

clipboard.copy(finalURL)
print(finalURL)

What I’ve done here is take the output from one of the online generator tools, which looks something like this:

https://calendar.google.com/calendar/u/0/r/eventedit?text=NetSpinnr%20Event&dates=20240401090000/20240401093000&details=This%20is%20a%20test%20event&location=https://www.netspinnr.com&sf=true&output=xml

Then run it through another handy Python snippet I have on hand for grabbing query parameters (it takes anything in my clipboard, grabs everything after ?, splits it using &, then outputs something easy to read):

import clipboard
from pprint import pprint

url = clipboard.paste()
params = url.split('?')[1].split('&')
paramDict = {}
for i in params:
    l = i.split('=')
    paramDict[l[0]]=l[1]

pprint(paramDict)

Which spits out this output:1

{'dates': '20240401090000/20240401093000',
 'details': 'This%20is%20a%20test%20event',
 'location': 'https://www.netspinnr.com',
 'output': 'xml',
 'sf': 'true',
 'text': 'NetSpinnr%20Event'}

From there you can pretty easily unpick what each query parameter is doing. Dates are, well, dates, just in YYYYMMDDhhmmss’ format.2 Start date comes first, then end date, separated by a /. Details is the event description. Location is location, text is the title, and so on. sf and output also do things, but I’m going to gloss over that here.3

What I’ve then done is take that and write a Python script to intake values for each of those query parameters. It’s ugly and in no state to deliver to the client, but it works. So I figured I’d share it here in case anyone’s interested, wants to fiddle with it themselves, or has suggestions for how to make it better.

Here it is again, this time with comments:

import clipboard

#Base URL here

url = "https://www.google.com/calendar/render?"

action = "action=TEMPLATE"

#Stores user input as the event title
title = input("Event Title: ")
text = "text="+title

#Stores user input for Start Time and End Time
startDate = input("Start DateTime: ")
endDate = input("End DateTime: ")
#Combines datetimes into the right final format
dates = "dates="+str(startDate)+"/"+str(endDate)

#Store user input for a Link and saves it to the 'Location' parameter
link = str(input("Link: "))
location = "location="+link

#Store user input and saves it to the Event's Description
description = str(input("Description: "))
details = "details="+description

sf = "sf=true"
output = "output=xml"

#Joins all of the query parameters defined using `&` 
parameters = "&".join([
    action,
    text,
    dates,
    details,
    location,
    sf,
    output])

#Concatenates the base 'add to calendar url' with the query parameters defined
finalURL = url+parameters

#Saves the final url to the clipboard for ease of sharing elsewhere
clipboard.copy(finalURL)

#Displays that final url
print(finalURL)

Next Steps

Maybe down the line I’ll build this out in JavaScript and stick it on the site somewhere. There’s dozens of these generators out there but I think building one that accepts straight-HTML with hyperlinks in the description could be very handy. Something for the future!


  1. If you’re wondering about all the % signs, that’s called URL-encoding and it’s the way that spaces and special character get encoded in urls. %20, for example, is a space.↩︎

  2. AKA: Year, Month, Day, Hour, Minute, Second, with everything except for year expressed using 2 digits. It’s all GMT, so if you want to do anything fun with timezones you need to either add the offset yourself (subtract 6 hours, for example) or add another query parameter to flag the timezone your event is in.↩︎

  3. Here’s a nice resource I found with a full outline of all query parameters. I could have sworn I’d tracked down an explanation for what sf is doing recently but I’m coming up blank now. Here’s another guided exploration of how these links work↩︎


Written by Jack Segal. Shoot me an email with questions or comments, and if you'd like to support my writing, you can send me a tip using Stripe!



Date
2024-03-05



Subscribe

Fill out the form to get notified whenever a new post goes live!

Powered by Buttondown.



Comments