Debian Codegraph @

bin/wgo

raw
1 #!/usr/bin/perl -w
2 #
3 # What's Going On. Given one or more .ics files on the command line,
4 # produces a text report showing what's happening in the next six weeks.
5 #
6 # This was my main reason for working on this -- I've been producing things
7 # like this by hand for a mailing list of friends on and off for the past
8 # six years, and was too lazy to automate the damn thing.
9 #
10 # To see it in action, try fetching the london.pm calendar from
11 # http://london.pm.org/ical.ics, then run "perl wgo ical.ics"
12 #
13 # Nik Clayton, <[email protected]>
14
15 use strict;
16
17 use lib '../lib';
18 use Text::vFile::asData;
19
20 use DateTime;
21 use DateTime::Span;
22 use DateTime::Format::ICal;
23
24 use IO::File;
25
26 # Could use Lingua::*::Inflect, but it's overkill for a quick demo
27 # 1 2 3 4 5 6 7 8 9 10
28 my @inflect = qw(st nd rd th th th th th th th
29 th th th th th th th th th th
30 st nd rd th th th th th th th
31 st);
32
33 # Get the current time, and the time six weeks from now. Then build a
34 # DateTime::Span. We'll test every event we find to see if it, or any of
35 # it's recurrences, are inside this span. If they are, we need to include
36 # them in the output.
37 #
38 # Yes, start time and end time should be parameters to the script.
39 my $start_dt = DateTime->now();
40 my $end_dt = $start_dt + DateTime::Duration->new(weeks => 6);
41
42 my $span = DateTime::Span->from_datetimes(start => $start_dt,
43 end => $end_dt);
44
45 my @cals = (); # List of refs returned by parse()
46
47 my %events = (); # Events we're interested in. Key is
48 # date/time formatted as an ICal string,
49 # value is a list of events happening at
50 # that time.
51
52 foreach(@ARGV) {
53 push @cals, Text::vFile::asData->new->parse(IO::File->new($_));
54 }
55
56 foreach my $cal (@cals) {
57 foreach my $obj (@{ $cal->{objects}->[0]->{objects} }) {
58 next if $obj->{type} ne 'VEVENT'; # Not interested in non events
59
60 my %e = (); # Info about this event
61 my $p = $obj->{properties}; # The event's properties
62
63 next if ! defined $p->{SUMMARY}->[0]->{value};
64
65 $e{start} =
66 DateTime::Format::ICal->parse_datetime($p->{DTSTART}->[0]->{value});
67
68 if(defined $p->{DTEND}->[0]->{value}) {
69 $e{end} =
70 DateTime::Format::ICal->parse_datetime($p->{DTEND}->[0]->{value});
71 # iCal represents all-day events by using ;VALUE=DATE and setting
72 # DTEND=end_date + 1
73 $e{end}->subtract( days => 1 )
74 if $p->{DTEND}[0]{param}{VALUE} && $p->{DTEND}[0]{param}{VALUE} eq 'DATE'
75
76 } else {
77 $e{duration} =
78 DateTime::Format::ICal->parse_duration($p->{DURATION}[0]{value} || "PT1S" );
79 $e{end} = $e{start} + $e{duration};
80 }
81
82 $e{span} = DateTime::Span->from_datetimes(start => $e{start},
83 end => $e{end});
84
85 $e{summary} = $p->{SUMMARY}->[0]->{value};
86 $e{desc} = $p->{DESCRIPTION}->[0]->{value};
87
88 $e{desc} ||= '';
89
90 $e{summary} =~ s/\\(.)/$1/g; # Backslash escape removal
91 $e{desc} =~ s/\\(.)/$1/g;
92
93 # Handle event recurrences. If there's a recurrence rule (RRULE), then
94 # use that.
95 if(exists $p->{RRULE}) { # $e{recur} = DateTime::Set
96 $e{recur} =
97 DateTime::Format::ICal->parse_recurrence(recurrence => $p->{RRULE}->[0]->{value},
98 dtstart => $e{start});
99 } else {
100 # If the event is a multi-day event then synthesise copies of the
101 # event for every day on which it occurs.
102 #
103 # Why? Suppose you're looking at a 5 day span, and you have an
104 # event that starts on day 2 and finishes on day 4 (3 days in total).
105 # The ->iterator() considers that this is one occurence of the event,
106 # rather than 3. So create recurrences so the iterator behaves in a
107 # more useful fashion.
108 $e{recur} =
109 DateTime::Set->from_recurrence(
110 recurrence => sub {
111 $_[0]->truncate(to => 'day')->add(days => 1);
112 },
113 span => $e{span});
114 $e{recur} =
115 $e{recur}->union(DateTime::Set->from_datetimes(dates => [$e{start}]));
116 }
117
118 # Synthesise a flag that indicates that this is an all-day event. Not
119 # sure if this is the correct way of detecting this, but it works so
120 # far...
121 $e{allday} =
122 exists $p->{DTSTART}->[0]->{param}->{VALUE} and
123 $p->{DTSTART}->[0]->{param}->{VALUE} eq 'DATE' ? 1 : 0;
124
125 # Check to see if it's in the time span we're interested in
126 if($e{recur}->intersects($span)) {
127 my $int_set = $e{recur}->intersection($span);
128
129 # Change the event's recurrence details so that only the events
130 # inside the time span we're interested in are listed.
131 $e{recur} = $int_set;
132 my $iter = $int_set->iterator();
133
134 while(my $dt = $iter->next()) {
135 $e{dt} = $dt;
136
137 # Save a copy of the event (es = event saved), and store it in
138 # the list of event refs keyed off the ICal date
139 my %es = %e;
140 push @{$events{ $dt->ymd() }}, \%es;
141 }
142 }
143 }
144 }
145
146 my $e; # Hash ref of event info
147 my $last_ed = ''; # Last event date we processed
148
149 foreach my $ed (sort keys %events) { # $ed = event date ('yyyy-mm-dd')
150 foreach $e (sort by_type_time @{$events{$ed}}) {
151 # Build the first part of the output line. For the first event of the
152 # day include the date, month, and day abbreviation. For subsequent
153 # events on the same day this is the empty string. This makes for
154 # nicer output, as it's more obvious which events share a day.
155 if($ed ne $last_ed) { # First event of the day
156 $e->{startstring} = sprintf("%2d %s (%s)",
157 $e->{dt}->day(),
158 $e->{dt}->month_abbr(),
159 $e->{dt}->day_abbr());
160 } else { # Second and subsequent events
161 $e->{startstring} = '';
162 }
163
164 $last_ed = $ed;
165
166 # If it's not an all day event then the first part of the summary is
167 # the time the event occurs
168 $e->{summary} = sprintf("%02d:%02d - %02d:%02d, %s",
169 $e->{span}->start()->hour(),
170 $e->{span}->start()->minute(),
171 $e->{span}->end()->hour(),
172 $e->{span}->end()->minute(),
173 $e->{summary})
174 unless $e->{allday};
175
176 # If this is the second or subsequent appearance of a multi-day event,
177 # replace the description (if it exists) with a pointer to the first
178 # occurence of the description
179 if($e->{recur}->min()->ymd() ne $ed and $e->{desc} ne '') {
180 $e->{desc} = sprintf("See %d%s %s entry for details.",
181 $e->{recur}->min()->day(),
182 $inflect[$e->{recur}->min()->day() - 1],
183 $e->{recur}->min()->month_abbr());
184 }
185
186 $e->{summary} = punctuate($e->{summary}) . ' ' .
187 punctuate($e->{desc}) if $e->{desc} ne '';
188
189 write;
190 }
191 }
192
193 exit;
194
195 sub by_type_time { # For sorting lists of events
196 # Two events on the same day? All day events come first
197 return -1 if $a->{allday} and ! $b->{allday};
198 return 1 if $b->{allday} and ! $a->{allday};
199
200 # If they're both all day events, sort by summary text
201 return $a->{summary} cmp $b->{summary} if $a->{allday} and $b->{allday};
202
203 # Otherwise, sort by start time
204 return $a->{dt} <=> $b->{dt};
205 }
206
207 # Try and make sure a string ends with sensible punctuation
208 sub punctuate {
209 my $str = shift;
210 return '' if ! defined $str;
211 $str =~ s/\s*$//g;
212 $str .= '.' unless $str =~ /[\.\?!]$/;
213 return $str;
214 }
215
216 format STDOUT =
217 @<<<<<<<<<<< | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
218 $e->{startstring}, $e->{summary}
219 ~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
220 $e->{summary}
221 ~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
222 $e->{summary}
223 ~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
224 $e->{summary}
225 ~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
226 $e->{summary}
227 ~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
228 $e->{summary}
229 ~ | ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
230 $e->{summary}
231 |
232 .