January 16, 2008

Birthday tracker

I'm not so great a remembering things like birthdates, so I whipped up a little script to generate an iCalendar file that can be read into my Google Calendar (or any other calendering app that lets you "subscribe" to a calendar).

I tried to keep this as bare-bones as possible. It is a ruby script, and it should work just fine on any shared host.

The script:


#!/usr/bin/env ruby
# Created by Karl Nelson, 1/16/2008
# This cgi script reads a YAML file (events.yml) and produces an iCalendar-formatted file
# that can be read by calendaring programs (iCal, Google Calendar, etc).
# The purpose is to display events that repeat annually, like birthdays, anniversaries, and such.
# I'm using this to track the important dates in my family.

require 'yaml'
$LOAD_PATH << './icalendar-1.0.2/lib'
require 'icalendar'
require 'date'
require 'cgi'

# Load data and various "constants"
cgi = CGI.new
events = YAML::load( File.open( 'events.yml'))
if cgi.include?('families')
families = cgi['families'].split(',')
else
families = ['smith']
end
years = [Date.today.year - 1, Date.today.year, Date.today.year + 1]
cal = Icalendar::Calendar.new

# Loop through the data, producing the events
years.each do | year |
families.each do | family |
events[family].each do | event |
actual_date = Date.parse(event['date'])
date = Date.new(year,actual_date.month,actual_date.mday)
if actual_date < date
ical_event = Icalendar::Event.new
ical_event.dtstart date, {"VALUE" => ["DATE"]}
ical_event.summary = event['event']
ical_event.description = "#{actual_date.strftime('%m/%d/%Y')}, #{(date.year - actual_date.year)} years ago"
cal.add_event(ical_event)
end
end
end
end

# Send the data out...
cgi.out("text/plain") { cal.to_ical }

Because I have more than one "family" I'd like to track, and I'd like to let my relatives use this for their purposes, I've built in a way to specify (via a query string variable) which family it displays. Then, in the YAML file, just list the events like this:

smith:
    -
        event: "Uncle Earl's birthday"
        date: 1/10/1950
    -
        event: "Aunt Bessie's birthday"
        date: 1/11/1950

Feel free to use it any way you want...

Posted by Karl at 11:24 AM