-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoodle.py
313 lines (235 loc) · 11.3 KB
/
moodle.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed May 3 12:52:08 2017
@author: christopher
"""
from bs4 import BeautifulSoup
from re import compile as rcompile
from requests import Session
import requests # only needed for requests.codes.ok - TODO find more elegant way
from os.path import join as pjoin
from account_exceptions import LoginException
from account_exceptions import LogoutException
"""
Read-only-data-storage class
This class represents submission data
"""
class Submission(object):
def __init__ (self, name, mail, submissionState, overdue, fileLink, fileName):
self.__name = name
self.__mail = mail
self.__subState = submissionState
self.__overdue = overdue
self.__fileLink = fileLink
self.__fileName = fileName
# dict-like read access
def __getitem__ (self, key):
if key == 'Name':
return self.__name
elif key == 'Mail':
return self.__mail
elif key == 'SubState':
return self.__subState
elif key == 'Overdue':
return self.__overdue
elif key == 'Link':
return self.__fileLink
elif key == 'FileName':
return self.__fileName
@staticmethod
def getKeys():
return ['Name', 'Mail', 'SubState', 'Overdue', 'Link', 'FileName']
def __iter__(self):
keys = self.getKeys()
for i in range(len(keys)):
yield self[keys[i]]
class MoodleApi(object):
def __init__ (self, acc = ('User', 'Password')):
self.__acc = acc
self.__baseURL = 'https://elearning2.uni-heidelberg.de'
self.__session = None
self.curURL = None
self.__sesskey = None
def login (self):
print('Moodle - login()')
self.__session = Session()
website = self.__baseURL + "/login/index.php"
r = self.__session.post(website, data = dict(username = self.__acc[0], password = self.__acc[1]))
if r.url == self.__baseURL + '/login/index.php' or r.status_code != requests.codes.ok:
raise LoginException('Login failed! - Check ur internet connection, username and password')
self.curURL = str(r.url)
soup = BeautifulSoup(self.__session.get(self.curURL).text, 'html.parser')
start = soup.text.find('sesskey')
# "sesskey":"adsasnin", - pattern
self.__sesskey = soup.text[start - 1:].split(',')[0].split(':')[1][1:-1]
def __enter__ (self):
self.login()
return self
def logout (self):
print('Moodle - logout()')
website = self.__baseURL + '/login/logout.php?sesskey=%s' % self.__sesskey
r = self.__session.post(website)
self.__session.close()
self.__session = None
self.__sesskey = None
self.curURL = None
if r.status_code != requests.codes.ok:
raise LogoutException('Logout Failed - Session was closed!')
def __exit__ (self, type, value, traceback):
self.logout()
def moveToStart (self):
self.curURL = self.__baseURL
def listFacilities (self):
self.moveToStart()
soup = BeautifulSoup(self.__session.get(self.curURL).text, 'html.parser')
rows = soup.findAll('a', href = rcompile('http://elearning2\.uni-heidelberg\.de/course/category.php\?id=\d+'))
res = []
for row in rows:
res.append({'ID':row['href'].split('?id=')[1], 'Name':row.text, 'Link':row['href']})
return res
def getFacility (self, partitialName = 'Informatik'):
for fac in self.listFacilities():
if partitialName in fac['Name']:
return fac
return None
def moveToFacility (self, partitialName = 'Informatik'):
self.curURL = self.__session.get(self.getFacility(partitialName)['Link']).url
def listSubFacilitiesOf (self, partitialName = 'Informatik'):
self.moveToFacility(partitialName)
r = self.__session.get(self.curURL)
soup = BeautifulSoup(r.text, 'html.parser')
rows = soup.findAll('a', href = rcompile('https://elearning2\.uni-heidelberg\.de/course/index\.php\?categoryid=\d+$'), attrs={'itemprop':False})
res = []
for row in rows:
res.append({'ID':row['href'].split('?categoryid=')[1], 'Name':row.text, 'Link':row['href']})
return res
def getSubFacility (self, pFacName = 'Informatik', pSFacName = 'Informatik'):
for subFac in self.listSubFacilitiesOf(pFacName):
if pSFacName in subFac['Name']:
return subFac
return None
def listCoursesOf (self, pFacName = 'Informatik', pSFacName = 'Informatik'):
subFacLink = self.getSubFacility(pFacName, pSFacName)['Link']
r = self.__session.get(subFacLink)
soup = BeautifulSoup(r.text, 'html.parser')
elem = soup.find('div', attrs={'class':'course_category_tree clearfix '})
rows = elem.findAll('a', href = rcompile('https://elearning2\.uni-heidelberg\.de/course/view\.php\?id=\d+'))
return [x.text for x in rows]
def findCourse (self, pFacName = 'Informatik', pSFacName = 'Informatik', course = 'Datenstrukt'):
subFacLink = self.getSubFacility(pFacName, pSFacName)['Link']
r = self.__session.get(subFacLink)
soup = BeautifulSoup(r.text, 'html.parser')
rows = soup.findAll('a', href = rcompile('https://elearning2\.uni-heidelberg\.de/course/view\.php\?id=\d+'))
for row in rows:
if course in row.text:
return row['href']
def moveToCourse (self, courseData = ('Informatik', 'Informatik', 'Datenstrukt'), link = ''):
if link == '':
self.curURL = self.findCourse(courseData[0], courseData[1], courseData[2])
else:
self.curURL = link
def moveToAllSubmissionsInCourse (self, sheetNr):
r = self.__session.get(self.curURL)
soup = BeautifulSoup(r.text, 'html.parser')
anchors = soup.findAll('a', onclick = True, href = True)
text = 'Übungsblatt %i Aufgabe' % sheetNr
fil = lambda x: [a for a in x if text == a.text][0]
# TODO random shit
#print('href')
#for a in anchors:
#print(a.text)
sheetLink = fil(anchors)['href']
if sheetLink == None:
raise ValueError('The exercise sheet was not found')
btnAs = BeautifulSoup(self.__session.get(sheetLink).text, 'html.parser').findAll('a', href=True, attrs={'class':'btn'}, text='Alle Abgaben anzeigen')
if len(btnAs) == 1:
self.curURL = btnAs[0]['href']
elif len(btnAs) < 1:
raise ValueError('No btn found!')
else:
raise ValueError('Too much btns found!')
soup = BeautifulSoup(self.__session.get(self.curURL).text, 'html.parser')
classAttrs = soup.find('body').attrs['class']
formData = {}
for attr in classAttrs:
if 'context-' in attr:
formData['contextid'] = attr.split('-')[1]
elif 'cmid-' in attr:
formData['id'] = attr.split('-')[1]
# href contains a link where the userid is a subsequence of
# split the href at ?. right side contains userid (uid)
# pattern of uidElem = id=<num>
uidElem = soup.find('a', href = True, attrs = {'class':'icon menu-action', 'role':'menuitem', 'data-title':'profile,moodle'})
formData['userid'] = uidElem['href'].split('?')[1].split('=')[1]
formData['action'] = 'saveoptions'
formData['sesskey'] = self.__sesskey
formData['_qf__mod_assign_grading_options_form'] = '1'
formData['mform_isexpanded_id_general'] = '0'
formData['perpage'] = '-1'
formData['downloadasfolders'] = '1'
self.__session.post(self.curURL, data = formData)
def extractSubscriptionRowsFor (self, courseData = ('Informatik', 'Informatik', 'Datenstrukt'), link = '', sheetNr = 1, studFilter = None):
self.moveToCourse(courseData, link)
self.moveToAllSubmissionsInCourse(sheetNr)
data = []
soup = BeautifulSoup(self.__session.get(self.curURL).text , 'html.parser')
table = soup.findAll('table', attrs={'class':'flexible generaltable generalbox'})[0]
rows = table.findChildren('tr')
# Row structure
# Cell 0: CheckBox
# Cell 1: Picture
# Cell 2: Name <<<
# Cell 3: E-Mail
# Cell 4: State <<<
# Cell 5: Grade
# Cell 6: Edit
# Cell 7: Last Modified
# Cell 8: Filelink <<<
# Cell 9 - 13: Feedbackstuff
for row in rows:
name = row.find('td', attrs={'class':'cell c2'})
if name != None:
name = name.find('a').text
else:
continue
mail = row.find('td', attrs={'class':'cell c3 email'})
if mail != None:
mail = mail.text
date = row.find('td', attrs={'class':'cell c4'})
if date == None:
continue
oDue = date.find('div', attrs={'class':'overduesubmission'})
if oDue != None:
oDue = oDue.text
else:
lSub = date.find('div', attrs={'class':'latesubmission'})
oDue = '' if lSub is None else lSub.text
subState = date.find('div').text
submFile = row.find('td', attrs={'class':'cell c8'})
if submFile == None:
continue
submFileAnchor = submFile.find('a', href=True)
if submFileAnchor == None:
continue
submFileLink = submFileAnchor['href']
submFileName = submFileAnchor.text
# name, mail, submissionState, overdue, fileLink, fileName
subm = Submission(name, mail, subState, oDue, submFileLink, submFileName)
data.append(subm)
return data if studFilter == None else studFilter.filterList(data)
def downloadSubmissions (self, submissions, destDir):
archives = []
for submission in submissions:
print('Downloading submission of %s in %s...' % (submission['Name'], pjoin(destDir, submission['FileName'])), end = '', flush = True)
r = self.__session.get(submission['Link'], stream = True)
if r.status_code == requests.codes.ok:
archivePath = pjoin(destDir, submission['FileName'])
with open(archivePath, 'wb') as f:
for chunk in r.iter_content(chunk_size = 128):
f.write(chunk)
archives.append(f.name)
print('[OK]')
else:
print('Error @ %s - %s' % (submission['Name'], submission['Link']))
return archives