Skip to content

Commit 2db5d97

Browse files
authored
fix(bilibili): 无法获取B站直播间信息 (#75)
1 parent 5f9e921 commit 2db5d97

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

bilibili/api.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ import (
1616
// ErrAPINeedCookie ...
1717
var ErrAPINeedCookie = errors.New("api need cookie")
1818

19+
// Ua 默认UA
20+
var Ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
21+
1922
// SearchUser 查找b站用户
2023
func SearchUser(cookiecfg *CookieConfig, keyword string) (r []SearchResult, err error) {
2124
client := &http.Client{}
@@ -166,9 +169,17 @@ func GetArticleInfo(id string) (card Card, err error) {
166169
}
167170

168171
// GetLiveRoomInfo 用直播间id查直播间信息
169-
func GetLiveRoomInfo(roomID string) (card RoomCard, err error) {
172+
func GetLiveRoomInfo(roomID string, cookie string) (card RoomCard, err error) {
170173
var data []byte
171-
data, err = web.GetData(fmt.Sprintf(ArticleInfoURL, roomID))
174+
if cookie != "" {
175+
data, err = web.RequestDataWithHeaders(web.NewDefaultClient(), fmt.Sprintf(LiveRoomInfoURL, roomID), "GET", func(req *http.Request) error {
176+
req.Header.Add("cookie", cookie)
177+
req.Header.Set("User-Agent", Ua)
178+
return nil
179+
}, nil)
180+
} else {
181+
data, err = web.GetData(fmt.Sprintf(LiveRoomInfoURL, roomID))
182+
}
172183
if err != nil {
173184
return
174185
}

0 commit comments

Comments
 (0)