-
Notifications
You must be signed in to change notification settings - Fork 0
/
iserv.py
214 lines (179 loc) · 6.11 KB
/
iserv.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
import datetime
import requests
import bs4
from bs4 import *
VERSION = (0, 2, 0, "beta")
DAYS = [
"monday",
"tuesday",
"wednesday",
"thursday",
"friday"
]
class LoginError(Exception):
pass
class IServ:
paths = {
"login": "/iserv/app/login",
"logout": "/iserv/app/logout",
"plan": "/iserv/plan/show/raw/",
"tasks": "/iserv/exercise.csv",
"vc_load": "/iserv/videoconference/api/health"
}
messages = {
"login_failed": "Anmeldung fehlgeschlagen!"
}
def __init__(self, domain):
self.domain = domain
self._csrf_token = None
self._s = requests.Session()
def login(self, user: str, pw: str):
# send post request with session object
r = self._s.post(
url=self.domain + IServ.paths['login'], # login path
data=f"_password={pw}&_username={user}", # pw ad user in body of request
headers={
"Content-Type": "application/x-www-form-urlencoded" # tell server kind of form, necessary
}
)
# check if login was successful
# TODO check in header
if IServ.messages["login_failed"] in r.text:
raise LoginError("Login failed")
# find and store csrf token, needed on logout
self._csrf_token = self._find_csrf(r.text)
# return True if login was successful
return True
def logout(self):
self._s.get(
# login path
url=self.domain + IServ.paths['logout'],
# add csrf token to query
params={"_csrf": self._csrf_token}
)
@staticmethod
def _find_csrf(doc: str):
s = BeautifulSoup(doc, "html.parser")
tag = s.find('body')\
.find_all('div')[0]\
.find_all('div')[0]\
.find_all('ul')[0]\
.find_all('li')[1]\
.find('div')\
.find('ul')\
.find_all('li')[4]\
.find('a')
return tag.attrs["href"].split('=')[-1]
def plan_changes(
self,
courses=None,
days=None,
week: int = datetime.date.today().isocalendar()[1],
plan_name="0_Vertretungen (Schüler)"
):
# default args
if days is None:
days = [""]
if type(days) == str:
days = [days]
days = [d.lower() for d in days]
if courses is None:
courses = [""]
if type(courses) == str:
courses = [courses]
courses = [c.lower() for c in courses]
# TODO on sunday next week
# get doc
r = self._s.get(
url=self.domain + self.paths["plan"] + plan_name + "/" + str(week) + "/w/w00000.htm"
)
# validate return
if not r.headers["content-disposition"] == "inline; filename=w00000.htm":
raise Exception("invalid return")
# create soup
s = BeautifulSoup(r.text, "html.parser") # , from_encoding="utf-8")
# get list of needed tables in doc
tables = s.find_all("table", class_="subst")
entries = {}
for i in range(len(tables)):
# get days
day_temp = DAYS[i]
# check day
if days == [""]:
pass
elif day_temp not in days:
# skip this table
continue
entries[day_temp] = []
# iterate through all rows of table and store data
for row in tables[i]:
if type(row) != bs4.element.NavigableString:
# get all values of columns
clm = [i.string for i in row.find_all("td")]
try:
add = False
for course in courses:
add_temp = True
for letter in course:
if letter not in clm[0].lower():
add_temp = False
if add_temp:
add = True
if add:
details = {
"courses": clm[0],
"hour": clm[1],
"subject": clm[2],
"teacher": clm[3],
"room": clm[4],
"comments": clm[5],
"org_subject": clm[6],
"org_teacher": clm[7],
"type": clm[8],
}
# print(details)
entries[day_temp].append(details)
except IndexError:
pass
return entries
def tasks(
self,
status: str = "current",
sort_by: str = "enddate",
sort_dir: str = "DESC"
):
# TODO add option to filter tasks
r = self._s.get(
url=self.domain + IServ.paths["tasks"],
params={
"filter[status]": status,
"sort[by]": sort_by,
"sort[dir]": sort_dir
}
)
tasks = []
tasks_ = r.text.splitlines()
tasks_.pop(0)
# TODO use csv.reader to solve problems with quoted values and separators between those
# tasks_ = csv.reader(r.text, quotechar='"')
for t in tasks_:
task = t.split(";")
for i in range(len(task)):
if task[i].lower() == "ja":
task[i] = True
elif task[i].lower() == "nein":
task[i] = False
tasks.append({
"task": task[0],
"startdate": task[1],
"enddate": task[2],
"tags": task[3],
"done": task[4],
"review": task[5],
})
return tasks
def vc_load(self):
r = self._s.get(
url=self.domain + IServ.paths["vc_load"]
)
return r.json()