-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuesli.py
266 lines (198 loc) · 8.31 KB
/
muesli.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# -*- coding: utf-8 -*-
"""
Created on Sun Oct 22 16:01:37 2017
@author: chris
"""
import json
from bs4 import BeautifulSoup
import re
import requests
import sys
from copy import deepcopy
from student import Student
from util import Cipher
#==============================================================================
class MuesliAcc:
def __init__ (self, mail, passw):
self.__mail = mail
self.__passw = passw
@property
def mail(self):
return self.__mail
@property
def passw(self):
return self.__passw
def toDict(self, key : str = "mymueslikey"):
with Cipher(key) as c:
d = dict()
d['ID'] = Cipher.invert(key)
d['Usern'] = c.encode(self.__mail)
d['Passw'] = c.encode(self.__passw)
return d
@staticmethod
def fromDict(d : dict):
with Cipher(Cipher.invert(d['ID'])) as c:
return MuesliAcc(c.decode(d['Usern']), c.decode(d['Passw']))
@staticmethod
def fromJsonString(jsonStr):
return MuesliAcc.fromDict(json.loads(jsonStr))
@staticmethod
def fromJsonFp(fp):
return MuesliAcc.fromDict(json.load(fp))
#==============================================================================
class Tutorial:
def __init__(self, subject, day, time, room = 'Unknown'\
, url = None, tutor = None, state = 'OWNED'):
self.__attrs = dict()
self.__attrs['Day'] = day
self.__attrs['Time'] = time
self.__attrs['Room'] = room
self.__attrs['URL'] = url
self.__attrs['Subject'] = subject
self.__attrs['Tutor'] = tutor
self.__attrs['State'] = state
#--------------------------------------------------------------------------
@property
def day(self):
return self.__attrs['Day']
@property
def time(self):
return self.__attrs['Time']
@property
def room(self):
return self.__attrs['Room']
@property
def url(self):
return self.__attrs['URL']
@url.setter
def url(self, url):
self.__attrs['URL'] = url
@property
def subject(self):
return self.__attrs['Subject']
@property
def tutor(self):
return self.__attrs['Tutor']
@property
def state(self):
return self.__attrs['State']
#--------------------------------------------------------------------------
def toJsonString(self):
return json.dumps(self.__attrs, sort_keys = True, indent = 4)
def toJsonFile(self, fname):
with open(fname, 'r', encoding = 'UTF-8') as fp:
json.dump(fp, self.__attrs)
def toDict(self):
return deepcopy(self.__attrs)
#--------------------------------------------------------------------------
@staticmethod
def fromDict(d : dict):
return Tutorial(d['Subject'], d['Day'], d['Time'], d['Room']\
, d['URL'], d['Tutor'], d['State'])
#==============================================================================
class Muesli:
def __init__ (self, acc : MuesliAcc):
self.__acc = acc
self.baseURL = 'https://muesli.mathi.uni-heidelberg.de'
self.session = None
self.curURL = None
#--------------------------------------------------------------------------
def login (self):
print('MÜSLI - login()')
self.session = requests.Session()
website = self.baseURL + "/user/login"
r = self.session.post(website, data = dict(email = self.__acc.mail, password = self.__acc.passw))
if r.url == website or r.status_code != requests.codes.ok:
raise RuntimeError('Login failed! - Check ur internet connection, username and password')
self.curURL = str(r.url)
def logout (self):
print('MÜSLI - logout')
website = self.baseURL + '/user/logout'
resultWeb = "https://muesli.mathi.uni-heidelberg.de/"
r = self.session.post(website)
self.session.close()
self.session = None
if r.url != resultWeb or r.status_code != requests.codes.ok:
raise RuntimeError('Logout Failed - Session was closed!')
#--------------------------------------------------------------------------
def __enter__ (self):
self.login()
return self
def __exit__ (self, type, value, traceback):
self.logout()
#--------------------------------------------------------------------------
def __extractTutorialInfo (self, tutoralLink):
r = self.session.post(tutoralLink)
soup = BeautifulSoup(r.text, 'html.parser')
headers = soup.findAll("h2")
pattern = re.compile("Übungsgruppe .*")
result = {}
days = {"Mo" : "Montag",
"Di" : "Dienstag",
"Mi" : "Mittwoch",
"Do" : "Donnerstag",
"Fr" : "Freitag",
"Sa" : "Samstag",
"So" : "Sonntag"}
"""
The header looks like:
Übungsgruppe zur
Vorlesung Einführung in die praktische Informatik
am Mo 16:00 (SR B128, Mathematikon B, Berliner Straße 43)
"""
result["Tutor"] = soup.find("p").text.split(':')[1].strip()
for header in headers:
if pattern.match(header.text):
words = header.text.split(' ')
for i in range(0, len(words)):
if "Vorlesung" == words[i]:
result["Subject"] = ' '.join(words[i + 1:]).split('\n')[0]
elif "am" == words[i]:
result["Day"] = days[words[i + 1]]
result["Time"] = words[i + 2]
elif re.compile("\(.*SR.*").match(words[i]):
result["Place"] = ' '.join(words[i:]).strip()[1:-1]
return Tutorial(result["Subject"], result["Day"], result["Time"] \
, result.get("Place", "Unkown Location") \
, url=tutoralLink \
, tutor=result["Tutor"])
def getAllTutorials(self, show_all = False):
startURL = 'https://muesli.mathi.uni-heidelberg.de/start'
if show_all:
r = self.session.post(startURL + '?show_all=1')
else:
r = self.session.post(startURL)
soup = BeautifulSoup(r.text, 'html.parser')
anchors = soup.findAll("a", href=re.compile("/tutorial/view/\d*"), title=False)
result = [self.baseURL + anchors[i].get("href") for i in range(0, len(anchors))]
return list(map(self.__extractTutorialInfo, result))
#--------------------------------------------------------------------------
def __getAllStudents(self, tut : Tutorial):
r = self.session.post(tut.url)
soup = BeautifulSoup(r.text, 'html.parser')
tables = soup.findAll("table", attrs={"class":"colored"})
students = []
if len(tables) != 1:
raise RuntimeError("On", tut.url
, "there where", len(tables)
, "colored tables (1 expected)!")
def extractMail(col):
return col.find('a')['href'][len('mailto:'):]
def toStudent(cols):
return Student(cols[0].text, extractMail(cols[0]), cols[1].text \
, tut.day, tut.time, tut.tutor)
grid = map(lambda row : row.findAll('td'), tables[0].findAll('tr'))
students = [toStudent(cols) for cols in grid if len(cols) > 0]
return sorted(students, key=lambda x: x.name)
def getAllStudents(self, tut):
if isinstance(tut, list):
res = []
for t in tut:
res.extend(self.getAllStudents(t))
return res
elif isinstance(tut, Tutorial):
return self.__getAllStudents(tut)
else:
raise RuntimeError("Need a 'list of Tutorials' or a 'Tutorial'")
#--------------------------------------------------------------------------
#==============================================================================