Improved mensa module

This commit is contained in:
Matthias Schiffer 2010-05-03 17:01:05 +02:00
parent 78703620d3
commit 7ce3e4c59e

View file

@ -17,69 +17,72 @@ class Module(ModuleBase):
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: if tomorrow:
day += timedelta(1) day += timedelta(1)
daystr = 'morgen' daystr = 'Morgen'
datestr = day.strftime('%d.%m.</div>') 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()
f.close()
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
found = False def handle_data(data):
line = f.readline() if not re.search(datestr, data):
while line: return False
if re.search(datestr, line):
found = True
break
line = f.readline()
if not found: (data, count) = re.compile(r'^.*' + datestr + r'(.*?)<td rowspan="3" class="schrift_fett"><div>.*$', re.DOTALL).subn(r'\1', data)
f.close()
handler.reply('%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr) if count < 1:
return return False
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 = [] meals = []
r = re.compile(r'<td class="schrift_gerichte">([^<]*)</td>') pos = 0
line = f.readline() r = re.compile(r'\s*<td class="schrift_(gerichte|spezial)">(.+?)</td>', re.DOTALL)
while line: match = r.match(mealstr, pos)
match = r.search(line) while match:
if match: meals.append((match.group(2), match.group(1) == 'spezial'))
meals.append(match.group(1))
if len(meals) >= 4: if len(meals) >= 4:
break break
if re.search('<td rowspan="3" class="schrift_fett"><div>', line): pos = match.end()
break match = r.match(mealstr, pos)
line = f.readline() if len(meals) < 4:
return False
prices = [] prices = []
r = re.compile(r'<td class="schrift_preise">([^<]*)</td>') pos = 0
line = f.readline() r = re.compile(r'\s*<td class="schrift_(?:preise|spezial)">(.+?)</td>', re.DOTALL)
while line: match = r.match(pricestr, pos)
match = r.search(line) while match:
if match:
prices.append(match.group(1)) prices.append(match.group(1))
if len(prices) >= 4: if len(prices) >= 4:
break break
if re.search('<td rowspan="3" class="schrift_fett"><div>', line): pos = match.end()
break match = r.match(pricestr, pos)
line = f.readline() if len(prices) < 4:
return False
f.close()
if len(meals) < 4 or len(prices) < 4:
handler.reply('%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr)
return
def unescape(s): def unescape(s):
s = s.replace('&nbsp;', '') s = s.replace('&nbsp;', '')
@ -95,21 +98,43 @@ class Module(ModuleBase):
s = s.replace('&lt;', '<') s = s.replace('&lt;', '<')
s = s.replace('&gt;', '>') s = s.replace('&gt;', '>')
s = s.replace('&quot;', '"') s = s.replace('&quot;', '"')
s = s.replace('<br />', ' ')
s = re.sub('\s+', ' ', s) s = re.sub('\s+', ' ', s)
return s return s
meals = map(unescape, meals) meals = map(lambda (s, special): (unescape(s), special), meals)
prices = map(unescape, prices) prices = map(unescape, prices)
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][0]:
if meals[0][1]:
reply += 'Mensatipp: '
else:
reply += 'Eintopf: '
reply += '%s (%s)\n' % (meals[0][0], prices[0])
if meals[1][0]:
if meals[1][1]:
reply += 'Mensatipp: '
else:
reply += 'Hauptgericht 1: '
reply += '%s (%s)\n' % (meals[1][0], prices[1])
if meals[2][0]:
if meals[2][1]:
reply += 'Mensatipp: '
else:
reply += 'Hauptgericht 2: '
reply += '%s (%s)\n' % (meals[2][0], prices[2])
if meals[3][0]:
if meals[3][1]:
reply += 'Mensatipp: '
else:
reply += 'Vegetarisches Hauptgericht: '
reply += '%s (%s)\n' % (meals[3][0], prices[3])
if meals[0]: return reply
reply += 'Eintopf: %s (%s)\n' % (meals[0], prices[0])
if meals[1]: reply = handle_data(data)
reply += 'Hauptgericht 1: %s (%s)\n' % (meals[1], prices[1]) if not reply:
if meals[2]: reply = '%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr
reply += 'Hauptgericht 2: %s (%s)\n' % (meals[2], prices[2])
if meals[3]:
reply += 'Vegetarisches Hauptgericht: %s (%s)\n' % (meals[3], prices[3])
handler.reply(reply.strip()) handler.reply(reply.strip())