8
8
from mautrix .errors import MForbidden
9
9
from mautrix .types import TextMessageEventContent , MessageType , Format , RelatesTo , RelationType , RoomID
10
10
11
- from .parser import get_menus
11
+ from .parser import get_menus , parse_movies
12
12
from .db import MenuDatabase
13
13
from .menu import Menu
14
14
15
15
URLS = [
16
16
'https://www.studentenwerk-magdeburg.de/mensen-cafeterien/mensa-unicampus/speiseplan-unten/' ,
17
17
'https://www.studentenwerk-magdeburg.de/mensen-cafeterien/mensa-unicampus/speiseplan-oben/'
18
- #'https://www.studentenwerk-magdeburg.de/mensen-cafeterien/mensa-stendal/speiseplan/',
19
- #'https://www.studentenwerk-magdeburg.de/mensen-cafeterien/mensa-herrenkrug/speiseplan/',
20
- #'https://www.studentenwerk-magdeburg.de/mensen-cafeterien/mensa-kellercafe/speiseplan/'
18
+ # 'https://www.studentenwerk-magdeburg.de/mensen-cafeterien/mensa-stendal/speiseplan/',
19
+ # 'https://www.studentenwerk-magdeburg.de/mensen-cafeterien/mensa-herrenkrug/speiseplan/',
20
+ # 'https://www.studentenwerk-magdeburg.de/mensen-cafeterien/mensa-kellercafe/speiseplan/'
21
21
]
22
22
NOTIF_TIME = {
23
23
"h" : 14 ,
@@ -39,6 +39,14 @@ def date_keyword_to_date(date_keyword) -> date:
39
39
return switcher .get (date_keyword )
40
40
41
41
42
+ def formatted_movie_list (movies : list [tuple [datetime , str ]]) -> str :
43
+ ret = "Next movies: \n \n "
44
+ for movie in movies :
45
+ datetime_str = movie [0 ].strftime ("%d.%m.%Y, %H:%M" )
46
+ ret += f"* { datetime_str } : { movie [1 ]} \n "
47
+ return ret
48
+
49
+
42
50
class MensaBot (Plugin ):
43
51
db : MenuDatabase
44
52
loop_task : asyncio .Future
@@ -55,8 +63,10 @@ async def fetch_loop(self) -> None:
55
63
self .log .debug ("Fetching loop started" )
56
64
while True :
57
65
now = datetime .now (TZ )
58
- days = 1 if now > now .replace (hour = NOTIF_TIME .get ("h" ), minute = NOTIF_TIME .get ("m" ), second = 0 , microsecond = 0 ) else 0
59
- scheduled_time = now .replace (hour = NOTIF_TIME .get ("h" ), minute = NOTIF_TIME .get ("m" ), second = 0 , microsecond = 0 ) + timedelta (days = days )
66
+ days = 1 if now > now .replace (hour = NOTIF_TIME .get ("h" ), minute = NOTIF_TIME .get ("m" ), second = 0 ,
67
+ microsecond = 0 ) else 0
68
+ scheduled_time = now .replace (hour = NOTIF_TIME .get ("h" ), minute = NOTIF_TIME .get ("m" ), second = 0 ,
69
+ microsecond = 0 ) + timedelta (days = days )
60
70
self .log .info (f"Scheduled fetch for { scheduled_time } in { scheduled_time - now } seconds" )
61
71
await asyncio .sleep ((scheduled_time - now ).total_seconds ())
62
72
asyncio .create_task (self .autofetch_menus ())
@@ -170,6 +180,25 @@ async def unsubscribe(self, evt: MessageEvent) -> None:
170
180
))
171
181
await evt .respond (content )
172
182
183
+ @command .new ("hid" , help = "Next Hörsaal im Dunkeln event" )
184
+ async def hid (self , evt : MessageEvent ) -> None :
185
+ movies = await parse_movies (self , "https://www.unifilm.de/studentenkinos/MD_HiD" )
186
+
187
+ # remove all old movies
188
+ movies [:] = [movie for movie in movies if movie [0 ].date () >= datetime .now (TZ ).date ()]
189
+
190
+ self .log .debug (f"Upcoming movies: { movies } " )
191
+ if len (movies ) == 0 :
192
+ await evt .respond ("No planned movies." )
193
+ return
194
+ else :
195
+ # only keep movies of the next movie day
196
+ next_movie_day = movies [0 ][0 ].date ()
197
+ movies [:] = [movie for movie in movies if movie [0 ].date () == next_movie_day ]
198
+
199
+ self .log .info (f"Movies on next movie day: { movies } " )
200
+ await evt .respond (formatted_movie_list (movies ))
201
+
173
202
async def fetch_menus (self ) -> None :
174
203
days = []
175
204
for url in URLS :
@@ -214,7 +243,7 @@ def get_next_available_day(self):
214
243
days = self .db .get_menu_days ()
215
244
for day in days :
216
245
if day >= today :
217
- if day != today : # no menu today, returning the next available
246
+ if day != today : # no menu today, returning the next available
218
247
return day
219
248
elif datetime .now (TZ ) > today2pm :
220
249
return days .__next__ ()
0 commit comments