# -*- coding: utf-8 -*-
from . import ModuleBase
import re
from datetime import datetime, date, timedelta, time
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 or datetime.now().time() > time(15):
day += timedelta(1)
daystr = 'Morgen'
try:
f = urllib.urlopen(self.manager.config['mensa_url'], 'r')
data = f.read()
f.close()
except IOError:
handler.reply('Hmm, es scheint gerade keine Daten über das Mensa-Menü zu geben.')
return
def handle_data(data):
r = re.compile(r'.*?
]*>(.*?)
.*?]*>(.*?)
', re.DOTALL|re.IGNORECASE)
r2 = re.compile(r'.*?]*>(.*?) | ', re.DOTALL|re.IGNORECASE)
def handle_data_row(data, pos):
match = r.match(data, pos)
if not match:
return False
ret = []
mealpos = 0
pricepos = 0
for day in range(0,5):
mealmatch = r2.match(match.group(1), mealpos)
pricematch = r2.match(match.group(2), pricepos)
if not mealmatch or not pricematch:
return False
ret.append((mealmatch.group(1), pricematch.group(1)))
mealpos = mealmatch.end()
pricepos = pricematch.end()
return (ret, match.end())
match = re.compile(r'^.*?]*>.*?]*>.*?(\d+)\.(\d+)\. ?- ?\d+\.\d+\.(\d+).*? | .*?]*>Freitag | \s*(.*?)
.*$', re.DOTALL|re.IGNORECASE).match(data)
if not match:
return False
firstday = datetime.strptime('%s-%s-%s' % (match.group(3), match.group(2), match.group(1)), '%Y-%m-%d').date()
weekday = (day - firstday).days
if weekday < 0 or weekday > 4:
return False
data = match.group(4)
meals = []
pos = 0
for i in range(0,6):
row = handle_data_row(data, pos)
if not row:
break
meals.append(row[0])
pos = row[1]
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 = s.replace('–', '–')
s = s.replace('"', '"')
s = s.replace('<', '<')
s = s.replace('>', '>')
s = s.replace('&', '&')
s = s.replace('
', ' ')
s = re.sub(r'-\s+', '-', s)
s = re.sub(r'\s+', ' ', s)
s = re.sub(r'([a-zäöüß])-([a-zäöüß])', r'\1\2', s)
s = re.sub(r']*logo_bio[^>]*>', 'Bio', s)
s = re.sub(r'<[^>]*>', '', s)
return s
stripunescape = (lambda item: (unescape(item[0].strip()), unescape(item[1].strip())))
meals = map(lambda item: map(stripunescape, item), meals)
if len(meals) == 0:
return False
while len(meals) < 6:
meals.insert(len(meals)-1, [[None], [None], [None], [None], [None]])
reply = 'Mensa-Menü für den %s\n\n' % day.strftime('%d.%m.%y')
if meals[0][weekday][0]:
reply += 'Eintopf: %s (%s)\n' % meals[0][weekday]
if meals[1][weekday][0]:
reply += 'Hauptgericht 1: %s (%s)\n' % meals[1][weekday]
if meals[3][weekday][0]:
reply += 'Hauptgericht 2: %s (%s)\n' % meals[3][weekday]
if meals[2][weekday][0]:
reply += 'Vegetarisches Hauptgericht: %s (%s)\n' % meals[2][weekday]
if meals[4][weekday][0]:
reply += 'Wok: %s (%s)\n' % meals[4][weekday]
if meals[5][weekday][0]:
reply += 'Beilagen: %s (%s)\n' % meals[5][weekday]
return reply
reply = handle_data(data)
if not reply:
reply = '%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr
handler.reply(reply.strip())