| 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 'Holidays' ? 'Holidays' : 'http://intranet.fotango.private/index.php?title=Holidays&action=raw'; |
| 10 |
|
| 11 |
=head1 NAME |
| 12 |
|
| 13 |
holidays_ical - scrape the Holidays wiki page into ics |
| 14 |
|
| 15 |
=head1 DESCRIPTION |
| 16 |
|
| 17 |
So this is why I wanted an iCal generator/parser. |
| 18 |
|
| 19 |
We have an intranet, with a page for who's going to be out of the office |
| 20 |
which blech suggested might be good to have available as .ics. |
| 21 |
|
| 22 |
The page is structured roughly like so: |
| 23 |
|
| 24 |
= 2004 = |
| 25 |
|
| 26 |
== February == |
| 27 |
|
| 28 |
* [[Richard Clamp]] |
| 29 |
** 1st - 3rd Baying at the moon |
| 30 |
|
| 31 |
This script scrapes that page and makes it available as a vCalendar. |
| 32 |
It's called as a cgi so you can subscribe to it with iCal.app and |
| 33 |
phpICal. |
| 34 |
|
| 35 |
=head1 AUTHOR |
| 36 |
|
| 37 |
Richard Clamp <[email protected]> |
| 38 |
|
| 39 |
=cut |
| 40 |
|
| 41 |
|
| 42 |
# sometimes, we want to use a url so wrap read_file as get_lines |
| 43 |
sub get_lines { |
| 44 |
my $file = shift; |
| 45 |
if ($file =~ m{^https?://}) { |
| 46 |
# s'really a url |
| 47 |
require LWP::Simple; |
| 48 |
my $content = LWP::Simple::get( $file ); |
| 49 |
return split $/, $content; |
| 50 |
} |
| 51 |
require File::Slurp; |
| 52 |
return File::Slurp::read_file( $file ); |
| 53 |
} |
| 54 |
|
| 55 |
# horrible stateful stuff, but easier than driving Template::Extract |
| 56 |
my ($year, $month, $who); |
| 57 |
|
| 58 |
my %months = ( |
| 59 |
January => 1, February => 2, March => 3, April => 4, May => 5, June => 6, |
| 60 |
July => 7, August => 8, September => 9, October => 10, November => 11, |
| 61 |
December => 12 ); |
| 62 |
|
| 63 |
sub guesstimate_event { |
| 64 |
my $event = shift; |
| 65 |
my $when = $event; |
| 66 |
|
| 67 |
my ($firstday, $lastday); |
| 68 |
if ($when =~ /(\d+)(?:st|nd|rd|th)?\s*(?:to|-)\s*(\d+)/i) { |
| 69 |
#print "From $1 to $2: $event\n"; |
| 70 |
($firstday, $lastday) = ($1, $2); |
| 71 |
} |
| 72 |
elsif ($when =~ /(\d+)/) { |
| 73 |
($firstday, $lastday) = ($1, $1); |
| 74 |
} |
| 75 |
else { |
| 76 |
warn "didn't handle $event"; |
| 77 |
return; |
| 78 |
} |
| 79 |
|
| 80 |
my $start = DateTime->new( |
| 81 |
year => $year, month => $months{ $month }, day => $firstday, |
| 82 |
); |
| 83 |
my $end = DateTime->new( |
| 84 |
year => $year, month => $months{ $month }, day => $lastday |
| 85 |
)->add( |
| 86 |
days => 1, |
| 87 |
( $firstday > $lastday ? ( months => 1 ) : () ) # 28th - 2nd probably means it went over a month |
| 88 |
); |
| 89 |
|
| 90 |
return { |
| 91 |
type => 'VEVENT', |
| 92 |
properties => { |
| 93 |
SUMMARY => [ { value => $who } ], |
| 94 |
DESCRIPTION => [ { value => $event } ], |
| 95 |
DTSTART => [ { value => $start->ymd(''), |
| 96 |
param => { VALUE => 'DATE' }, |
| 97 |
} ], |
| 98 |
DTEND => [ { value => $end->ymd(''), |
| 99 |
param => { VALUE => 'DATE' }, |
| 100 |
} ], |
| 101 |
UID => [ { value => md5_hex( "$year $month $who - $event" ), |
| 102 |
} ], |
| 103 |
}, |
| 104 |
}; |
| 105 |
} |
| 106 |
|
| 107 |
my $cal = { |
| 108 |
type => 'VCALENDAR', |
| 109 |
properties => { |
| 110 |
'X-WR-CALNAME' => [ { value => "Fotango Holidays" } ], |
| 111 |
}, |
| 112 |
objects => [], |
| 113 |
}; |
| 114 |
for (get_lines( $file )) { |
| 115 |
next if /^\s*$/; |
| 116 |
/^= (.*) =/ and do { $year = $1; next }; |
| 117 |
/^== (.*) ==/ and do { $month = $1; next }; |
| 118 |
/^\* \[\[(.*)\]\]/ and do { $who = $1; next }; |
| 119 |
/^\*\* (.*)/ and do { |
| 120 |
#print "year = $year, month = $month, event = $1\n"; |
| 121 |
push @{ $cal->{objects} }, guesstimate_event( $1 ); |
| 122 |
next; |
| 123 |
}; |
| 124 |
# warn "unhandled line: $_"; |
| 125 |
} |
| 126 |
|
| 127 |
print CGI->header('text/calendar'); |
| 128 |
print map "$_\n", Text::vFile::asData->generate_lines( $cal ); |