-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmoodle.py
More file actions
207 lines (159 loc) · 6.58 KB
/
moodle.py
File metadata and controls
207 lines (159 loc) · 6.58 KB
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
#!/usr/bin/env python
# -*- coding: utf8 -*-
from bs4 import BeautifulSoup
import datetime
import mechanize
import urlparse
__author__ = 'uli'
class CourseNotFound(Exception):
pass
class CourseMissing(Exception):
pass
class ParsingError(Exception):
pass
class Moodle(object):
def __init__(self, base_url='https://csimoodle.ucd.ie/moodle/'):
self.base_url = base_url
self.browser = mechanize.Browser(factory=mechanize.RobustFactory())
self._course = None
self._course_id = None
def _select_form_by_id(self, name):
number = 0
for offset, form in enumerate(self.browser.forms()):
if not "id" in form.attrs or form.attrs["id"] != name:
continue
number = offset
break
else:
raise ParsingError
self.browser.select_form(nr=number)
def login(self, user, password):
self.browser.open("{0}/login/".format(self.base_url))
self._select_form_by_id(u"login")
self.browser["username"] = user
self.browser["password"] = password
self.browser.submit()
@property
def course(self):
return self._course
@course.setter
def course(self, course):
self._course = course
self.browser.open("{0}".format(self.base_url))
links = list(self.browser.links(text_regex=course))
if not links:
raise CourseNotFound
l = links[0]
self._course_id = urlparse.parse_qs(urlparse.urlparse(l.url).query)["id"][0]
self._course_url = l.url
def check_questions(self, category=None, func=None):
if not self._course_id:
raise CourseMissing
self.browser.open("{0}/question/edit.php?courseid={1}".format(self.base_url, self._course_id))
if category:
self._select_form_by_id(u"catmenu")
categories = self.browser.form.find_control("category")
for cat in categories.items:
text = unicode([label.text for label in cat.get_labels()])
if category in text:
self.browser["category"] = [cat.name]
self.browser.submit()
break
else:
raise ParsingError
for link in list(self.browser.links(url_regex="{0}question/question.php".format(self.base_url))):
if dict(link.attrs)["title"] != u"Edit":
continue
self.browser.follow_link(link)
if func:
func(self)
def get_points(self, tag):
res = tag.find('del')
if res:
res.extract()
return float(tag.text[1:])
else:
return float(tag.text)
def extract_quiz(self, quiz):
self.browser.open("{0}".format(self._course_url))
link = list(self.browser.links(text_regex=quiz))
quiz_id = urlparse.parse_qs(urlparse.urlparse(link[0].url).query)["id"][0]
report_url = "{0}/mod/quiz/report.php?id={1}&&mode=overview&pagesize=1000".format(self.base_url, quiz_id)
self.browser.open(report_url)
self._select_form_by_id("attemptsform")
response = self.browser.response()
soup = BeautifulSoup(response.read())
def has_class_but_no_id(tag):
return tag.name == 'tr' and tag.has_attr('id') and 'mod-quiz-report-overview-report' in tag.attrs['id'] \
and not 'emptyrow' in tag.attrs['class']
results = []
for item in soup.findAll(has_class_but_no_id):
if not len(item.contents) >= 3:
continue
links = item.contents[2].find_all("a")
if not links:
continue
name = links[0].string
email = item.contents[3].string
review_link = links[1].attrs['href']
start = datetime.datetime.strptime(item.contents[5].string, '%d %B %Y %I:%M %p')
end = datetime.datetime.strptime(item.contents[6].string, '%d %B %Y %I:%M %p')
total_points = self.get_points(item.contents[8])
points = []
current_tag = item.contents[9]
while current_tag.next_sibling:
current_tag = current_tag.next_sibling
points.append(self.get_points(current_tag))
q = self._extract_questions("{0}&showall=1".format(review_link))
result = {}
result["name"] = name
result["email"] = email
result["start"] = start
result["end"] = end
result["duration"] = end - start
result["total_points"] = total_points
result["points"] = points
result["questions"] = q
results.append(result)
return results
def _extract_questions(self, url):
self.browser.open(url)
response = self.browser.response()
soup = BeautifulSoup(response.read())
results = []
tag = soup.find("div", id="q1")
first = True
while first or tag.next_sibling:
result = {}
if not first and tag.next_sibling:
tag = tag.next_sibling
else:
first = False
if not "id" in tag.attrs:
continue
result["answer"] = tag.textarea.text
url = tag.find(text="Edit question").parent.attrs['href']
question_id = urlparse.parse_qs(urlparse.urlparse(url).query)["id"][0]
result["question"] = question_id
history = tag.find("div", attrs={'class': 'history'})
tries = int(history.find("tr", attrs={'class': 'lastrow'}).contents[1].text)
result["tries"] = tries
result["points"] = float(tag.find(attrs={'class': 'grade'}).string.replace("Mark ", "").split(" ")[0])
history_answers = []
for offset, item in enumerate(history.tbody.findAll("tr")):
histitem = {}
if offset == 0:
continue
if not item.contents[5].string.startswith("Submit:"):
continue
answer = item.contents[5].string.replace("Submit:", "", 1)
histitem["answer"] = answer
try:
histitem["points"] = self.get_points(item.contents[9])
except ValueError:
histitem["points"] = 0
histitem["time"] = datetime.datetime.strptime(item.contents[3].string, '%d/%m/%y, %H:%M')
history_answers.append(histitem)
result["history"] = history_answers
results.append(result)
return results