-
Notifications
You must be signed in to change notification settings - Fork 442
/
cc.go
116 lines (100 loc) · 2.53 KB
/
cc.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
package cc
import (
"errors"
"fmt"
"net/http"
"net/url"
"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 = "cc.163.com"
cnName = "CC直播"
apiUrl = "http://cgi.v.cc.163.com/video_play_url/"
dataRe = `<script id="__NEXT_DATA__" type="application/json" crossorigin="anonymous">(.*?)</script>`
)
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) getData() (*gjson.Result, error) {
resp, err := requests.Get(l.Url.String(), live.CommonUserAgent)
if err != nil {
return nil, err
}
body, err := resp.Bytes()
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, live.ErrRoomNotExist
}
data := utils.UnescapeHTMLEntity(utils.Match1(dataRe, string(body)))
if data == "" {
return nil, errors.New("data is empty")
}
result := gjson.Parse(data)
return &result, nil
}
func (l *Live) getCcID() (string, error) {
data, err := l.getData()
if err != nil {
return "", err
}
return data.Get("props.pageProps.roomInfoInitData.micfirst.ccid").String(), nil
}
func (l *Live) GetInfo() (info *live.Info, err error) {
data, err := l.getData()
if err != nil {
return nil, err
}
var (
hostName = data.Get("props.pageProps.roomInfoInitData.micfirst.nickname").String()
roomName = data.Get("props.pageProps.roomInfoInitData.live.title").String()
)
if hostName == "" || roomName == "" {
return nil, errors.New("failed to parse host`s name and room`s name")
}
info = &live.Info{
Live: l,
HostName: hostName,
RoomName: roomName,
Status: data.Get("props.pageProps.roomInfoInitData.live.swf").String() != "",
}
return info, nil
}
func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
ccid, err := l.getCcID()
if err != nil {
return nil, err
}
resp, err := requests.Get(fmt.Sprintf("%s%s", apiUrl, ccid), live.CommonUserAgent)
if err != nil {
return nil, err
}
body, err := resp.Bytes()
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, live.ErrRoomNotExist
}
return utils.GenUrls(
gjson.GetBytes(body, "videourl").String(),
gjson.GetBytes(body, "bakvideourl").String(),
)
}
func (l *Live) GetPlatformCNName() string {
return cnName
}