-
Notifications
You must be signed in to change notification settings - Fork 501
/
lives.go
executable file
·174 lines (151 loc) · 3.5 KB
/
lives.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
//go:generate mockgen -package mock -destination mock/mock.go github.com/hr3lxphr6j/bililive-go/src/live Live
package live
import (
"errors"
"net/http"
"net/http/cookiejar"
"net/url"
"strings"
"time"
"github.com/bluele/gcache"
)
var (
m = make(map[string]Builder)
InitializingLiveBuilderInstance InitializingLiveBuilder
)
func Register(domain string, b Builder) {
m[domain] = b
}
func getBuilder(domain string) (Builder, bool) {
builder, ok := m[domain]
return builder, ok
}
type Builder interface {
Build(*url.URL, ...Option) (Live, error)
}
type InitializingLiveBuilder interface {
Build(Live, *url.URL, ...Option) (Live, error)
}
type InitializingFinishedParam struct {
InitializingLive Live
Live Live
Info *Info
}
type Options struct {
Cookies *cookiejar.Jar
Quality int
AudioOnly bool
}
func NewOptions(opts ...Option) (*Options, error) {
cookieJar, err := cookiejar.New(&cookiejar.Options{})
if err != nil {
return nil, err
}
options := &Options{Cookies: cookieJar, Quality: 0}
for _, opt := range opts {
opt(options)
}
return options, nil
}
func MustNewOptions(opts ...Option) *Options {
options, err := NewOptions(opts...)
if err != nil {
panic(err)
}
return options
}
type Option func(*Options)
func WithKVStringCookies(u *url.URL, cookies string) Option {
return func(opts *Options) {
cookiesList := make([]*http.Cookie, 0)
for _, pairStr := range strings.Split(cookies, ";") {
pairs := strings.SplitN(pairStr, "=", 2)
if len(pairs) != 2 {
continue
}
cookiesList = append(cookiesList, &http.Cookie{
Name: strings.TrimSpace(pairs[0]),
Value: strings.TrimSpace(pairs[1]),
})
}
opts.Cookies.SetCookies(u, cookiesList)
}
}
func WithQuality(quality int) Option {
return func(opts *Options) {
opts.Quality = quality
}
}
func WithAudioOnly(audioOnly bool) Option {
return func(opts *Options) {
opts.AudioOnly = audioOnly
}
}
type ID string
type StreamUrlInfo struct {
Url *url.URL
Name string
Description string
Resolution int
Vbitrate int
}
type Live interface {
SetLiveIdByString(string)
GetLiveId() ID
GetRawUrl() string
GetInfo() (*Info, error)
GetStreamUrls() ([]*url.URL, error)
GetPlatformCNName() string
GetLastStartTime() time.Time
SetLastStartTime(time.Time)
GetHeadersForDownloader() map[string]string
}
type WrappedLive struct {
Live
cache gcache.Cache
}
func newWrappedLive(live Live, cache gcache.Cache) Live {
return &WrappedLive{
Live: live,
cache: cache,
}
}
func (w *WrappedLive) GetInfo() (*Info, error) {
i, err := w.Live.GetInfo()
if err != nil {
if info, err2 := w.cache.Get(w); err2 == nil {
info.(*Info).RoomName = err.Error()
}
return nil, err
}
if w.cache != nil {
w.cache.Set(w, i)
}
return i, nil
}
func New(url *url.URL, cache gcache.Cache, opts ...Option) (live Live, err error) {
builder, ok := getBuilder(url.Host)
if !ok {
return nil, errors.New("not support this url")
}
live, err = builder.Build(url, opts...)
if err != nil {
return
}
live = newWrappedLive(live, cache)
for i := 0; i < 3; i++ {
var info *Info
if info, err = live.GetInfo(); err == nil {
if info.CustomLiveId != "" {
live.SetLiveIdByString(info.CustomLiveId)
}
return
}
time.Sleep(1 * time.Second)
}
// when room initializaion is failed
live, err = InitializingLiveBuilderInstance.Build(live, url, opts...)
live = newWrappedLive(live, cache)
live.GetInfo() // dummy call to initialize cache inside wrappedLive
return
}