This repository has been archived on 2025-03-02. You can view files and clone it, but cannot push or open issues or pull requests.
curunir/modules/mensa.py
2010-05-18 09:56:54 +02:00

135 lines
4.8 KiB
Python

# -*- 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'
datestr = day.strftime(r'%d\.%m\.<\/div>')
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):
if not re.search(datestr, data):
return False
match = re.compile(r'^.*' + datestr + r'(.*?)(?:<td rowspan="3" class="schrift_fett"><div>|</table>).*$', re.DOTALL).match(data)
if not match:
return False
data = match.group(1)
r = re.compile(r'.*?<tr[^>]*>(.*?)</tr>', re.DOTALL)
r2 = re.compile(r'(?:.*?<td[^>]*>(.*?)</td>){4}', re.DOTALL)
mealstr = ''
pos = 0
while not r2.match(mealstr):
match = r.match(data, pos)
if not match:
return False
mealstr = match.group(1)
pos = match.end()
match = r.match(data, pos)
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:
meals.append(match.group(2))
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:
prices.append(match.group(1))
if len(prices) >= 4:
break
pos = match.end()
match = r.match(pricestr, pos)
if len(prices) < 4:
return False
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 = 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)
s = re.sub(r'<span[^>]*>(.+?)</span>', r'\1', 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].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())
return reply
reply = handle_data(data)
if not reply:
reply = '%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr
handler.reply(reply.strip())