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

142 lines
5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- 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'.*?<tr[^>]*>(.*?)</tr>.*?<tr[^>]*>(.*?)</tr>', re.DOTALL|re.IGNORECASE)
r2 = re.compile(r'.*?<td[^>]*>(.*?)</td>', 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'^.*?<table[^>]*>.*?<td[^>]*>.*?(\d+)\.(\d+)\. - \d+\.\d+\.(\d+).*?</td>.*?<td[^>]*>Freitag</td>\s*</tr>(.*?)</table>.*$', 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('&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('&eacute;', 'é')
s = s.replace('&ldquo;', '')
s = s.replace('&bdquo;', '')
s = s.replace('&ndash;', '')
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)
s = re.sub(r'<img[^>]*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())