summaryrefslogtreecommitdiffstats
path: root/modules/mensa.py
blob: 40c63fa78e91f7081375bcfbae66958a4adb5c36 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# -*- coding: utf-8 -*-

from base import ModuleBase
import re
from datetime import date, timedelta
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:
            day += timedelta(1)
            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
        
        def handle_data(data):
            if not re.search(datestr, data):
                return False
            
            (data, count) = re.compile(r'^.*' + datestr + r'(.*?)<td rowspan="3" class="schrift_fett"><div>.*$', re.DOTALL).subn(r'\1', data)
            
            if count < 1:
                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 = []
            pos = 0
            r = re.compile(r'\s*<td class="schrift_(gerichte|spezial)">(.+?)</td>', 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*<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):
                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('&amp;', '&')
                s = s.replace('&lt;', '<')
                s = s.replace('&gt;', '>')
                s = s.replace('&quot;', '"')
                s = s.replace('<br />', ' ‒ ')
                s = re.sub('\s+', ' ', s)
                return s
            
            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
        
        reply = handle_data(data)
        if not reply:
            reply = '%s kein Mensa-Menü... ist vielleicht Wochenende?' % daystr
        
        handler.reply(reply.strip())