diff options
-rwxr-xr-x | bin/events2md.pl | 61 |
1 files changed, 46 insertions, 15 deletions
diff --git a/bin/events2md.pl b/bin/events2md.pl index 05081d1..4019420 100755 --- a/bin/events2md.pl +++ b/bin/events2md.pl @@ -44,6 +44,45 @@ $CALENDAR_URI ||= shift @ARGV $OUTPUT_FILE = shift @ARGV if @ARGV; +class Calendar { + field $log = Log::Any->get_logger; + + # borrow from Data::ICal::new() signature + field $data : param = undef; + field $filename : param = undef; + + ADJUST { + if ($data) { + if ( $data isa Data::ICal ) { } + else { $data = Data::ICal->new( data => $data ) } + } + elsif ($filename) { $data = Data::ICal->new( filename => $filename ) } + + if ( $log->is_trace ) { + use DDP; + p $data; + } + } + + # mimick Data::ICal::DateTime::events() signature + method events ( $set = undef, $period = undef ) + { + $log->infof( + 'will pick events between %s and %s', + $set->start, $set->end + ) if $set; + + my @events = sort { + DateTime->compare( $a->begin, $b->begin ) + || DateTime->compare( $a->end, $b->end ) + || $a->summary cmp $b->summary + } map { Event->new( entry => $_ ) } + $data->events( $set || (), $period || () ); + + return @events; + } +} + class Event { use Encode qw(decode_utf8); # TODO: modernize CalDAV access instead @@ -175,13 +214,14 @@ if ( $base_uri->scheme eq 'http' or $base_uri->scheme eq 'https' ) { # fetch and parse CalDAV calendar data $log->debug('fetch and parse CalDAV calendar data...'); - $calendar = Cal::DAV->new( + my $session = Cal::DAV->new( user => $user, pass => $pass, url => $base_uri, ); - $calendar->get($calendar_uri) + $session->get($calendar_uri) if $calendar_uri; + $calendar = Calendar->new( data => $session->cal ); } elsif ( $base_uri->scheme eq 'file' ) { defined $base_uri->file @@ -192,12 +232,12 @@ elsif ( $base_uri->scheme eq 'file' ) { $log->debug('parse local calendar data...'); my $path = path( $base_uri->file ); if ( $path->is_file ) { - $calendar = Data::ICal->new( data => $path->slurp_raw ); + $calendar = Calendar->new( filename => "$path" ); } else { my $data; $path->visit( sub { $data .= $_->slurp_raw if $_->is_file } ); - $calendar = Data::ICal->new( data => $data ); + $calendar = Calendar->new( data => $data ); } } if ( $log->is_trace ) { @@ -229,18 +269,9 @@ if ( $ENV{CAL_DAV_NOW} ) { unless defined $start; } $start ||= DateTime->now; -my $end = $start->clone->add( months => 6 ); -$log->infof( 'will pick events between %s and %s', $start, $end ); +my $end = $start->clone->add( months => 6 ); my $span = DateTime::Span->from_datetimes( start => $start, end => $end ); -my @events = sort { - DateTime->compare( $a->begin, $b->begin ) - || DateTime->compare( $a->end, $b->end ) - || $a->summary cmp $b->summary -} map { Event->new( entry => $_ ) } $calendar->events($span); -if ( $log->is_trace ) { - use DDP; - p @events; -} +my @events = $calendar->events($span); # serialize calendar view my $output_path; |