-
Notifications
You must be signed in to change notification settings - Fork 2
/
task.go
120 lines (99 loc) · 2.63 KB
/
task.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
package geektime
import (
"path/filepath"
)
type task interface {
run()
}
type courseFetcher struct {
bus *bus
introURL, courseID, cookie, outputDir string
}
func (f *courseFetcher) run() {
c, err := fetchCourse(f.introURL, f.courseID, f.cookie)
f.bus.post(eventCourse, courseRet{c, err})
}
type articlesFetcher struct {
bus *bus
articlesURL, courseID, cookie string
}
func (f *articlesFetcher) run() {
as, err := fetchArticles(f.articlesURL, f.courseID, f.cookie)
f.bus.post(eventArticles, articles{as, err})
}
type articleVideoFetcher struct {
bus *bus
articleURL, courseID, cookie string
articleID int
videoID string
}
func (f *articleVideoFetcher) run() {
v, err := fetchVideoOfArticle(f.articleURL, f.courseID, f.cookie, f.articleID)
f.bus.post(eventArticleVideo, articleVideo{f.articleID, v.Name, f.videoID, err})
}
type playAuthFetcher struct {
bus *bus
name string
videoPlayAuthURL, videoID, cookie string
articleID int
}
func (f *playAuthFetcher) run() {
auth, err := fetchVideoPlayAuth(f.videoPlayAuthURL, f.cookie, f.articleID, 1, f.videoID)
f.bus.post(eventPlayAuth, playAuth{auth, f.name, f.articleID, f.videoID, err})
}
type playListFetcher struct {
bus *bus
name string
articleID int
playListURL, videoID string
auth videoPlayAuth
}
func (f *playListFetcher) run() {
l, err := fetchPlayList(f.playListURL, f.videoID, f.auth)
f.bus.post(eventPlayList, playListRet{l, f.name, f.articleID, err})
}
type m3u8Fetcher struct {
bus *bus
name string
articleID int
urls []string
outputDir string
}
func (f *m3u8Fetcher) run() {
var (
url, path string
err error
)
for _, url = range f.urls {
path, err = download(url, f.outputDir)
if err == nil {
break
}
}
if err != nil {
f.bus.post(eventM3U8Parsed, m3u8{articleID: f.articleID, err: err})
return
}
writeFile(filepath.Join(f.outputDir, "m3u8_addr"), []byte(url))
ts, err := parseM3u8(path)
if err != nil {
f.bus.post(eventM3U8Parsed, m3u8{articleID: f.articleID, err: err})
return
}
f.bus.post(eventM3U8Parsed, m3u8{
name: f.name,
articleID: f.articleID,
ts: ts,
m3u8URL: url,
outputDir: f.outputDir,
})
}
type tsDownloader struct {
bus *bus
articleID int
url, outputDir string
}
func (d *tsDownloader) run() {
_, err := download(d.url, d.outputDir)
d.bus.post(eventDownloadTS, downloadTS{d.url, d.articleID, err})
}