-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
bilibili.go
182 lines (164 loc) · 3.96 KB
/
bilibili.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
175
176
177
178
179
180
181
182
package tv
import (
"fmt"
"time"
"github.com/go-olive/tv/util"
)
func init() {
registerSite("bilibili", &bilibili{})
}
type bilibili struct {
base
}
func (this *bilibili) Name() string {
return "哔哩哔哩"
}
func (this *bilibili) Snap(tv *Tv) error {
tv.Info = &Info{
Timestamp: time.Now().Unix(),
}
options := []Option{
this.setRoomOn(),
this.setStreamURL(),
}
for _, option := range options {
if err := option(tv); err != nil {
return err
}
}
return nil
}
func (this *bilibili) setRoomOn() Option {
type roomInit struct {
Code int64 `json:"code"`
Data struct {
RoomID int64 `json:"room_id"`
LiveStatus int64 `json:"live_status"`
UID int64 `json:"uid"`
}
}
return func(tv *Tv) error {
roomInit := new(roomInit)
req := &util.HttpRequest{
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/live/info.md#获取房间页初始化信息
URL: "https://api.live.bilibili.com/room/v1/Room/room_init",
Method: "POST",
RequestData: map[string]interface{}{
"id": tv.RoomID,
},
ResponseData: roomInit,
ContentType: "application/form-data",
}
if err := req.Send(); err != nil {
return err
}
if roomInit.Code != 0 || roomInit.Data.LiveStatus != 1 {
return nil
}
tv.RoomID = fmt.Sprint(roomInit.Data.RoomID)
tv.roomOn = true
statusInfo := new(bilibiliAutoGeneratedStatusInfo)
req = &util.HttpRequest{
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/live/info.md#批量查询直播间状态
URL: "http://api.live.bilibili.com/room/v1/Room/get_status_info_by_uids",
Method: "POST",
RequestData: map[string]interface{}{
"uids": []int64{roomInit.Data.UID},
},
ResponseData: statusInfo,
ContentType: "application/json",
}
if err := req.Send(); err != nil {
return nil
}
for _, v := range statusInfo.Data {
if v.UID == roomInit.Data.UID {
tv.roomName = v.Title
tv.roomNameSet = true
}
}
return nil
}
}
func (this *bilibili) setStreamURL() Option {
return this.getRealURL
}
func (this *bilibili) getAutoGenerated(roomID string, currentQn int) (*bilibiliAutoGenerated, error) {
auto := new(bilibiliAutoGenerated)
req := &util.HttpRequest{
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/live/live_stream.md
URL: "https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo",
Method: "GET",
RequestData: map[string]interface{}{
"room_id": roomID,
"protocol": "0,1",
"format": "0,1,2",
"codec": "0,1",
"qn": currentQn,
"platform": "h5",
"ptype": "8",
},
ResponseData: auto,
ContentType: "application/form-data",
}
err := req.Send()
return auto, err
}
func (this *bilibili) getRealURL(tv *Tv) error {
if !tv.roomOn {
return nil
}
// 原画画质
const highestQn = 10000
auto, err := this.getAutoGenerated(tv.RoomID, highestQn)
if err != nil {
return err
}
for _, stream := range auto.Data.PlayurlInfo.Playurl.Stream {
if stream.ProtocolName != "http_stream" {
continue
}
for _, format := range stream.Format {
if format.FormatName != "flv" {
continue
}
var qnMax int
if len(format.Codec) <= 0 {
continue
}
for _, qn := range format.Codec[0].AcceptQn {
if qn > qnMax {
qnMax = qn
}
}
if format.Codec[0].CurrentQn != qnMax && qnMax > 0 {
auto, err = this.getAutoGenerated(tv.RoomID, qnMax)
if err != nil {
return err
}
}
}
}
for _, stream := range auto.Data.PlayurlInfo.Playurl.Stream {
if stream.ProtocolName != "http_stream" {
continue
}
for _, format := range stream.Format {
if format.FormatName != "flv" {
continue
}
if len(format.Codec) <= 0 {
continue
}
baseURL := format.Codec[0].BaseURL
urlInfo := format.Codec[0].URLInfo[0]
streamURL := urlInfo.Host + baseURL + urlInfo.Extra
tv.streamUrl = streamURL
return nil
}
}
if tv.streamUrl == "" {
tv.roomOn = false
}
return fmt.Errorf("fail to getRealURL")
}