From 7ce3e4c59e5d42a2c625ff9f971f32b168708c94 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Mon, 3 May 2010 17:01:05 +0200 Subject: Improved mensa module --- modules/mensa.py | 177 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 101 insertions(+), 76 deletions(-) (limited to 'modules') diff --git a/modules/mensa.py b/modules/mensa.py index 55fa664..40c63fa 100644 --- a/modules/mensa.py +++ b/modules/mensa.py @@ -17,99 +17,124 @@ class Module(ModuleBase): return tomorrow = re.match(r'!mensa\W+tomorrow(\W|\Z)', text) - daystr = 'heute' + daystr = 'Heute' day = date.today() if tomorrow: day += timedelta(1) - daystr = 'morgen' - datestr = day.strftime('%d.%m.') + 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 - found = False - line = f.readline() - while line: - if re.search(datestr, line): - found = True - break - line = f.readline() - - if not found: - f.close() - handler.reply('%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr) - return - - meals = [] - r = re.compile(r'([^<]*)') - line = f.readline() - while line: - match = r.search(line) - if match: - meals.append(match.group(1)) + def handle_data(data): + if not re.search(datestr, data): + return False - if len(meals) >= 4: - break + (data, count) = re.compile(r'^.*' + datestr + r'(.*?)
.*$', re.DOTALL).subn(r'\1', data) - if re.search('
', line): - break + if count < 1: + return False - line = f.readline() - - prices = [] - r = re.compile(r'([^<]*)') - line = f.readline() - while line: - match = r.search(line) - if match: + r = re.compile(r'.*?]*>(.*?)', 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 = [] + pos = 0 + r = re.compile(r'\s*(.+?)', re.DOTALL) + match = r.match(mealstr, pos) + while match: + meals.append((match.group(2), match.group(1) == 'spezial')) + + 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*(.+?)', 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: - break + if len(prices) < 4: + return False - if re.search('
', line): - break + def unescape(s): + s = s.replace(' ', '') + s = s.replace('€', '€') + s = s.replace('ä', 'ä') + s = s.replace('Ä', 'Ä') + s = s.replace('ö', 'ö') + s = s.replace('Ö', 'Ö') + s = s.replace('ü', 'ü') + s = s.replace('Ü', 'Ü') + s = s.replace('ß', 'ß') + s = s.replace('&', '&') + s = s.replace('<', '<') + s = s.replace('>', '>') + s = s.replace('"', '"') + s = s.replace('
', ' ‒ ') + s = re.sub('\s+', ' ', s) + return s - line = f.readline() - - f.close() - - if len(meals) < 4 or len(prices) < 4: - handler.reply('%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr) - return - - def unescape(s): - s = s.replace(' ', '') - s = s.replace('€', '€') - s = s.replace('ä', 'ä') - s = s.replace('Ä', 'Ä') - s = s.replace('ö', 'ö') - s = s.replace('Ö', 'Ö') - s = s.replace('ü', 'ü') - s = s.replace('Ü', 'Ü') - s = s.replace('ß', 'ß') - s = s.replace('&', '&') - s = s.replace('<', '<') - s = s.replace('>', '>') - s = s.replace('"', '"') - s = re.sub('\s+', ' ', 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') + meals = map(lambda (s, special): (unescape(s), special), meals) + prices = map(unescape, prices) + + 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]) + + return reply - if meals[0]: - reply += 'Eintopf: %s (%s)\n' % (meals[0], prices[0]) - if meals[1]: - reply += 'Hauptgericht 1: %s (%s)\n' % (meals[1], prices[1]) - if meals[2]: - reply += 'Hauptgericht 2: %s (%s)\n' % (meals[2], prices[2]) - if meals[3]: - reply += 'Vegetarisches Hauptgericht: %s (%s)\n' % (meals[3], prices[3]) + reply = handle_data(data) + if not reply: + reply = '%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr handler.reply(reply.strip()) -- cgit v1.2.3