-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcentris.py
393 lines (295 loc) · 9.7 KB
/
centris.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#The amazing centris 2.0
import requests
from getpass import getpass
from bs4 import BeautifulSoup
from prettytable import PrettyTable
from datetime import datetime
import argparse
import re
from pathlib import Path
def login(args):
store_user()
store_pass()
def store_pass():
with open('supersecure', 'w') as f:
f.write(getpass())
def password():
return open('supersecure').read()
def username():
return open('user').read()
def store_user():
with open('user', 'w') as f:
x = input("Username: ")
f.write(x)
url = 'https://myschool.ru.is/myschool/' #the baseurl
def islogedin():
try:
username()
password()
except:
print('Hey cutie! You must be logged in <3')
login(None)
def getcourses():
r = requests.get('https://myschool.ru.is/myschool', auth=(username(), password()))
soup = BeautifulSoup(r.text, 'html.parser')
courselink = ''
for link in soup.find_all('a'):
if link.text == 'Námskeið':
courselink = url + link.get('href')
r = requests.get(courselink, auth=(username(), password()))
coursesoup = BeautifulSoup(r.text, 'html.parser')
links = coursesoup.find_all('span', title = True)
list = []
for i in links:
list.append(i['title'])
return list
#My courses
def courses(args):
islogedin()
r = requests.get('https://myschool.ru.is/myschool', auth=(username(), password()))
soup = BeautifulSoup(r.text, 'html.parser')
courses = soup.find_all('center')
tablecontent = courses[1]
x = tablecontent.find('table')
tableheader = x.find('th')
y = PrettyTable()
y.field_names = tableheader
allcourses = getcourses()
clean = []
for i in allcourses:
string = re.sub('\n', '', i)
cleanstring = re.sub('\.[0-9]+', '', string)
clean.append(re.sub('\r', ' ', cleanstring))
list = []
for i in clean:
list.append(i)
y.add_row(list)
list = []
print(y.get_string())
#timetable
def timetable(args):
islogedin()
r = requests.get('https://myschool.ru.is/myschool', auth=(username(), password()))
soup = BeautifulSoup(r.text, 'html.parser')
for link in soup.find_all('a'):
if link.text == 'Stundatafla':
timetable = url + link.get('href') #create url for Verkefni
res = requests.get(timetable, auth=(username(), password()))
timetablesoup = BeautifulSoup(res.text, 'html.parser')
timetable = timetablesoup.find('div', {'class': 'ruContentPage'})
rows = timetable.find_all('tr')
allrows = []
for i in rows:
allrows.append(i)
cleanrows = allrows[1:]
m = PrettyTable()
tableheader = cleanrows[0].find_all('th')
tabledate = cleanrows[1].find_all('th')
field = []
count = 0
for data in tableheader:
field.append(data.text[0:3] + ' - ' + tabledate[count].text)
count = count + 1
m.field_names = field
for row in cleanrows[2:-3]:
list = row.find_all('td')
data = []
for i in list:
data.append(i.text.strip('\n'))
m.add_row(data)
print(m.get_string())
#Due assignments
def dueass():
islogedin()
r = requests.get('https://myschool.ru.is/myschool', auth=(username(), password()))
soup = BeautifulSoup(r.text, 'html.parser')
for link in soup.find_all('a'):
if link.text == 'Verkefni':
dueproject = url + link.get('href') #create url for Verkefni
res = requests.get(dueproject, auth=(username(), password())) #new requests with the new url
projectsoup = BeautifulSoup(res.text, 'html.parser')
proj = projectsoup.find('div', {'class': 'ruContentPage'})
tables = proj.find_all('table')
def printtable(html, n):
tableheader = html.find_all('th')
tablerows=html.find_all('td')
x = PrettyTable() #create new pretty table method
field = []
for data in tableheader:
field.append(data.text)
x.field_names = field
row = []
num = 0
for data in tablerows:
row.append(data.text)
if num == n:
x.add_row(row)
row = []
num = 0
else:
num += 1
print(x.get_string())
printtable(tables[0], 4)
y = PrettyTable()
tableheader = tables[1].find_all('th')
rows = tables[1].find_all('tr')
#print pretty table for all assignments in one course
def ass(url):
res = requests.get(url, auth=(username(), password()))
projectsoup = BeautifulSoup(res.text, 'html.parser')
ttables = projectsoup.find_all('center')
ttlist = []
for i in ttables:
ttlist.append(i.table)
ttablelist = []
for i in ttlist:
ttablelist.append(i.find('table'))
tableheader = ttablelist[0].find('tr')
m = PrettyTable()
header = tableheader.find_all('th')
headerlist = []
for i in header:
headerlist.append(i.text)
m.field_names = headerlist
rows = []
for i in ttablelist:
rows += i.find_all('tr')
for row in rows:
if row.find('td'):
list = row.find_all('td')
data = []
for i in list:
data.append(i.text)
while len(data) < 7:
data.append('')
m.add_row(data)
title = projectsoup.find('span', {'class':'ruContent'})
print()
print(title.text)
print(m.get_string())
def allcourselink():
r = requests.get('https://myschool.ru.is/myschool', auth=(username(), password()))
soup = BeautifulSoup(r.text, 'html.parser')
courselink = ''
for link in soup.find_all('a'):
if link.text == 'Námskeið':
courselink = url + link.get('href') #create url for Verkefni
res = requests.get(courselink, auth=(username(), password()))
projectsoup = BeautifulSoup(res.text, 'html.parser')
proj = projectsoup.find('td', {'class': 'ruRight'})
for link in projectsoup.find_all('a'):
if link.text == 'Verkefni *':
courselink = url + link.get('href')
#test new link
res = requests.get(courselink, auth=(username(), password()))
projectsoup = BeautifulSoup(res.text, 'html.parser')
newlinks = projectsoup.find('div', {'id': 'ruTabsNewcontainer'})
tablist = newlinks.find_all('a')
return tablist
def allassignments():
tablist = allcourselink()
#print all assignments
for i in tablist:
ass(url + i.get('href'))
def assignments(args):
islogedin()
if args.all:
allassignments()
elif args.due:
dueass()
elif args.course:
course(args.course)
else:
dueass()
def logout(args):
if Path('supersecure').exists():
Path('supersecure').unlink()
if Path('user').exists():
Path('user').unlink()
def lunch(args):
islogedin()
r = requests.get('https://myschool.ru.is/myschool', auth=(username(), password()))
soup = BeautifulSoup(r.text, 'html.parser')
for link in soup.find_all('a'):
if link.text == 'MÁLIÐ':
menulink = link.get('href') #create url for Verkefni
res = requests.get(menulink)
menusoup = BeautifulSoup(res.text, 'html.parser')
x = menusoup.find_all('div',{'class': 'wpb_wrapper'})
menuweek = []
for i in menusoup.find_all('div',{'class': 'wpb_wrapper'}):
if i.find('div',{'class': 'menu-text'}):
data = []
for k in i.find_all('div',{'class': 'menu-text'}):
data.append(k)
menuweek.append(data)
if args.today:
menutoday(menuweek)
elif args.week:
allweekmenu(menuweek)
else:
menutoday(menuweek)
def allweekmenu(menuweek):
days = ['Mánudagur', 'Þriðjudagur', 'Miðvikudagur', 'Fimmtudagur', 'Föstudagur']
count = 0
print('-------------------------------------------------')
for day in menuweek:
print('{:^40}'.format('*** ' + days[count] + ' ***'))
print()
for i in day:
print(i.find('div', {'class': 'menu-title'}).text)
print('-> '+i.find('div', {'class': 'menu-desc'}).text)
print()
print('-------------------------------------------------')
print()
count += 1
def menutoday(menuweek):
days = ['Mánudagur', 'Þriðjudagur', 'Miðvikudagur', 'Fimmtudagur', 'Föstudagur']
x = datetime.today().weekday()
if x == 6 or x == 7:
print('No lunch today :-(')
else:
print('{:^40}'.format('*** ' + days[x] + ' ***'))
print()
for i in menuweek[x]:
print(i.find('div', {'class': 'menu-title'}).text)
print('-> '+i.find('div', {'class': 'menu-desc'}).text)
print()
def course(string):
courselist = getcourses()
splitlist = []
for i in courselist:
splitlist.append(re.split('-|\.', i))
courses = [i[2] for i in splitlist ]
if string in courses:
tablist = allcourselink()
for i in tablist:
if string in i.parent['title']:
ass(url + i.get('href'))
else:
print('Please insert one of the following commands: ')
for i in courses:
print(i, end=', ')
#ARGSPARSERS
parser = argparse.ArgumentParser(
description="Get stuff from myschool")
subparsers = parser.add_subparsers()
parser_timetable = subparsers.add_parser('timetable', help='shows your current timetable from myschool')
parser_timetable.set_defaults(func=timetable)
parser_courses = subparsers.add_parser('courses', help='shows your current courses')
parser_courses.set_defaults(func=courses)
parser_assignments = subparsers.add_parser('assignments', help='shows your upcoming assignment by default, add -a for all assignments, add -c "abbreviation for coursename"')
parser_assignments.add_argument('-a', '--all', action='store_true', help='shows all your assignments for this semester')
parser_assignments.add_argument('-d', '--due', action='store_true', help='shows your comming up assignments')
parser_assignments.add_argument('-c','--course', metavar='STRING', default ='', type=str, help='shows all your assignments for specified course: type -c "abbreviation for coursename"')
parser_assignments.set_defaults(func=assignments)
parser_timetable = subparsers.add_parser('logout', help='logout from your account')
parser_timetable.set_defaults(func=logout)
parser_timetable = subparsers.add_parser('login', help='login to your myschool acc')
parser_timetable.set_defaults(func=login)
parser_menu = subparsers.add_parser('lunch', help='shows the menu from the cafeteria in HR. shows the lunch today by default')
parser_menu.add_argument('-t', '--today', action='store_true', help='show the today\'s lunch at málið in HR')
parser_menu.add_argument('-w', '--week', action='store_true', help='show the lunch for the week at málið in HR')
parser_menu.set_defaults(func=lunch)
args = parser.parse_args()
args.func(args)