diff options
Diffstat (limited to 'modules')
-rw-r--r-- | modules/mensa.py | 115 |
1 files changed, 115 insertions, 0 deletions
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(' ', '') + 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()) |