]> git.etc.gen.nz Git - calendar-busy.git/commitdiff
Take a Free/Busy feed and generate an iCal file
authorAndrew Ruthven <andrew@etc.gen.nz>
Fri, 4 Nov 2016 10:10:56 +0000 (23:10 +1300)
committerAndrew Ruthven <andrew@etc.gen.nz>
Fri, 4 Nov 2016 10:10:56 +0000 (23:10 +1300)
calendar-busy.pl [new file with mode: 0755]

diff --git a/calendar-busy.pl b/calendar-busy.pl
new file mode 100755 (executable)
index 0000000..eda87d7
--- /dev/null
@@ -0,0 +1,48 @@
+#!/usr/bin/perl -w
+
+use v5.10;
+use LWP;
+use LWP::UserAgent;
+use Data::ICal;
+use Data::ICal::Entry::Event;
+
+
+my $url = 'http://calendar.etc.gen.nz/freebusy.php/susanne';
+
+my $ua = LWP::UserAgent->new();
+
+$ua->default_header('Accept'          => 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5');
+$ua->default_header('Accept-Language' => 'en-us,en;q=0.5');
+$ua->default_header('Accept-Charset'  => 'ISO-8859-1,utf-8;q=0.7,*;q=0.7');
+$ua->default_header('Accept-Encoding' => 'gzip,deflate');
+$ua->default_header('Content-Type'    => 'text/xml');
+
+my $request  = HTTP::Request->new('GET', $url, undef);
+my $response = $ua->request($request);
+
+die "Failed to get response from server: " . $response->status_line
+    unless $response->is_success;
+
+my $freebusy = $response->decoded_content;
+
+my $calendar = Data::ICal->new();
+
+for my $line (split(/\n/, $freebusy)) {
+    next unless $line =~ /^FREEBUSY/;
+
+    
+    (my $start, $end) = $line =~ /^FREEBUSY:([0-9TZ]+)\/([0-9TZ]+)/;
+    my $event = Data::ICal::Entry::Event->new();
+    $event->add_properties(
+        summary => 'Busy',
+        dtstart => $start,
+        dtend   => $end,
+    );
+
+    $calendar->add_entry($event);
+    $count++;
+}
+
+say $calendar->as_string;
+
+