-
Notifications
You must be signed in to change notification settings - Fork 2
/
event.go
163 lines (138 loc) · 3.12 KB
/
event.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
package geektime
import (
"fmt"
"strings"
)
type event int
const (
// eventCourse course fetched
eventCourse event = iota
// eventArticles articles is fetched
eventArticles
eventArticleFinished
// eventArticleVideo get the video information of the article
eventArticleVideo
// eventPlayAuth get the play auth
eventPlayAuth
// eventPlayList get the play list which contains video url
eventPlayList
// eventDownloadTS the ts file is downloaded
eventDownloadTS
// eventM3U8Parsed the m3u8 file is parsed
eventM3U8Parsed
eventCreateArticleFoldFailed
eventUIUpdateProgress
eventUIProgressTotal
eventCount
)
var (
eventNames = []string{
"course",
"articles",
"articleFinished",
"articleVideo",
"playAuth",
"playList",
"downloadTS",
"m3u8Parsed",
"createVideoFoldFailed",
"uiUpdateProgress",
"uiProgressTotal",
}
)
func (e event) String() string {
return eventNames[e]
}
type bus struct {
eventHandlers [eventCount][]func(interface{})
}
func (b *bus) subscribe(e event, f func(interface{})) {
b.eventHandlers[e] = append(b.eventHandlers[e], f)
}
func (b *bus) post(e event, v interface{}) {
for _, f := range b.eventHandlers[e] {
f(v)
}
}
type courseRet struct {
course course
err error
}
func (c courseRet) String() string {
return fmt.Sprintf("course ret[course:%s,%s]", c.course.String(), errMsg(c.err))
}
type articles struct {
articles []article
err error
}
func (a articles) String() string {
as := make([]string, len(a.articles))
for i := range a.articles {
as[i] = a.articles[i].String()
}
return fmt.Sprintf("articles:[articles:{%s},%s]", strings.Join(as, ","), errMsg(a.err))
}
type articleVideo struct {
articleID int
name string
id string
err error
}
func (av articleVideo) String() string {
return fmt.Sprintf(
"articleVideo:[articleID:%d,name:%s,videoID:%s,%s]",
av.articleID, av.name, av.id, errMsg(av.err),
)
}
type playAuth struct {
auth videoPlayAuth
name string
articleID int
videoID string
err error
}
func (p playAuth) String() string {
as := fmt.Sprintf("%+v", p.auth)
return fmt.Sprintf(
"playAuth:[auth:%s,name:%s,artilceID:%d,videoID:%s,%s]",
as, p.name, p.articleID, p.videoID, errMsg(p.err),
)
}
type playListRet struct {
list playList
name string
articleID int
err error
}
func (p playListRet) String() string {
ls := fmt.Sprintf("%+v", p.list)
return fmt.Sprintf("playListRet:[list:%s,name:%s,articleID:%d,%s]", ls, p.name, p.articleID, errMsg(p.err))
}
type downloadTS struct {
url string
articleID int
err error
}
func (d downloadTS) String() string {
return fmt.Sprintf("downloadTS:[articleID:%d,url:%s,%s", d.articleID, d.url, errMsg(d.err))
}
type m3u8 struct {
name string
articleID int
ts []string
m3u8URL string
outputDir string
err error
}
func (m m3u8) String() string {
return fmt.Sprintf(
"m3u8:[name:%s,articleID:%d,ts count:%d,m3u8URL:%s,outputDir:%s,%s]",
m.name, m.articleID, len(m.ts), m.m3u8URL, m.outputDir, errMsg(m.err),
)
}
func errMsg(err error) string {
if err == nil {
return "suc"
}
return "error:" + err.Error()
}