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

137 lines
4.9 KiB
Python
Raw Normal View History

2010-05-03 05:20:21 +02:00
# -*- coding: utf-8 -*-
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)
2010-10-31 23:01:21 +01:00
2010-05-03 05:20:21 +02:00
def commands(self):
return [('!mensa [tomorrow]', 'Zeigt das aktuelle Menü der Mensa der Uni Lübeck an (oder das des nächsten Tages)')]
2010-10-31 23:01:21 +01:00
2010-05-03 05:20:21 +02:00
def groupchat(self, room, nick, text, handler):
if not re.match(r'!mensa(?:\W|\Z)', text):
return
2010-10-31 23:01:21 +01:00
2010-05-03 05:20:21 +02:00
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'
2010-10-31 23:01:21 +01:00
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-10-31 23:01:21 +01:00
2010-05-03 17:01:05 +02:00
def handle_data(data):
2011-02-08 23:44:06 +01:00
r = re.compile(r'.*?<tr[^>]*>(.*?)</tr>.*?<tr[^>]*>(.*?)</tr>', re.DOTALL|re.IGNORECASE)
r2 = re.compile(r'.*?<td[^>]*>(.*?)</td>', re.DOTALL|re.IGNORECASE)
2010-10-31 23:01:21 +01:00
def handle_data_row(data, pos):
match = r.match(data, pos)
2010-10-31 23:01:21 +01:00
if not match:
return False
2010-10-31 23:01:21 +01:00
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())
2011-02-08 23:44:06 +01:00
match = re.compile(r'^.*?<table[^>]*>.*?<td[^>]*>.*?(\d+)\.(\d+)\. - \d+\.\d+\.(\d+).*?</td>.*?<td[^>]*>Freitag</td>\s*</tr>(.*?)</table>.*$', re.DOTALL|re.IGNORECASE).match(data)
2010-10-31 23:01:21 +01:00
2010-05-03 17:01:05 +02:00
if not match:
return False
2010-10-31 23:01:21 +01:00
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:
2010-05-03 17:01:05 +02:00
return False
2010-10-31 23:01:21 +01:00
data = match.group(4)
meals = []
2010-05-03 17:01:05 +02:00
pos = 0
2010-10-31 23:01:21 +01:00
2011-02-14 02:02:00 +01:00
for i in range(0,5):
2010-10-31 23:01:21 +01:00
row = handle_data_row(data, pos)
if not row:
return False
meals.append(row[0])
pos = row[1]
2010-05-03 17:01:05 +02:00
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;', 'ß')
2010-10-27 11:37:31 +02:00
s = s.replace('&eacute;', 'é')
2010-05-19 19:24:10 +02:00
s = s.replace('&ldquo;', '')
2010-05-25 14:26:53 +02:00
s = s.replace('&bdquo;', '')
2010-07-07 21:18:56 +02:00
s = s.replace('&ndash;', '')
2010-10-27 11:37:31 +02:00
s = s.replace('&quot;', '"')
s = s.replace('&lt;', '<')
s = s.replace('&gt;', '>')
s = s.replace('&amp;', '&')
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-19 19:24:10 +02:00
s = re.sub(r'<img[^>]*logo_bio[^>]*>', 'Bio', s)
s = re.sub(r'<[^>]*>', '', s)
2010-05-03 17:01:05 +02:00
return s
2010-10-31 23:01:21 +01:00
stripunescape = (lambda item: (unescape(item[0].strip()), unescape(item[1].strip())))
meals = map(lambda item: map(stripunescape, item), meals)
2010-05-03 17:01:05 +02:00
reply = 'Mensa-Menü für den %s\n\n' % day.strftime('%d.%m.%y')
2010-10-31 23:01:21 +01:00
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]
2011-02-14 02:02:00 +01:00
#if meals[4][weekday][0]:
# reply += 'Wok: %s (%s)\n' % meals[4][weekday]
2010-10-31 23:01:21 +01:00
if meals[4][weekday][0]:
2011-02-14 02:02:00 +01:00
reply += 'Beilagen: %s (%s)\n' % meals[4][weekday]
2010-10-31 23:01:21 +01:00
2010-05-03 17:01:05 +02:00
return reply
2010-10-31 23:01:21 +01: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-10-31 23:01:21 +01:00
2010-05-03 05:20:21 +02:00
handler.reply(reply.strip())