Sign In/My Account | View Cart  
advertisement


Listen Print Discuss

Parsing iCal Data
by Robert Pratte | Pages: 1, 2, 3, 4

This code will produce the following Dot file:

digraph unnamed {
   size     = "10,7";
   compound = true;
   ratio    = fill;
   rankdir  = LR;

   "5F88A0EC_AD21_428E_AAAD_005F1B1AB72E" [ shape = record, 
      label = "Set up File Server | <START> Start | 
	  <END> End  | Music server for the kids."];

   "5F88A0EC_AD21_428E_AAAD_005F1B1AB72E":START  -> "20050615T180000";

   "BDF17182_CA21_4752_8D4F_40A4FE47C90D" [ shape = record, label = "WWDC | 
      <START> Start | <END> End  | Lots of sessions."];

   "BDF17182_CA21_4752_8D4F_40A4FE47C90D":START  -> "20050606";

   "BDF17182_CA21_4752_8D4F_40A4FE47C90D":END    -> "20050612";

   // Create tasks relationships

   // Create timeline relationships

   "20050606" -> "20050612" -> "20050615T180000";
}

Note that this code uses the record shape, holding individual segments within the larger object. This is slightly more complicated than the default oval that Dot uses.

Where to Go from Here

If you are using Apple's iCal application, note that the location and naming scheme of iCalendar files changed between the 1.x and 2.x releases. Previously, iCalendar files went in the ~/Library/Calendars/ directory and had names of the form <calendar name>.ics. Thus, a calendar named Work would have a file Work.ics. However, the 2.x release keeps iCalendar information in the ~/Library/Application Support/iCal/Sources/<calendar name>/ directory as sources.ics.

Other applications that implement the iCalendar specification, such as Mozilla's Calendar extension for Mozilla/Firefox/Thunderbird, may follow a different convention. On a Mac, Firefox stores .ics files in the ~/Library/Application Support/FireFox/Profiles/<profile>/Calendar/ directory, where <profile> is the profile specified in the Firefox profile.ini file. Again, other systems will likely store this information in different locations.

While on the topic of different implementations, bear in mind that, while the key:value specifications are consistent (as long as the application conforms to RFC 2246), the actual .ics file may look slightly different. For example, Firefox lays out that first event from the previous example as:

BEGIN:VEVENT
UID
 :b9794c88-1dd1-11b2-bb51-8a92011a78e8
SUMMARY
 :Apple WWDC
DESCRIPTION
 :Lots of sessions
LOCATION
 :San Francisco
URL
 :http://developer.apple.com/wwdc
STATUS
 :TENTATIVE
CLASS
 :PRIVATE
DTSTART
 ;VALUE=DATE
 :20050606
DTEND
 ;VALUE=DATE
 :20050612
DTSTAMP
 :20050618T191731Z
END:VEVENT

Here, the key:value tuples (plus any data modifiers such as VALUE=DATE) almost always split up across lines. In this case, it would be best to handle this difference when reading in the .ics file, so that the rest of the script can expect data in a generic format. One way to do this is to copy the array representing the .ics file using a finite-state machine. Another method would be to walk the array and join array elements under certain conditions, such as if the first non-white-space character of the current element begins with a colon or semicolon character, or is simply non-alphabetic.

Hopefully, this article will spur you to create a bridge between two of your favorite applications. Good luck, and please remember to share your contributions with the community.

Perl Best Practices

Related Reading

Perl Best Practices
By Damian Conway