Nat TaylorBlog, Product Management & Tinkering

Working with a Trumba Calendar

Published on . Updated on

Recently I got fed up with my YMCA’s calendar due to all the stuff they overlay on it. They use Trumba (Trumba offers web-hosted event calendar software for publishing online, interactive, calendars of events) who it turns out have a pretty nice API.

The trick is getting the calendar’s “webName” which I get from searching sources in DevTools, then the URL is as simple as https://www.trumba.com/calendars/northshore-ymca.json?startdate=20221206&days=7 (and they also support other formats.)

I only wanted events for one location and certain event titles, so I added a bit for that.

"""Get calendar info for the Northshore YMCA

@see https://www.trumba.com/help/api/customfeedurls.aspx"""

import requests
import datetime
import dataclasses

@dataclasses.dataclass
class Event:
    title: str = None
    start: datetime.datetime = None
    end: datetime.datetime = None
    permaLinkUrl: str = None
    signUpUrl: str = None

    def __init__(self, **kwargs):
        self.title = kwargs.get('title')
        self.start = datetime.datetime.fromisoformat(kwargs.get('startDateTime'))
        self.end = datetime.datetime.fromisoformat(kwargs.get('endDateTime'))
        self.permaLinkUrl = kwargs.get('permaLinkUrl')
        if 'signUpUrl' in kwargs:
            self.signUpUrl = kwargs.get('signUpUrl')
    
    def time(self, bound):
        if bound=='day':
            return self.start.strftime('%a')
        return getattr(self, bound).strftime('%I:%M%p').lstrip('0')


today = datetime.datetime.now().strftime('%Y%m%d')
# _1568936_ is for the Marblehead YMCA
r = requests.get(f"https://www.trumba.com/calendars/northshore-ymca.json?startdate={today}&days=7&previousweeks=0&filter3=_1568936_").json()


events_of_interest = [
    'Open Swim - Small Pool',
    'Toddler Open Gymnastics',
    'Bounce House',
    'Kids Club',
    'Open Swim in Small Pool',
    'Toddler and Me Yoga',
]
for i, e in enumerate(r):
    ev = Event(**e)
    if any([t in ev.title for t in events_of_interest]) and 'Adult Open Swim' not in ev.title:
        if ev.signUpUrl:
            title = f"<a href={ev.signUpUrl}>{ev.title}</a>"
        else:
            title = ev.title
        print("<li>{day} {start}-{end} {title}</li>".format(title = title, start=ev.time('start'), end=ev.time('end'), day=ev.time('day')))

Popular Posts

Post Navigation

«
»