-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun.py
85 lines (74 loc) · 2.37 KB
/
run.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
from moodlereader import MoodleBackupReader, FILE_PREFIX
def update_html_refs(prefix, html, file_refs, files):
for file_ref in file_refs:
html = html.replace(file_ref, prefix + files[file_ref]["url"])
return html
reader = MoodleBackupReader("moodlebackup.mbz")
reader.open()
reader.read()
sections = reader.sections
section_ids = reader.section_ids
activities = reader.activities
files = reader.files
file_contexts = reader.file_contexts
url_prefix = "https://www.openlearning.com/courses/my_course"
pages = {}
for module_id, activity in activities.items():
blocks = []
is_missing = False
if activity["type"] == "page":
blocks.append({
"html": update_html_refs(url_prefix, activity["content"], activity["files"], files),
})
elif activity["type"] == "book":
for chapter in activity["chapters"]:
blocks.append({
"html": update_html_refs(url_prefix, chapter["content"], chapter["files"], files),
"title": chapter["title"],
})
elif activity["type"] == "resource":
print(file_contexts[activity["contextid"]])
blocks.append({
"title": activity["name"],
"file": update_html_refs(
url_prefix,
file_contexts[activity["contextid"]],
file_contexts[activity["contextid"]],
files
)
})
else:
is_missing = True
if is_missing and "id" in activity:
pages[module_id] = {
"title": activity["title"],
"name": activity["module_name"] + " not imported",
"blocks": [],
"intro": ""
}
elif not is_missing:
pages[module_id] = {
"title": activity["title"],
"name": activity["name"],
"blocks": blocks,
"intro": activity["intro"]
}
modules = []
missing = {
"title": "Missing content",
"name": "Missing content",
"blocks": [],
"intro": ""
}
for section_id in section_ids:
section = sections[section_id]
modules.append({
"title": section["title"],
"html": update_html_refs(url_prefix, section["summary"], section["files"], files),
"pages": [
pages.get(key, missing) for key in section.get("sequence")
]
})
import json
print(json.dumps(modules, indent=2))
reader.close()