/
hongdoufm.go
98 lines (84 loc) · 2.3 KB
/
hongdoufm.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
package hongdoufm
import (
"strings"
"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 = "www.hongdoufm.com"
cnName = "克拉克拉"
roomInitUrl = "https://live.hongdoulive.com/LiveRoom/getRoomInfo?roomId="
)
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
roomID string
}
//克拉克拉平台直播间连接有两种格式
// 1、https://www.hongdoufm.com/room/roomid 这是直播间列表中的房间地址
// 2、http://www.hongdoufm.com/PcLive/index/detail?id=roomid 这是实际直播间地址,上述地址会经过302跳转
func (l *Live) getRoomInfo() ([]byte,error) {
if strings.Contains(l.Url.String(),"?") {
//实际直播间地址
result,_:= url.ParseQuery(l.Url.RawQuery)
roomid := result.Get("id")
l.roomID = roomid
}else{
//列表直播间地址
paths := strings.Split(l.Url.Path, "/")
if len(paths) < 2 {
return nil,live.ErrRoomUrlIncorrect
}
roomid := paths[2]
l.roomID = roomid
}
resp, err := requests.Get(roomInitUrl+l.roomID)
if err != nil {
return nil,err
}
if resp.StatusCode != http.StatusOK {
return nil,live.ErrRoomNotExist
}
body, err := resp.Bytes()
if err != nil || gjson.GetBytes(body, "h.code").Int() != 200 {
return nil,live.ErrRoomNotExist
}
return body,nil
}
func (l *Live) GetInfo() (info *live.Info, err error) {
body,err := l.getRoomInfo()
if err !=nil{
return nil,live.ErrRoomNotExist
}
info = &live.Info{
Live: l,
HostName: gjson.GetBytes(body, "b.userInfo.nickname").String(),
RoomName: gjson.GetBytes(body, "b.title").String(),
Status: gjson.GetBytes(body, "b.status").Int() == 4,
CustomLiveId:"hongdoufm/"+l.roomID,
}
return info,nil
}
func (l *Live) GetStreamUrls() (us []*url.URL, err error) {
body,err := l.getRoomInfo()
if err !=nil{
return nil,live.ErrRoomNotExist
}
return utils.GenUrls(gjson.GetBytes(body,"b.flvPlayUrl").String())
}
func (l *Live) GetPlatformCNName() string {
return cnName
}