3
3
from mautrix .types import TextMessageEventContent , MessageType , Format , RelatesTo , RelationType
4
4
from maubot import Plugin , MessageEvent
5
5
from maubot .handlers import command
6
- import requests
7
6
8
7
URL = 'https://www.studentenwerk-magdeburg.de/mensen-cafeterien/mensa-unicampus/'
9
8
@@ -13,7 +12,8 @@ class MensaBot(Plugin):
13
12
@command .new ("hunger" , help = "Show the meals" )
14
13
@command .argument ("message" , pass_raw = True , required = False )
15
14
async def hunger_handler (self , evt : MessageEvent , message : str = "" ) -> None :
16
- menu : Menu = Menu (URL )
15
+ menu : Menu = Menu ()
16
+ await menu .init (self , URL )
17
17
content = TextMessageEventContent (
18
18
msgtype = MessageType .NOTICE , format = Format .HTML ,
19
19
body = f"{ menu } " ,
@@ -35,12 +35,15 @@ def __init__(self, name: str, price: str):
35
35
self .name = name
36
36
self .price = price
37
37
38
+
38
39
def __str__ (self ) -> str :
39
40
return self .name + "\n " + self .price
40
41
42
+
41
43
def to_list (self ) -> List :
42
44
return [self .name , self .price ]
43
45
46
+
44
47
def to_dict (self ) -> Dict :
45
48
return {
46
49
"name" : self .name ,
@@ -53,35 +56,42 @@ class Menu:
53
56
day : str = ""
54
57
meals : List [Meal ] = []
55
58
56
- def __init__ (self , url : str ):
59
+ async def init (self , mensabot : Plugin , url : str ) -> None :
57
60
self .meals = []
58
- page = requests .get (url )
59
- soup = BeautifulSoup (page .content , 'html.parser' )
60
- div_mensa = soup .find ("div" , class_ = "mensa" )
61
- self .day = div_mensa .find ("table" ).find ("thead" ).find ("tr" ).find ("td" ).string
62
- for element in div_mensa .find ("table" ).find ("tbody" ).find_all ("tr" ):
63
- meal = Meal (element .find_all ("td" ).pop (0 ).find ("strong" ).contents .pop (0 ).string ,
64
- element .find_all ("td" ).pop (0 ).contents .pop (2 ).string )
65
- self .meals .append (meal )
61
+ async with mensabot .http .get (url ) as resp :
62
+ page = await resp .text ()
63
+ mensabot .log .info (page )
64
+ soup = BeautifulSoup (page , 'html.parser' )
65
+ div_mensa = soup .find_all ("div" , class_ = "mensa" )
66
+ self .day = div_mensa [0 ].find ("table" ).find ("thead" ).find ("tr" ).find ("td" ).string
67
+ for mensa_table in div_mensa :
68
+ for element in mensa_table .find ("table" ).find ("tbody" ).find_all ("tr" ):
69
+ meal = Meal (element .find_all ("td" ).pop (0 ).find ("strong" ).contents .pop (0 ).string ,
70
+ element .find_all ("td" ).pop (0 ).contents .pop (2 ).string )
71
+ self .meals .append (meal )
72
+
66
73
67
74
def __str__ (self ) -> str :
68
75
plain_text = ""
69
76
for meal in self .meals :
70
77
plain_text += "\n ----------\n " + str (meal )
71
78
return f'Speiseplan für { self .day } :{ plain_text } '
72
79
80
+
73
81
def to_html (self ) -> str :
74
82
plain_text = ""
75
83
for meal in self .meals :
76
84
plain_text += "<hr><br><strong>" + meal .name + "</strong><br>" + meal .price
77
85
return f'<h3>Speiseplan für { self .day } :</h3>{ plain_text } '
78
86
87
+
79
88
def to_list (self ) -> List [List ]:
80
89
result = []
81
90
for meal in self .meals :
82
91
result .append (meal .to_list ())
83
92
return result
84
93
94
+
85
95
def to_dict (self ) -> Dict :
86
96
result = {}
87
97
for i in range (len (self .meals )):
0 commit comments