diff options
author | Jonas Smedegaard <dr@jones.dk> | 2024-08-28 15:05:05 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2024-08-28 17:11:34 +0200 |
commit | a2692bc40e727588c441d80bbb24712492b9c7ff (patch) | |
tree | 21b5cc3f2196e656c60734eb376432f38b99ea66 /bin | |
parent | 70f0d43e281808332444d4d490034cf0bd892ff6 (diff) |
define class Calendar
Diffstat (limited to 'bin')
-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; |