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
141
142
143
|
# -*- 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(' ', '')
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 = s.replace('<', '<')
s = s.replace('>', '>')
s = s.replace('&', '&')
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())
|