-
Notifications
You must be signed in to change notification settings - Fork 2
/
driver.go
238 lines (197 loc) · 4.78 KB
/
driver.go
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
package geektime
import (
"errors"
"path/filepath"
"strings"
)
const (
defaultWorkerCount = 512
)
// Config the configuration for creating pipeline
type Config struct {
ArticlesURL string
ArticleURL string
IntroURL string
VideoPlayAuthURL string
PlayListURL string
CourseID string
OutputDir string
Cookie string
Overwrite bool
}
type driver struct {
Config
bus *bus
apiExecutor *executor
downloadExecutor *executor
playAuthExecutor *executor
course course
}
func newDriver(conf Config, bus *bus) (*driver, error) {
wrapErr := func(m string) error {
return errors.New("create driver error:" + m)
}
if conf.CourseID == "" {
return nil, wrapErr("empty course id")
}
if conf.Cookie == "" {
return nil, wrapErr("empty cookie")
}
if conf.ArticlesURL == "" {
conf.ArticlesURL = defaultArticlesURL
}
if conf.ArticleURL == "" {
conf.ArticleURL = defaultArticleURL
}
if conf.IntroURL == "" {
conf.IntroURL = defaultIntroURL
}
if conf.VideoPlayAuthURL == "" {
conf.VideoPlayAuthURL = defaultVideoPlayAuthURL
}
if conf.PlayListURL == "" {
conf.PlayListURL = defaultPlayListURL
}
return &driver{
Config: conf,
bus: bus,
apiExecutor: newExecutor(128, 256),
playAuthExecutor: newExecutor(1, 256), // for the rate limit
downloadExecutor: newExecutor(512, 4096),
}, nil
}
// Start starts download
func (p *driver) Start() {
p.subscribeEvents()
p.apiExecutor.start()
p.playAuthExecutor.start()
p.downloadExecutor.start()
p.apiExecutor.execute(&courseFetcher{p.bus, p.IntroURL, p.CourseID, p.Cookie, p.OutputDir})
p.apiExecutor.execute(&articlesFetcher{p.bus, p.ArticlesURL, p.CourseID, p.Cookie})
}
// Abort shutdowns the executor
func (p *driver) Abort() {
p.apiExecutor.shutdown()
p.playAuthExecutor.shutdown()
p.downloadExecutor.shutdown()
}
func (p *driver) subscribeEvents() {
p.bus.subscribe(eventCourse, func(v interface{}) { p.onCourse(v.(courseRet)) })
p.bus.subscribe(eventArticles, func(v interface{}) { p.onActicles(v.(articles)) })
p.bus.subscribe(eventArticleVideo, func(v interface{}) { p.onActicleVideo(v.(articleVideo)) })
p.bus.subscribe(eventPlayAuth, func(v interface{}) { p.onPlayAuth(v.(playAuth)) })
p.bus.subscribe(eventPlayList, func(v interface{}) { p.onPlayList(v.(playListRet)) })
p.bus.subscribe(eventM3U8Parsed, func(v interface{}) { p.onM3U8(v.(m3u8)) })
}
func (p *driver) onCourse(c courseRet) {
if c.err != nil {
return
}
p.course = c.course
makeSureDirExist(filepath.Join(p.OutputDir, c.course.Title))
}
func (p *driver) onActicles(as articles) {
if as.err != nil {
return
}
for _, a := range as.articles {
if isAudio(a) {
p.procAudit(a)
} else if a.VideoID != "" {
p.apiExecutor.execute(&articleVideoFetcher{
bus: p.bus,
articleURL: p.ArticleURL,
courseID: p.CourseID,
cookie: p.Cookie,
articleID: a.ID,
videoID: a.VideoID,
})
} else {
p.bus.post(eventArticleFinished, a.ID)
}
}
}
func (p *driver) procAudit(a article) {
outputDir := filepath.Join(p.OutputDir, p.course.Title, simplify(a.Title))
err := makeSureDirExist(outputDir)
if err != nil {
p.bus.post(eventCreateArticleFoldFailed, a.ID)
return
}
p.apiExecutor.execute(&m3u8Fetcher{
p.bus,
a.Title,
a.ID,
[]string{a.AuditM3U8URL},
outputDir,
})
}
func isAudio(a article) bool {
return a.AuditM3U8URL != ""
}
func (p *driver) onActicleVideo(av articleVideo) {
if av.err != nil {
return
}
name := simplify(av.name)
err := makeSureDirExist(filepath.Join(p.OutputDir, p.course.Title, name))
if err != nil {
p.bus.post(eventCreateArticleFoldFailed, av.articleID)
return
}
p.apiExecutor.execute(&playAuthFetcher{p.bus, name, p.VideoPlayAuthURL, av.id, p.Cookie, av.articleID})
}
func (p *driver) onPlayAuth(pa playAuth) {
if pa.err == nil {
p.playAuthExecutor.execute(&playListFetcher{
p.bus,
pa.name,
pa.articleID,
p.PlayListURL,
pa.videoID,
pa.auth,
})
}
}
func (p *driver) onPlayList(l playListRet) {
if l.err != nil {
return
}
urls := make([]string, len(l.list.PlayInfoList.PlayInfo))
for i, p := range l.list.PlayInfoList.PlayInfo {
urls[i] = p.PlayURL
}
vp := filepath.Join(p.OutputDir, p.course.Title, l.name)
p.apiExecutor.execute(&m3u8Fetcher{
p.bus,
l.name,
l.articleID,
urls,
vp,
})
}
func (p *driver) onM3U8(m m3u8) {
if m.err != nil {
return
}
path := m.m3u8URL[:strings.LastIndexByte(m.m3u8URL, '/')+1]
for _, ts := range m.ts {
p.downloadExecutor.execute(&tsDownloader{
p.bus,
m.articleID,
path + ts,
m.outputDir,
})
}
}
func simplify(t string) string {
invalidChars := `| \::*??“”",,()()`
var nr []rune
for _, r := range t {
if strings.IndexRune(invalidChars, r) >= 0 {
continue
}
nr = append(nr, r)
}
return string(nr)
}