From 05176460b7da9817d0ac5d6c4ecb23e165ea5ae7 Mon Sep 17 00:00:00 2001
From: Andrew Ruthven <andrew@etc.gen.nz>
Date: Fri, 4 Nov 2016 23:10:56 +1300
Subject: [PATCH] Take a Free/Busy feed and generate an iCal file

---
 calendar-busy.pl | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 48 insertions(+)
 create mode 100755 calendar-busy.pl

diff --git a/calendar-busy.pl b/calendar-busy.pl
new file mode 100755
index 0000000..eda87d7
--- /dev/null
+++ b/calendar-busy.pl
@@ -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;
+
+
-- 
2.30.2