This repository has been archived by the owner on Jan 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
blackboardxml.py
236 lines (203 loc) · 9.5 KB
/
blackboardxml.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
#
# CURRENTLY SEPERATED AND NOT WORKING
#
# XML Black Board Mobile Endpoint Support
# Emulate Above Classes
# TODO: Refactor XML Classes When Finished. & Refactor Entire Module
import xmltodict
class BlackBoardCourseXML:
"""
"""
def __init__(self, client: BlackBoardClient, **kwargs):
self.client = client
if 'data' in kwargs:
self._course_data = kwargs['data']
if self._course_data is None:
self._course_data = dict()
self.bbid = self._course_data.get('@bbid', None)
self.name = self._course_data.get('@name', None)
self.course_id = self._course_data.get('@courseid', None)
self.role = self._course_data.get('@role', None)
self.is_available = self._course_data.get('@isAvail', None)
self.locale = self._course_data.get('@locale', None)
self.ultra_status = self._course_data.get('@ultraStatus', None)
self.last_access_date = self._course_data.get('@lastAccessDate', None)
self.enrollment_date = self._course_data.get('@enrollmentdate', None)
self.role_identifier = self._course_data.get('@roleIdentifier', None)
self.start_date_duration = self._course_data.get(
'@startDateDuration', None)
self.end_date_duration = self._course_data.get(
'@endDateDuration', None)
self.duration_type = self._course_data.get('@durationType', None)
self.days_from_the_date_of_enrollment = self._course_data.get(
'@daysFromTheDateOfEnrollment', None)
def __str__(self):
return "{} ({})".format(self.name, self.bbid)
def __repr__(self):
return str(self)
def contents(self):
"""
:return:
"""
content_data = xmltodict.parse(self.client.session.get(
self.client.institute.b2_url + "courseMap?course_id=" + self.bbid).text)
if content_data['mobileresponse'] and content_data['mobileresponse']['@status'] == 'OK':
return [BlackBoardContentXML(self, data=content)
for content in content_data['mobileresponse']['map']['map-item']]
return []
def download_all_attachments(self, save_location='./'):
"""
:param save_location:
"""
# Content Iteration Loop
def iterate_with_path(content: BlackBoardContentXML, path=None):
"""
:param content:
:param path:
"""
if path is None:
path = './{}'.format(self.name)
if content.link_type == "resource/x-bb-folder":
path += ("/" + content.name)
for attachment in content.get_attachments():
attachment.download(path)
if content.children is not None:
for child in content.get_children():
iterate_with_path(child, path)
# Content Iteration Start
for c in self.contents():
iterate_with_path(c, "{}/{}".format(save_location, self.name))
_println("Downloaded All Attachments For Course: {}".format(self.name))
class BlackBoardContentXML:
"""
"""
def __init__(self, course, **kwargs):
self.course = course
self.client = course.client
if 'data' in kwargs:
self._content_data = kwargs['data']
else:
self._content_data = dict()
# TODO: Either Split into Multiple More Explicit Classes or Keep Clumped
self.name = self._content_data.get('@name', None)
self.view_url = self._content_data.get('@viewurl', None)
self.is_available = self._content_data.get('@isAvail', None)
self.link_type = self._content_data.get('@linktype', None)
self.link_target = self._content_data.get('@linktarget', None)
self.is_folder = self._content_data.get('@isfolder', None)
self.new = self._content_data.get('@new', None)
self.unread_items = self._content_data.get('@unreadItems', None)
self.unread_key = self._content_data.get('@unreadKey', None)
self.can_mark_as_read = self._content_data.get('@canMarkAsRead', None)
self.content_id = self._content_data.get('@contentid', None)
self.can_post_content_item = self._content_data.get(
'@canpostcontentitem', None)
self.can_attach_files = self._content_data.get(
'@canattachfiles.2.5', None)
self.date_modified = self._content_data.get('@datemodified', None)
self.created_date = self._content_data.get('@createdDate', None)
self.children = self._content_data.get('children', None)
# self.map_item = self._content_data.get('map-item', None)
self.description = self._content_data.get('description', None)
# object containing attachment object
self.attachments = self._content_data.get('attachments', None)
# List or dictonary depndding on number of items. represents attachment object
# self.attachment = self._content_data.get('attachment', None)
# Attachment Info
self.url = self._content_data.get('@url', None)
self.link_label = self._content_data.get('@linkLabel', None)
self.modified_date = self._content_data.get('@modifiedDate', None)
# probably Assignment
self.due_date = self._content_data.get('@dueDate', None)
self.due_today = self._content_data.get('@dueToday', None)
self.past_due = self._content_data.get('@pastDue', None)
self.due_tomorrow = self._content_data.get('@dueTomorrow', None)
self.due_after_tomorrow = self._content_data.get(
'@dueAfterTomorrow', None)
self.submission_number = self._content_data.get(
'@submissionNumber', None)
self.max_number_of_submission = self._content_data.get(
'@maxNumberOfSubmission', None)
self.items_due_today = self._content_data.get('@itemsDueToday', None)
self.items_past_due = self._content_data.get('@itemsPastDue', None)
self.total_items = self._content_data.get('@totalItems', None)
self.linked_folder = self._content_data.get('@linkedFolder', None)
self.type = self._content_data.get('@type', None)
self.assessment_type = self._content_data.get('@assessment_type', None)
self.is_assessment_mobile_friendly = self._content_data.get(
'@is_assessment_mobile_friendly', None)
def __str__(self):
return "{} ({})".format(self.name, self.content_id)
def __repr__(self):
return str(self)
def get_children(self):
"""
:return:
"""
if self.children is not None:
# _println(type(self.children["map-item"]))
if type(self.children["map-item"]) is list:
return [BlackBoardContentXML(self.course, data=content) for content in self.children["map-item"]]
elif type(self.children["map-item"]) is dict:
return [BlackBoardContentXML(self.course, data=self.children["map-item"])]
return []
# TODO: Figure Out if I Want Separate Attachment Class Like With API
def get_attachments(self):
"""
:return:
"""
if self.link_type in ("resource/x-bb-file", "resource/x-bb-document", "resource/x-bb-assignment"):
if self.attachments is not None:
if type(self.attachments['attachment']) is list:
return [BlackBoardAttachmentXML(self, content) for content in self.attachments['attachment']]
elif type(self.attachments['attachment']) is dict:
return [BlackBoardAttachmentXML(self, self.attachments['attachment'])]
return []
class BlackBoardAttachmentXML:
"""
"""
def __init__(self, content, attachment_data):
self.content = content
self.course = content.course
self.client = content.course.client
self.__attachment_data = attachment_data
self.name = attachment_data['@name']
self.url = attachment_data['@url']
self.link_label = attachment_data['@linkLabel']
self.link_label_safe = re.sub(
'[<>:"/\\|?*]', '', self.link_label) if self.link_label is not None else ''
self.modified_date = attachment_data['@modifiedDate']
def __str__(self):
return "{}".format(self.link_label)
def __repr__(self):
return str(self)
def download(self, location=None, **kwargs):
"""
:param location:
:param kwargs:
:return:
"""
download_location = (
"./{}" if location is None else location + "/{}").format(self.link_label_safe)
download = self.client.session.get(
self.client.institute.display_lms_host + self.url)
if download.status_code == 302:
_println("File Located at: {}".format(
download.headers.get("Location", "")))
# Navigate to Location
# TODO: Add Redirect Navigation
elif download.status_code == 200:
if not os.path.exists(location):
os.makedirs(location)
if os.path.isfile(download_location):
# Check if Overwrite
_println("File Exists No Download..")
# TODO: Make Manifest Option
return
try:
with open(download_location, 'wb') as file_out:
file_out.write(download.content)
_println("Downloaded: {}\nto: {}\n".format(
self.link_label_safe, location))
except FileNotFoundError:
_println(f"{self.link_label_safe} @ {location} NOT FOUND\n\n")