| 1 |
#!/usr/bin/perl |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
use Text::vFile::asData; |
| 5 |
use Digest::MD5 qw(md5_hex); |
| 6 |
use DateTime; |
| 7 |
use CGI; |
| 8 |
|
| 9 |
my $file = -e 'SystemsSupportRota' ? 'SystemsSupportRota' : 'http://intranet.fotango.private/index.php?title=Systems_Support_Rota&action=raw'; |
| 10 |
|
| 11 |
=head1 NAME |
| 12 |
|
| 13 |
rota_ical.cgi - scrape the SystemsSupportRota wiki page into ics |
| 14 |
|
| 15 |
=head1 DESCRIPTION |
| 16 |
|
| 17 |
=cut |
| 18 |
|
| 19 |
my @tasks; |
| 20 |
my %rrule; |
| 21 |
sub make_events { |
| 22 |
my $when = shift; |
| 23 |
my %who; |
| 24 |
@who{ @tasks } = @_; |
| 25 |
|
| 26 |
my $year = DateTime->now->year; |
| 27 |
return unless $when =~ m{(\S+)\s+(\d+)\s*/\s*(\d+)}; |
| 28 |
my ($weekday, $day, $month) = ($1, $2, $3); |
| 29 |
|
| 30 |
my $start = DateTime->new( year => $year, month => $month, day => $day ); |
| 31 |
my $end = $start->clone->add( days => 1 ); |
| 32 |
|
| 33 |
my $is_rrule; |
| 34 |
if ($when =~ /From/) { |
| 35 |
# establish an rrule for that weekday |
| 36 |
push @{ $rrule{$weekday} }, { |
| 37 |
start => $start->epoch, |
| 38 |
map { $_ => md5_hex( "$when $who{$_} $_" ) } @tasks, |
| 39 |
}; |
| 40 |
$is_rrule = 1; |
| 41 |
} |
| 42 |
|
| 43 |
# find the latest rrule that covers this date |
| 44 |
my ($rrule) = grep { $start->epoch >= $_->{start} } sort { $a->{start} <=> $b->{start} } @{ $rrule{ $weekday } || [] }; |
| 45 |
|
| 46 |
return map +{ |
| 47 |
type => 'VEVENT', |
| 48 |
properties => { |
| 49 |
SUMMARY => [ { value => "$who{$_} $_" } ], |
| 50 |
DESCRIPTION => [ { value => "$when $who{$_} $_" } ], |
| 51 |
DTSTART => [ { value => $start->ymd(''), |
| 52 |
param => { VALUE => 'DATE' }, |
| 53 |
} ], |
| 54 |
DTEND => [ { value => $end->ymd(''), |
| 55 |
param => { VALUE => 'DATE' }, |
| 56 |
} ], |
| 57 |
UID => [ { value => $rrule ? $rrule->{ $_ } : md5_hex( "$when $who{$_} $_" ) } ], |
| 58 |
($is_rrule ? ( RRULE => [ { value => "FREQ=WEEKLY;INTERVAL=1" } ] ) |
| 59 |
: $rrule ? ( 'RECURRENCE-ID' => [ { param => { VALUE => "DATE" }, value => $start->ymd('') } ] ) : ()), |
| 60 |
}, |
| 61 |
}, sort grep { $who{$_} } @tasks; |
| 62 |
} |
| 63 |
|
| 64 |
my $cal = { |
| 65 |
type => 'VCALENDAR', |
| 66 |
properties => { |
| 67 |
'X-WR-CALNAME' => [ { value => "Systems Rota" } ], |
| 68 |
}, |
| 69 |
objects => [], |
| 70 |
}; |
| 71 |
|
| 72 |
# sometimes, we want to use a url so wrap read_file as get_lines |
| 73 |
sub get_lines { |
| 74 |
my $file = shift; |
| 75 |
if ($file =~ m{^https?://}) { |
| 76 |
# s'really a url |
| 77 |
require LWP::Simple; |
| 78 |
my $content = LWP::Simple::get( $file ); |
| 79 |
return split $/, $content; |
| 80 |
} |
| 81 |
require File::Slurp; |
| 82 |
return File::Slurp::read_file( $file ); |
| 83 |
} |
| 84 |
|
| 85 |
sub parse_tables { |
| 86 |
my (@rows, @row, $in_table); |
| 87 |
for (@_) { |
| 88 |
chomp; |
| 89 |
/^{\|/ and do { $in_table = 1; next }; |
| 90 |
/^\|}/ and do { $in_table = 0; next }; |
| 91 |
next unless $in_table; |
| 92 |
/^\|-/ and do { |
| 93 |
push @rows, [@row] if @row; |
| 94 |
@row = (); |
| 95 |
next; |
| 96 |
}; |
| 97 |
/^\| (.*)/ and do { |
| 98 |
push @row, $1; |
| 99 |
next; |
| 100 |
}; |
| 101 |
print "Didn't expect: $_\n"; |
| 102 |
} |
| 103 |
return @rows, (@row ? [@row] : ()); |
| 104 |
} |
| 105 |
|
| 106 |
for (parse_tables( get_lines( $file ) )) { |
| 107 |
$_->[0] =~ /Date/ and do { |
| 108 |
(undef, @tasks) = @$_; |
| 109 |
next; |
| 110 |
}; |
| 111 |
push @{ $cal->{objects} }, make_events( @$_ ); |
| 112 |
} |
| 113 |
|
| 114 |
print CGI->header('text/calendar'); |
| 115 |
print map "$_\n", Text::vFile::asData->generate_lines( $cal ); |