-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjson2node.py
More file actions
119 lines (100 loc) · 4.14 KB
/
json2node.py
File metadata and controls
119 lines (100 loc) · 4.14 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
import json
from collections import OrderedDict
from abc import ABC, abstractmethod
from le_utils.constants import content_kinds
class Node:
def __init__(self, title=None, source_id=None, lang="en", author=None, license=None):
self.title = title
self.source_id = source_id
self.tree_nodes = OrderedDict()
self.lang = lang
self.description = None
self.author = author
self.license = license
def add_node(self, obj):
node = obj.to_dict()
if node is not None:
self.tree_nodes[node["source_id"]] = node
def to_dict(self):
return dict(
kind=content_kinds.TOPIC,
source_id=self.source_id,
title=self.title,
description=self.description,
language=self.lang,
author=self.author,
license=self.license,
children=list(self.tree_nodes.values())
)
class GradeJsonTree:
def __init__(self, *args, subject_node=None, **kwargs):
self.grades = []
self.subject_node = subject_node
def load(self, filename, auto_parse=False, author=None, license=None,
save_url_to=None, load_video_list=False):
with open(filename, "r") as f:
grades = json.load(f)
for grade in grades:
grade_obj = GradeNode(title=grade["title"],
source_id=grade["source_id"],
author=author,
license=license)
if "subjects" in grade:
for subject in grade["subjects"]:
subject_obj = self.subject_node(title=subject["title"],
source_id=subject["source_id"],
lang=subject["lang"],
author=author,
license=license)
subject_obj.auto_generate_lessons(subject["lessons"],
save_url_to=save_url_to, load_video_list=load_video_list)
grade_obj.add_subject(subject_obj)
self.grades.append(grade_obj)
elif "lessons" in grade:
for lesson in grade["lessons"]:
lesson_obj = self.subject_node(title=lesson,
source_id=lesson,
lang=grade["lang"],
author=author,
license=license)
lesson_obj.auto_generate_lessons(lesson,
save_url_to=save_url_to, load_video_list=load_video_list)
grade_obj.add_subject(lesson_obj)
self.grades.append(grade_obj)
def __iter__(self):
return iter(self.grades)
class GradeNode(Node):
def __init__(self, *args, **kwargs):
super(GradeNode, self).__init__(*args, **kwargs)
self.subjects = []
def add_subject(self, subject):
self.subjects.append(subject)
class ABCSubjectNode(ABC, Node):
def __init__(self, *args, **kwargs):
Node.__init__(self, *args, **kwargs)
self.lessons = []
@abstractmethod
def auto_generate_lessons(self, urls):
raise NotImplementedError
class ABCLessonNode(ABC, Node):
def __init__(self, *args, **kwargs):
Node.__init__(self, *args, **kwargs)
self.item = None
@abstractmethod
def download(self, download=True, base_path=None):
raise NotImplementedError
def to_dict(self):
children = list(self.tree_nodes.values())
if len(children) == 1:
return children[0]
else:
return dict(
kind=content_kinds.TOPIC,
source_id=self.source_id,
title=self.title,
description=self.description,
language=self.lang,
author=self.author,
license=self.license,
children=children
)