summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--connection/xmpp.py2
-rw-r--r--modules/mensa.py115
2 files changed, 116 insertions, 1 deletions
diff --git a/connection/xmpp.py b/connection/xmpp.py
index dfdeb28..454f3df 100644
--- a/connection/xmpp.py
+++ b/connection/xmpp.py
@@ -4,7 +4,7 @@ from pyxmpp.client import Client
from pyxmpp.jabber.muc import *
from pyxmpp.interface import implements
from pyxmpp.interfaces import *
-from functools import partial
+
class MessageHandler:
implements(IMessageHandlersProvider)
diff --git a/modules/mensa.py b/modules/mensa.py
new file mode 100644
index 0000000..55fa664
--- /dev/null
+++ b/modules/mensa.py
@@ -0,0 +1,115 @@
+# -*- coding: utf-8 -*-
+
+from base import ModuleBase
+import re
+from datetime import date, timedelta
+import urllib
+
+class Module(ModuleBase):
+ def __init__(self, manager):
+ ModuleBase.__init__(self, manager)
+
+ def commands(self):
+ return [('!mensa [tomorrow]', 'Zeigt das aktuelle Menü der Mensa der Uni Lübeck an (oder das des nächsten Tages)')]
+
+ def groupchat(self, room, nick, text, handler):
+ if not re.match(r'!mensa(?:\W|\Z)', text):
+ return
+
+ tomorrow = re.match(r'!mensa\W+tomorrow(\W|\Z)', text)
+ daystr = 'heute'
+ day = date.today()
+ if tomorrow:
+ day += timedelta(1)
+ daystr = 'morgen'
+ datestr = day.strftime('%d.%m.</div>')
+
+ try:
+ f = urllib.urlopen(self.manager.config['mensa_url'], 'r')
+ except IOError:
+ handler.reply('Hmm, es scheint gerade keine Daten über das Mensa-Menü zu geben.')
+ return
+
+ found = False
+ line = f.readline()
+ while line:
+ if re.search(datestr, line):
+ found = True
+ break
+ line = f.readline()
+
+ if not found:
+ f.close()
+ handler.reply('%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr)
+ return
+
+ meals = []
+ r = re.compile(r'<td class="schrift_gerichte">([^<]*)</td>')
+ line = f.readline()
+ while line:
+ match = r.search(line)
+ if match:
+ meals.append(match.group(1))
+
+ if len(meals) >= 4:
+ break
+
+ if re.search('<td rowspan="3" class="schrift_fett"><div>', line):
+ break
+
+ line = f.readline()
+
+ prices = []
+ r = re.compile(r'<td class="schrift_preise">([^<]*)</td>')
+ line = f.readline()
+ while line:
+ match = r.search(line)
+ if match:
+ prices.append(match.group(1))
+
+ if len(prices) >= 4:
+ break
+
+ if re.search('<td rowspan="3" class="schrift_fett"><div>', line):
+ break
+
+ line = f.readline()
+
+ f.close()
+
+ if len(meals) < 4 or len(prices) < 4:
+ handler.reply('%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr)
+ return
+
+ def unescape(s):
+ s = s.replace('&nbsp;', '')
+ s = s.replace('&euro;', '€')
+ s = s.replace('&auml;', 'ä')
+ s = s.replace('&Auml;', 'Ä')
+ s = s.replace('&ouml;', 'ö')
+ s = s.replace('&Ouml;', 'Ö')
+ s = s.replace('&uuml;', 'ü')
+ s = s.replace('&Uuml;', 'Ü')
+ s = s.replace('&szlig;', 'ß')
+ s = s.replace('&amp;', '&')
+ s = s.replace('&lt;', '<')
+ s = s.replace('&gt;', '>')
+ s = s.replace('&quot;', '"')
+ s = re.sub('\s+', ' ', s)
+ return s
+
+ meals = map(unescape, meals)
+ prices = map(unescape, prices)
+
+ reply = 'Mensa-Menü für den %s\n\n' % day.strftime('%d.%m.%y')
+
+ if meals[0]:
+ reply += 'Eintopf: %s (%s)\n' % (meals[0], prices[0])
+ if meals[1]:
+ reply += 'Hauptgericht 1: %s (%s)\n' % (meals[1], prices[1])
+ if meals[2]:
+ reply += 'Hauptgericht 2: %s (%s)\n' % (meals[2], prices[2])
+ if meals[3]:
+ reply += 'Vegetarisches Hauptgericht: %s (%s)\n' % (meals[3], prices[3])
+
+ handler.reply(reply.strip())