-
Notifications
You must be signed in to change notification settings - Fork 501
/
lives.go
executable file
·81 lines (69 loc) · 1.38 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
//go:generate mockgen -package mock -destination mock/mock.go github.com/hr3lxphr6j/bililive-go/src/live Live
package live
import (
"errors"
"net/url"
"time"
"github.com/bluele/gcache"
)
var (
m = make(map[string]Builder)
)
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) (Live, error)
}
type ID string
type Live interface {
GetLiveId() ID
GetRawUrl() string
GetInfo() (*Info, error)
GetStreamUrls() ([]*url.URL, error)
GetPlatformCNName() string
GetLastStartTime() time.Time
SetLastStartTime(time.Time)
}
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 {
return nil, err
}
if w.cache != nil {
w.cache.Set(w, i)
}
return i, nil
}
func New(url *url.URL, cache gcache.Cache) (live Live, err error) {
builder, ok := getBuilder(url.Host)
if !ok {
return nil, errors.New("not support this url")
}
live, err = builder.Build(url)
if err != nil {
return
}
live = newWrappedLive(live, cache)
for i := 0; i < 3; i++ {
if _, err = live.GetInfo(); err == nil {
break
}
time.Sleep(1 * time.Second)
}
return
}