2010-05-03 05:20:21 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2010-05-09 21:49:15 +02:00
|
|
|
from . import ModuleBase
|
2010-05-03 05:20:21 +02:00
|
|
|
import re
|
2010-05-06 22:32:16 +02:00
|
|
|
from datetime import datetime, date, timedelta, time
|
2010-05-03 05:20:21 +02:00
|
|
|
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)
|
2010-05-03 17:01:05 +02:00
|
|
|
daystr = 'Heute'
|
2010-05-03 05:20:21 +02:00
|
|
|
day = date.today()
|
2010-05-06 22:32:16 +02:00
|
|
|
if tomorrow or datetime.now().time() > time(15):
|
2010-05-03 05:20:21 +02:00
|
|
|
day += timedelta(1)
|
2010-05-03 17:01:05 +02:00
|
|
|
daystr = 'Morgen'
|
|
|
|
datestr = day.strftime(r'%d\.%m\.<\/div>')
|
2010-05-03 05:20:21 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
f = urllib.urlopen(self.manager.config['mensa_url'], 'r')
|
2010-05-03 17:01:05 +02:00
|
|
|
data = f.read()
|
|
|
|
f.close()
|
2010-05-03 05:20:21 +02:00
|
|
|
except IOError:
|
|
|
|
handler.reply('Hmm, es scheint gerade keine Daten über das Mensa-Menü zu geben.')
|
|
|
|
return
|
|
|
|
|
2010-05-03 17:01:05 +02:00
|
|
|
def handle_data(data):
|
|
|
|
if not re.search(datestr, data):
|
|
|
|
return False
|
2010-05-03 05:20:21 +02:00
|
|
|
|
2010-05-04 15:14:31 +02:00
|
|
|
match = re.compile(r'^.*' + datestr + r'(.*?)(?:<td rowspan="3" class="schrift_fett"><div>|</table>).*$', re.DOTALL).match(data)
|
2010-05-03 05:20:21 +02:00
|
|
|
|
2010-05-04 15:14:31 +02:00
|
|
|
if not match:
|
2010-05-03 17:01:05 +02:00
|
|
|
return False
|
2010-05-03 05:20:21 +02:00
|
|
|
|
2010-05-04 15:14:31 +02:00
|
|
|
data = match.group(1)
|
|
|
|
|
2010-05-03 17:01:05 +02:00
|
|
|
r = re.compile(r'.*?<tr[^>]*>(.*?)</tr>', re.DOTALL)
|
|
|
|
match = r.match(data)
|
|
|
|
if not match:
|
|
|
|
return False
|
|
|
|
mealstr = match.group(1)
|
|
|
|
|
|
|
|
match = r.match(data, match.end())
|
|
|
|
if not match:
|
|
|
|
return False
|
|
|
|
pricestr = match.group(1)
|
|
|
|
|
|
|
|
meals = []
|
|
|
|
pos = 0
|
|
|
|
r = re.compile(r'\s*<td class="schrift_(gerichte|spezial)">(.+?)</td>', re.DOTALL)
|
|
|
|
match = r.match(mealstr, pos)
|
|
|
|
while match:
|
2010-05-09 08:10:42 +02:00
|
|
|
meals.append(match.group(2))
|
2010-05-03 17:01:05 +02:00
|
|
|
|
|
|
|
if len(meals) >= 4:
|
|
|
|
break
|
|
|
|
|
|
|
|
pos = match.end()
|
|
|
|
match = r.match(mealstr, pos)
|
|
|
|
|
|
|
|
if len(meals) < 4:
|
|
|
|
return False
|
|
|
|
|
|
|
|
prices = []
|
|
|
|
pos = 0
|
|
|
|
r = re.compile(r'\s*<td class="schrift_(?:preise|spezial)">(.+?)</td>', re.DOTALL)
|
|
|
|
match = r.match(pricestr, pos)
|
|
|
|
while match:
|
2010-05-03 05:20:21 +02:00
|
|
|
prices.append(match.group(1))
|
2010-05-03 17:01:05 +02:00
|
|
|
|
|
|
|
if len(prices) >= 4:
|
|
|
|
break
|
|
|
|
|
|
|
|
pos = match.end()
|
|
|
|
match = r.match(pricestr, pos)
|
2010-05-03 05:20:21 +02:00
|
|
|
|
2010-05-03 17:01:05 +02:00
|
|
|
if len(prices) < 4:
|
|
|
|
return False
|
2010-05-03 05:20:21 +02:00
|
|
|
|
2010-05-03 17:01:05 +02:00
|
|
|
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('"', '"')
|
2010-05-11 21:38:45 +02:00
|
|
|
s = s.replace('<br />', ' ')
|
|
|
|
s = re.sub(r'-\s+', '-', s)
|
|
|
|
s = re.sub(r'\s+', ' ', s)
|
|
|
|
s = re.sub(r'([a-zäöüß])-([a-zäöüß])', r'\1\2', s)
|
2010-05-09 08:10:42 +02:00
|
|
|
s = re.sub(r'<span[^>]*>(.+?)</span>', r'\1', s)
|
2010-05-03 17:01:05 +02:00
|
|
|
return s
|
2010-05-03 05:20:21 +02:00
|
|
|
|
2010-05-09 08:10:42 +02:00
|
|
|
meals = map(unescape, meals)
|
2010-05-03 17:01:05 +02:00
|
|
|
prices = map(unescape, prices)
|
|
|
|
|
|
|
|
reply = 'Mensa-Menü für den %s\n\n' % day.strftime('%d.%m.%y')
|
2010-05-09 08:10:42 +02:00
|
|
|
if meals[0]:
|
|
|
|
reply += 'Eintopf: %s (%s)\n' % (meals[0].strip(), prices[0].strip())
|
|
|
|
if meals[1]:
|
|
|
|
reply += 'Hauptgericht 1: %s (%s)\n' % (meals[1].strip(), prices[1].strip())
|
|
|
|
if meals[2]:
|
|
|
|
reply += 'Hauptgericht 2: %s (%s)\n' % (meals[2].strip(), prices[2].strip())
|
|
|
|
if meals[3]:
|
|
|
|
reply += 'Vegetarisches Hauptgericht: %s (%s)\n' % (meals[3].strip(), prices[3].strip())
|
2010-05-03 17:01:05 +02:00
|
|
|
|
|
|
|
return reply
|
2010-05-03 05:20:21 +02:00
|
|
|
|
2010-05-03 17:01:05 +02:00
|
|
|
reply = handle_data(data)
|
|
|
|
if not reply:
|
|
|
|
reply = '%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr
|
2010-05-03 05:20:21 +02:00
|
|
|
|
|
|
|
handler.reply(reply.strip())
|