This repository was archived by the owner on Jul 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsectionresolver.go
More file actions
277 lines (226 loc) · 7.48 KB
/
sectionresolver.go
File metadata and controls
277 lines (226 loc) · 7.48 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
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
package insidescraper
// ResolvedSection stores optimized data.
type ResolvedSection struct {
*SiteData
ID string
Content []ContentReference
Audio map[string]Media
// AudioCount contains the total number of audio classes contained in this section,
// including all descendant sections.
AudioCount int
}
// ContentReference can refer to any of section, lesson, or media
type ContentReference struct {
Type DataType
// Reference can either be the ID of a section or lesson, or a media source URL.
Reference string
}
// ResolvingItem is an interim object during resolution.
type ResolvingItem struct {
Type DataType
SectionID string
// Audio is for when a section consists only of a single audio.
Audio *Media
// Lesson is for when a section is really just a lesson.
Lesson *Lesson
}
// SectionResolver optimizes data structure.
type SectionResolver struct {
// Site is the original Site
Site Site
// NeweSite is the resolved Site
ResolvedSite ResolvedSite
}
// ResolvedSite contains all site data.
type ResolvedSite struct {
Sections map[string]ResolvedSection
Lessons map[string]Lesson
// IDs of all top level sections.
TopLevel []TopItem
}
// ResolveSite resolves the Site into an optimized site.
func (resolver *SectionResolver) ResolveSite() {
resolver.ResolvedSite = ResolvedSite{
TopLevel: resolver.Site.TopLevel,
Sections: make(map[string]ResolvedSection),
Lessons: make(map[string]Lesson),
}
for _, topSection := range resolver.Site.TopLevel {
resolver.ResolveSection(topSection.ID)
}
}
// ResolveSection converts the given section into its most efficiant
// representation.
func (resolver *SectionResolver) ResolveSection(sectionID string) *ResolvingItem {
// If this item was already resolved, don't do it again.
if _, exists := resolver.ResolvedSite.Sections[sectionID]; exists {
return &ResolvingItem{
Type: SectionType,
SectionID: sectionID,
}
}
section := resolver.Site.Sections[sectionID]
if section.AudioCount == 1 {
if len(section.Lessons) > 0 {
lesson := resolver.Site.Lessons[section.Lessons[0]]
media := resolver.ResolveMedia(lesson.Audio[0], lesson.SiteData)
return &ResolvingItem{
Type: MediaType,
Audio: &media,
}
}
}
if !(len(section.Sections) > 0 && len(section.Lessons) > 0) {
if len(section.Sections) == 1 {
return resolver.ResolveSection(section.Sections[0])
}
if len(section.Sections) > 0 {
if resolver.isEverySectionMedia(sectionID) {
return &ResolvingItem{
Type: LessonType,
Lesson: resolver.simpleSectionsToLesson(sectionID),
}
}
} else if resolver.isEveryLessonMedia(sectionID) {
return &ResolvingItem{
Type: LessonType,
Lesson: resolver.simpleLessonsToLesson(sectionID),
}
}
}
// TODO: Better resolve lessons. Resolve lessons to parent (current section).
// If a lesson just has one media, add it to parent.
// Otherwise, reference the lesson in parent, and add lesson to resolved output.
resolver.ResolvedSite.Sections[sectionID] = ResolvedSection{
SiteData: section.SiteData,
ID: sectionID,
AudioCount: section.AudioCount,
Content: make([]ContentReference, 0),
Audio: make(map[string]Media),
}
// Incorporate all the lessons. If its a single audio, is absorbed into parent section.
for _, lessonID := range section.Lessons {
resolver.useResolvedToParent(resolver.resolveLesson(lessonID), sectionID)
}
// Finally, if this is a real, complicated section, resolve all of its sub sections.
for _, subsectionID := range section.Sections {
resolver.useResolvedToParent(resolver.ResolveSection(subsectionID), sectionID)
}
return &ResolvingItem{
Type: SectionType,
SectionID: sectionID,
}
}
// resolveLessons resolves lesson into reference. If a lesson is just a single media, turned into media.
func (resolver *SectionResolver) resolveLesson(lessonID string) *ResolvingItem {
lesson := resolver.Site.Lessons[lessonID]
if len(lesson.Audio) == 1 {
audio := resolver.ResolveMedia(lesson.Audio[0], lesson.SiteData)
return &ResolvingItem{
Type: MediaType,
Audio: &audio,
}
}
if len(lesson.Audio) == 0 {
return &ResolvingItem{
Type: MediaType,
Audio: nil,
}
}
return &ResolvingItem{
Type: LessonType,
Lesson: &lesson,
}
}
// useResolvedToParent integrates the resolved section into the parent.
func (resolver *SectionResolver) useResolvedToParent(resolved *ResolvingItem, parentID string) {
parent := resolver.ResolvedSite.Sections[parentID]
if resolved.Type == LessonType || resolved.Type == SectionType {
reference := resolved.SectionID
if resolved.Type == LessonType {
reference = resolved.Lesson.ID
}
parent.Content = append(parent.Content, ContentReference{
Type: resolved.Type,
Reference: reference,
})
}
// For a lesson, also add it to the lesson map.
if resolved.Type == LessonType {
resolver.ResolvedSite.Lessons[resolved.Lesson.ID] = *resolved.Lesson
} else if resolved.Type == MediaType && resolved.Audio != nil {
parent.Audio[resolved.Audio.Source] = *resolved.Audio
parent.Content = append(parent.Content, ContentReference{
Type: MediaType,
Reference: resolved.Audio.Source,
})
}
resolver.ResolvedSite.Sections[parentID] = parent
}
// simpleContentToLesson converts the given section to a lesson.
func (resolver *SectionResolver) simpleContentToLesson(sectionID string, sourceIDs []string, toAudio func(ID string) Media) *Lesson {
section := resolver.Site.Sections[sectionID]
audio := make([]Media, 0)
for _, ID := range sourceIDs {
audio = append(audio, toAudio(ID))
}
return &Lesson{
ID: section.ID,
SiteData: section.SiteData,
Audio: audio,
}
}
// simpleLessonsToLesson converts from section which has all lessons with one
// class to just one lesson.
func (resolver *SectionResolver) simpleLessonsToLesson(sectionID string) *Lesson {
section := resolver.Site.Sections[sectionID]
return resolver.simpleContentToLesson(sectionID, section.Lessons, func(id string) Media {
lesson := resolver.Site.Lessons[id]
if len(lesson.Audio) == 1 {
return resolver.ResolveMedia(lesson.Audio[0], lesson.SiteData)
}
return Media{}
})
}
// simpleSectionsToLesson converts from all child sections having just one
// lesson to one lesson with all that content.
func (resolver *SectionResolver) simpleSectionsToLesson(sectionID string) *Lesson {
return resolver.simpleContentToLesson(sectionID, resolver.Site.Sections[sectionID].Lessons, func(id string) Media {
return *resolver.ResolveSection(id).Audio
})
}
// IsEverySectionMedia checks if the given section is really
// just a lesson.
func (resolver *SectionResolver) isEverySectionMedia(sectionID string) bool {
section := resolver.Site.Sections[sectionID]
for _, subSection := range section.Sections {
if resolver.Site.Sections[subSection].AudioCount > 1 {
return false
}
}
return true
}
// isEveryLessonMedia checks if all lessons in section have just one media.
func (resolver *SectionResolver) isEveryLessonMedia(sectionID string) bool {
section := resolver.Site.Sections[sectionID]
for _, lessonID := range section.Lessons {
if len(resolver.Site.Lessons[lessonID].Audio) > 1 {
return false
}
}
return true
}
// ResolveMedia gives the given media all of its data.
func (resolver *SectionResolver) ResolveMedia(audio Media, lesson *SiteData) Media {
title := audio.Title
if len(title) == 0 {
title = lesson.Title
}
description := audio.Description
if len(description) == 0 {
description = lesson.Description
}
audio.Title = title
audio.Description = description
return audio
}