From 78703620d3c72921ae33abeb6534ba90aaaed06d Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 3 May 2010 05:20:21 +0200 Subject: Added mensa module --- connection/xmpp.py | 2 +- modules/mensa.py | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+), 1 deletion(-) create mode 100644 modules/mensa.py 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.') + + 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'([^<]*)') + line = f.readline() + while line: + match = r.search(line) + if match: + meals.append(match.group(1)) + + if len(meals) >= 4: + break + + if re.search('
', line): + break + + line = f.readline() + + prices = [] + r = re.compile(r'([^<]*)') + line = f.readline() + while line: + match = r.search(line) + if match: + prices.append(match.group(1)) + + if len(prices) >= 4: + break + + if re.search('
', 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(' ', '') + s = s.replace('€', '€') + s = s.replace('ä', 'ä') + s = s.replace('Ä', 'Ä') + s = s.replace('ö', 'ö') + s = s.replace('Ö', 'Ö') + s = s.replace('ü', 'ü') + s = s.replace('Ü', 'Ü') + s = s.replace('ß', 'ß') + s = s.replace('&', '&') + s = s.replace('<', '<') + s = s.replace('>', '>') + s = s.replace('"', '"') + 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()) -- cgit v1.2.3