diff options
author | Jonas Smedegaard <dr@jones.dk> | 2021-05-20 21:41:05 +0200 |
---|---|---|
committer | Jonas Smedegaard <dr@jones.dk> | 2021-05-20 21:41:05 +0200 |
commit | 7fb094cf46779ee707f7ae71b8ab3b92ff64413a (patch) | |
tree | 6ce401e25a8e6d621eb2629d9d7451ae5ea9cca7 | |
parent | 26fd1adbbd67b0cfe547db0ea77dbdfb8a1e2eb6 (diff) |
support passing local file/dir URI instead of CalDAV URIs
-rwxr-xr-x | bin/events2md.pl | 25 |
1 files changed, 20 insertions, 5 deletions
diff --git a/bin/events2md.pl b/bin/events2md.pl index 7fd8a5e..9423133 100755 --- a/bin/events2md.pl +++ b/bin/events2md.pl @@ -19,6 +19,7 @@ use Log::Any::Adapter; use URI; use IO::Prompter; use Cal::DAV; +use Data::ICal; use iCal::Parser; use List::Util qw(first); use List::MoreUtils qw(nsort_by sort_by); @@ -51,7 +52,7 @@ $base_uri = URI->new($BASE_URI) $base_uri or $log->fatal('required base URI not provided') && exit 2; $base_uri->scheme - or $log->fatal('bad base URI: must be an internet URI') && exit 2; + or $base_uri->scheme('file'); if ( $base_uri->scheme eq 'http' or $base_uri->scheme eq 'https' ) { $log->infof( 'will use base URI %s', $base_uri ); $calendar_uri = URI->new( $CALENDAR_URI || $base_uri ); @@ -81,8 +82,8 @@ if ( $base_uri->scheme eq 'http' or $base_uri->scheme eq 'https' ) { } $log->debugf( 'resolved credentials for user %s', $user ); - # fetch and parse calendar data - $log->debug('fetch and parse calendar data...'); + # fetch and parse CalDAV calendar data + $log->debug('fetch and parse CalDAV calendar data...'); $calendar = Cal::DAV->new( user => $user, pass => $pass, @@ -91,8 +92,22 @@ if ( $base_uri->scheme eq 'http' or $base_uri->scheme eq 'https' ) { $calendar->get($calendar_uri) if $calendar_uri; } -else { - $log->fatal('bad base URI: must be an internet URI') && exit 2; +elsif ( $base_uri->scheme eq 'file' ) { + defined $base_uri->file + or $log->fatal('bad base URI: cannot open file') && exit 2; + $log->infof( 'will use base URI %s', $base_uri ); + + # parse local calendar data + $log->debug('parse local calendar data...'); + my $path = path( $base_uri->file ); + if ( $path->is_file ) { + $calendar = Data::ICal->new( data => $path->slurp_raw ); + } + else { + my $data; + $path->visit( sub { $data .= $_->slurp_raw if $_->is_file } ); + $calendar = Data::ICal->new( data => $data ); + } } if ( $log->is_trace ) { use DDP; |