/
zhanqi.go
88 lines (75 loc) · 1.9 KB
/
zhanqi.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
package zhanqi
import (
"encoding/base64"
"fmt"
"net/http"
"net/url"
"strings"
"github.com/hr3lxphr6j/requests"
"github.com/tidwall/gjson"
"github.com/hr3lxphr6j/bililive-go/src/live"
"github.com/hr3lxphr6j/bililive-go/src/live/internal"
"github.com/hr3lxphr6j/bililive-go/src/pkg/utils"
)
const (
domain = "www.zhanqi.tv"
cnName = "战旗"
apiUrl = "https://www.zhanqi.tv/api/static/v2.1/room/domain/%s.json"
)
func init() {
live.Register(domain, new(builder))
}
type builder struct{}
func (b *builder) Build(url *url.URL, opt ...live.Option) (live.Live, error) {
return &Live{
BaseLive: internal.NewBaseLive(url, opt...),
}, nil
}
type Live struct {
internal.BaseLive
}
func (l *Live) requestRoomInfo() ([]byte, error) {
resp, err := requests.Get(fmt.Sprintf(apiUrl, strings.Split(l.Url.Path, "/")[1]), live.CommonUserAgent)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, live.ErrRoomNotExist
}
body, err := resp.Bytes()
if err != nil {
return nil, err
}
if gjson.GetBytes(body, "code").Int() != 0 {
return nil, live.ErrRoomNotExist
}
return body, nil
}
func (l *Live) GetInfo() (info *live.Info, err error) {
body, err := l.requestRoomInfo()
if err != nil {
return nil, err
}
info = &live.Info{
Live: l,
HostName: gjson.GetBytes(body, "data.nickname").String(),
RoomName: gjson.GetBytes(body, "data.title").String(),
Status: gjson.GetBytes(body, "data.status").String() == "4",
}
return info, nil
}
func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
body, err := l.requestRoomInfo()
if err != nil {
return nil, err
}
videoLevels := gjson.GetBytes(body, "data.flashvars.VideoLevels").String()
data, err := base64.StdEncoding.DecodeString(videoLevels)
if err != nil {
return nil, err
}
return utils.GenUrls(gjson.GetBytes(data, "streamUrl").String())
}
func (l *Live) GetPlatformCNName() string {
return cnName
}