Rewrote mensa module ...
This commit is contained in:
parent
c33aea37f2
commit
ae46cfb761
2 changed files with 70 additions and 74 deletions
2
curunir
2
curunir
|
@ -5,4 +5,4 @@ DIR=`dirname $0`
|
||||||
|
|
||||||
cd "$DIR"
|
cd "$DIR"
|
||||||
export PYTHONPATH="$PYTHONPATH:."
|
export PYTHONPATH="$PYTHONPATH:."
|
||||||
exec /usr/bin/env python -OO "$APP.py" "$@"
|
exec /usr/bin/env python2 -OO "$APP.py" "$@"
|
||||||
|
|
142
modules/mensa.py
142
modules/mensa.py
|
@ -8,22 +8,21 @@ import urllib
|
||||||
class Module(ModuleBase):
|
class Module(ModuleBase):
|
||||||
def __init__(self, manager):
|
def __init__(self, manager):
|
||||||
ModuleBase.__init__(self, manager)
|
ModuleBase.__init__(self, manager)
|
||||||
|
|
||||||
def commands(self):
|
def commands(self):
|
||||||
return [('!mensa [tomorrow]', 'Zeigt das aktuelle Menü der Mensa der Uni Lübeck an (oder das des nächsten Tages)')]
|
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):
|
def groupchat(self, room, nick, text, handler):
|
||||||
if not re.match(r'!mensa(?:\W|\Z)', text):
|
if not re.match(r'!mensa(?:\W|\Z)', text):
|
||||||
return
|
return
|
||||||
|
|
||||||
tomorrow = re.match(r'!mensa\W+tomorrow(\W|\Z)', text)
|
tomorrow = re.match(r'!mensa\W+tomorrow(\W|\Z)', text)
|
||||||
daystr = 'Heute'
|
daystr = 'Heute'
|
||||||
day = date.today()
|
day = date.today()
|
||||||
if tomorrow or datetime.now().time() > time(15):
|
if tomorrow or datetime.now().time() > time(15):
|
||||||
day += timedelta(1)
|
day += timedelta(1)
|
||||||
daystr = 'Morgen'
|
daystr = 'Morgen'
|
||||||
datestr = day.strftime(r'%d\.%m\.<\/div>')
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
f = urllib.urlopen(self.manager.config['mensa_url'], 'r')
|
f = urllib.urlopen(self.manager.config['mensa_url'], 'r')
|
||||||
data = f.read()
|
data = f.read()
|
||||||
|
@ -31,67 +30,60 @@ class Module(ModuleBase):
|
||||||
except IOError:
|
except IOError:
|
||||||
handler.reply('Hmm, es scheint gerade keine Daten über das Mensa-Menü zu geben.')
|
handler.reply('Hmm, es scheint gerade keine Daten über das Mensa-Menü zu geben.')
|
||||||
return
|
return
|
||||||
|
|
||||||
def handle_data(data):
|
def handle_data(data):
|
||||||
if not re.search(datestr, data):
|
r = re.compile(r'.*?<tr[^>]*>(.*?)</tr>.*?<tr[^>]*>(.*?)</tr>', re.DOTALL)
|
||||||
return False
|
r2 = re.compile(r'.*?<td[^>]*>(.*?)</td>', re.DOTALL)
|
||||||
|
|
||||||
match = re.compile(r'^.*' + datestr + r'(.*?)(?:<td rowspan="3" class="schrift_fett"><div>|</table>).*$', re.DOTALL).match(data)
|
def handle_data_row(data, pos):
|
||||||
|
|
||||||
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)
|
match = r.match(data, pos)
|
||||||
|
|
||||||
if not match:
|
if not match:
|
||||||
return False
|
return False
|
||||||
mealstr = match.group(1)
|
|
||||||
pos = match.end()
|
ret = []
|
||||||
|
mealpos = 0
|
||||||
match = r.match(data, pos)
|
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>.*?(?:<col[^>]*>\s*){4}(.*?)</table>.*$', re.DOTALL).match(data)
|
||||||
|
|
||||||
if not match:
|
if not match:
|
||||||
return False
|
return False
|
||||||
pricestr = match.group(1)
|
|
||||||
|
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 = []
|
meals = []
|
||||||
pos = 0
|
pos = 0
|
||||||
r = re.compile(r'\s*<td class="schrift_(gerichte|spezial)">(.+?)</td>', re.DOTALL)
|
|
||||||
match = r.match(mealstr, pos)
|
for i in range(0,6):
|
||||||
while match:
|
row = handle_data_row(data, pos)
|
||||||
meals.append(match.group(2))
|
|
||||||
|
if not row:
|
||||||
if len(meals) >= 4:
|
return False
|
||||||
break
|
|
||||||
|
meals.append(row[0])
|
||||||
pos = match.end()
|
pos = row[1]
|
||||||
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):
|
def unescape(s):
|
||||||
s = s.replace(' ', '')
|
s = s.replace(' ', '')
|
||||||
s = s.replace('€', '€')
|
s = s.replace('€', '€')
|
||||||
|
@ -117,24 +109,28 @@ class Module(ModuleBase):
|
||||||
s = re.sub(r'<img[^>]*logo_bio[^>]*>', 'Bio', s)
|
s = re.sub(r'<img[^>]*logo_bio[^>]*>', 'Bio', s)
|
||||||
s = re.sub(r'<[^>]*>', '', s)
|
s = re.sub(r'<[^>]*>', '', s)
|
||||||
return s
|
return s
|
||||||
|
|
||||||
meals = map(unescape, meals)
|
stripunescape = (lambda item: (unescape(item[0].strip()), unescape(item[1].strip())))
|
||||||
prices = map(unescape, prices)
|
meals = map(lambda item: map(stripunescape, item), meals)
|
||||||
|
|
||||||
reply = 'Mensa-Menü für den %s\n\n' % day.strftime('%d.%m.%y')
|
reply = 'Mensa-Menü für den %s\n\n' % day.strftime('%d.%m.%y')
|
||||||
if meals[0]:
|
if meals[0][weekday][0]:
|
||||||
reply += 'Eintopf: %s (%s)\n' % (meals[0].strip(), prices[0].strip())
|
reply += 'Eintopf: %s (%s)\n' % meals[0][weekday]
|
||||||
if meals[1]:
|
if meals[1][weekday][0]:
|
||||||
reply += 'Hauptgericht 1: %s (%s)\n' % (meals[1].strip(), prices[1].strip())
|
reply += 'Hauptgericht 1: %s (%s)\n' % meals[1][weekday]
|
||||||
if meals[2]:
|
if meals[3][weekday][0]:
|
||||||
reply += 'Hauptgericht 2: %s (%s)\n' % (meals[2].strip(), prices[2].strip())
|
reply += 'Hauptgericht 2: %s (%s)\n' % meals[3][weekday]
|
||||||
if meals[3]:
|
if meals[2][weekday][0]:
|
||||||
reply += 'Vegetarisches Hauptgericht: %s (%s)\n' % (meals[3].strip(), prices[3].strip())
|
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
|
return reply
|
||||||
|
|
||||||
reply = handle_data(data)
|
reply = handle_data(data)
|
||||||
if not reply:
|
if not reply:
|
||||||
reply = '%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr
|
reply = '%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr
|
||||||
|
|
||||||
handler.reply(reply.strip())
|
handler.reply(reply.strip())
|
||||||
|
|
Reference in a new issue