| 1 |
#!/usr/bin/perl |
| 2 |
|
| 3 |
use constant SOURCE => "/Users/mark/Library/Application Support/iCal/Sources/E49F498D-968C-4A4F-93E8-6E9AF7F44B2B.calendar/corestorage.ics"; |
| 4 |
|
| 5 |
use strict; |
| 6 |
use warnings; |
| 7 |
|
| 8 |
use XML::RSS; |
| 9 |
|
| 10 |
# load the calendar |
| 11 |
use Data::ICal::DateTime; |
| 12 |
|
| 13 |
# create a new RSS feed |
| 14 |
my $rss = new XML::RSS (version => '1.0'); |
| 15 |
|
| 16 |
rss(0, 1, "today!"); |
| 17 |
rss(1, 2, "tomorrow!"); |
| 18 |
rss(2, 3, "day after tomorrow!"); |
| 19 |
rss(3, 7, "within a week"); |
| 20 |
rss(7, 14, "within a fortnight"); |
| 21 |
|
| 22 |
print $rss->as_string; |
| 23 |
|
| 24 |
################################# |
| 25 |
|
| 26 |
my ($ical, @events); |
| 27 |
sub rss |
| 28 |
{ |
| 29 |
my ($start, $end, $comment) = @_; |
| 30 |
|
| 31 |
# calculate the next 14 days. This is done once because it 'explodes' |
| 32 |
# the events and hence is quite expensive |
| 33 |
unless ($ical) |
| 34 |
{ |
| 35 |
$ical = Data::ICal->new( filename => SOURCE ); |
| 36 |
@events = $ical->events( |
| 37 |
DateTime::Span->from_datetimes( |
| 38 |
start => midnight(), |
| 39 |
end => midnight()->add( days => 14, seconds => -1), |
| 40 |
) |
| 41 |
); |
| 42 |
} |
| 43 |
|
| 44 |
# what are we looking for this time? |
| 45 |
my $span = DateTime::Span->from_datetimes( |
| 46 |
start => midnight()->add( days => $start ), |
| 47 |
end => midnight()->add( days => $end, seconds => -1), |
| 48 |
); |
| 49 |
|
| 50 |
# find which of those events in the next 14 days we're concerned |
| 51 |
# with and add them to the rss feed |
| 52 |
foreach my $event (grep { $_->is_in($span) } @events) |
| 53 |
{ |
| 54 |
$rss->add_item( |
| 55 |
title => $event->summary . " $comment", |
| 56 |
description => "On ".$event->start->ymd." (".(qw( |
| 57 |
Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday ))[ $event->start->dow ] |
| 58 |
. ")" |
| 59 |
); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
# midnight today. |
| 64 |
my $midnight; |
| 65 |
sub midnight |
| 66 |
{ |
| 67 |
$midnight ||= |
| 68 |
DateTime->now->set( hour => 0, minute => 0, second => 0, nanosecond => 0 ); |
| 69 |
return $midnight->clone; |
| 70 |
} |