Adding an initial calendar implementation.
authorFredrik Unger <fred@tree.se>
Fri, 26 Oct 2012 07:05:13 +0000 (09:05 +0200)
committerFredrik Unger <fred@tree.se>
Fri, 26 Oct 2012 07:05:13 +0000 (09:05 +0200)
This calendar was developed in another git to begin with.
It worked for displaying a calendar from apple calendar server.
This will be a base for a rewrite.

xinclude/calendar.py [new file with mode: 0755]

diff --git a/xinclude/calendar.py b/xinclude/calendar.py
new file mode 100755 (executable)
index 0000000..a956ba0
--- /dev/null
@@ -0,0 +1,79 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import getpass
+from dateutil.tz import *; from datetime import *
+import vobject
+import httplib
+import urlparse
+from lxml import etree
+import sys
+import re
+
+pw = getpass.getpass()
+urlstr = "CALDAV ULR"
+url = urlparse.urlparse(urlstr)
+headers = {"User-Agent": "Mozilla/5.0",
+                        "Content-Type": "text/xml",
+                        "Accept": "text/xml"}
+
+headers['authorization'] = "Basic %s" % (("%s:%s" % (username, pw)).encode('base64')[:-1])
+handle = httplib.HTTPSConnection('tree.se','8443')
+res = handle.request('GET', 'CALENDAR PATH', "", headers)
+r = handle.getresponse()
+if r.status != 200:
+  print "Failed to connect! Wrong Password ?"
+  sys.exit(5)
+s = r.read()
+events = []
+headers = ['dtstart','summary','location','description']
+for cal in vobject.readComponents(s):
+  for ev in cal.vevent_list:
+    details = {}
+    for k in headers:
+      details[k] = ""
+    for p in ev.getChildren():
+      details[p.name.lower()] = p.value
+    events.append(details)
+handle.close()
+
+sortedevents = sorted(events, key=lambda k: k['dtstart'], reverse=True)
+output = u'''
+<variablelist role="calendar" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
+<title>%s</title>''' % (u'Stammtisch träffar')
+for ev in sortedevents:
+    loc = ev['location'].split('[')[0]
+    if ev['location'].find('['):
+        lat = ev['location'].split('[')[1].split(',')[0]
+        lon = ev['location'].split('[')[1].split(',')[1].replace(']','')
+        loc = ' <link xlink:href="http://openstreetmap.org/?mlat=%s&amp;mlon=%s&amp;zoom=17">%s</link>' % (lat,lon,loc)
+    output +=  u'''<varlistentry>
+<term>
+<date role="calendar">%s %s %s</date>
+</term>
+<listitem>
+<para>%s</para>
+<variablelist>
+<varlistentry>
+  <term>Tid</term>
+  <listitem><para>%s</para></listitem>
+</varlistentry>
+<varlistentry>
+  <term>Plats</term>
+  <listitem><para>%s</para></listitem>
+</varlistentry>
+<varlistentry>
+  <term>Beskrivning</term>
+  <listitem><para role="desc">%s</para></listitem>
+</varlistentry>
+</variablelist>
+</listitem>
+</varlistentry>''' % (ev['dtstart'].strftime('%Y'),
+                      ev['dtstart'].strftime('%b'),
+                      ev['dtstart'].strftime('%d'),
+                      ev['summary'],
+                      ev['dtstart'].strftime('%H:%M'),
+                      loc,
+                      '</para><para>'.join(re.split('\n\n',unicode(ev['description']))))
+output += u'</variablelist>'
+print output.encode("utf-8")