From 07b996687ec2ddc0e7634bed5ef69dc42f7d020b Mon Sep 17 00:00:00 2001 From: qydysky Date: Tue, 23 Apr 2024 18:56:23 +0000 Subject: [PATCH] Imrpove biliApi --- F/api.go | 159 ++++++++++++------------------------------ F/biliApiInterface.go | 6 +- go.mod | 2 +- go.sum | 4 +- 4 files changed, 51 insertions(+), 120 deletions(-) diff --git a/F/api.go b/F/api.go index 63d09a3..ccd8f23 100644 --- a/F/api.go +++ b/F/api.go @@ -569,7 +569,7 @@ func (t *GetFunc) getRoomPlayInfo() (missKey []string) { //Roominitres { - if err, res := biliApi.GetRoomPlayInfo(t.Roomid); err != nil { + if err, res := biliApi.GetRoomPlayInfo(t.Roomid, 0); err != nil { apilog.L(`E: `, err) return } else { @@ -614,91 +614,58 @@ func (t *GetFunc) getRoomPlayInfoByQn() (missKey []string) { return } - { - AcceptQn := []int{} - for k := range t.AcceptQn { - if k <= t.Live_want_qn { - AcceptQn = append(AcceptQn, k) - } - } - MaxQn := 0 - for i := 0; len(AcceptQn) > i; i += 1 { - if AcceptQn[i] > MaxQn { - MaxQn = AcceptQn[i] - } + AcceptQn := []int{} + for k := range t.AcceptQn { + if k <= t.Live_want_qn { + AcceptQn = append(AcceptQn, k) } - if MaxQn == 0 { - apilog.L(`W: `, "使用默认") + } + MaxQn := 0 + for i := 0; len(AcceptQn) > i; i += 1 { + if AcceptQn[i] > MaxQn { + MaxQn = AcceptQn[i] } - t.Live_qn = MaxQn } - - Roomid := strconv.Itoa(t.Roomid) + if MaxQn == 0 { + apilog.L(`W: `, "使用默认") + } + t.Live_qn = MaxQn //Roominitres { - Cookie := make(map[string]string) - t.Cookie.Range(func(k, v interface{}) bool { - Cookie[k.(string)] = v.(string) - return true - }) - - req := t.Common.ReqPool.Get() - defer t.Common.ReqPool.Put(req) - if err := req.Reqf(reqf.Rval{ - Url: fmt.Sprintf("https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?room_id=%s&protocol=0,1&format=0,1,2&codec=0,1,2&qn=%d&platform=web&ptype=8&dolby=5&panorama=1", Roomid, t.Live_qn), - Header: map[string]string{ - `Referer`: "https://live.bilibili.com/" + Roomid, - `Cookie`: reqf.Map_2_Cookies_String(Cookie), - }, - Proxy: t.Proxy, - Timeout: 10 * 1000, - Retry: 2, - }); err != nil { + if err, res := biliApi.GetRoomPlayInfo(t.Roomid, MaxQn); err != nil { apilog.L(`E: `, err) return - } - - var j J.GetRoomPlayInfo - - if e := json.Unmarshal([]byte(req.Respon), &j); e != nil { - apilog.L(`E: `, e) - return - } else if j.Code != 0 { - apilog.L(`E: `, j.Message) - return - } + } else { + //主播uid + t.UpUid = res.UpUid + //房间号(完整) + t.Roomid = res.RoomID + //直播开始时间 + t.Live_Start_Time = res.LiveStartTime + //是否在直播 + t.Liveing = res.Liveing - //主播uid - t.UpUid = j.Data.UID - //房间号(完整) - if j.Data.RoomID != 0 { - t.Roomid = j.Data.RoomID - } - //直播开始时间 - if j.Data.LiveTime != 0 { - t.Live_Start_Time = time.Unix(int64(j.Data.LiveTime), 0) - } - //是否在直播 - t.Liveing = j.Data.LiveStatus == 1 + //未在直播,不获取直播流 + if !t.Liveing { + t.Live_qn = 0 + t.AcceptQn = t.Qn + t.Live = t.Live[:0] + return + } - //未在直播,不获取直播流 - if !t.Liveing { - t.Live_qn = 0 - t.AcceptQn = t.Qn - t.Live = t.Live[:0] - return + //当前直播流 + var s = make([]J.StreamType, len(res.Streams)) + for i := 0; i < len(res.Streams); i++ { + s[i] = J.StreamType(res.Streams[i]) + } + t.configStreamType(s) } - - //当前直播流 - t.configStreamType(j.Data.PlayurlInfo.Playurl.Stream) } return } func (c *GetFunc) getDanmuInfo() (missKey []string) { - apilog := apilog.Base_add(`getDanmuInfo`) - if c.Roomid == 0 { missKey = append(missKey, `Roomid`) } @@ -709,53 +676,13 @@ func (c *GetFunc) getDanmuInfo() (missKey []string) { return } - Roomid := strconv.Itoa(c.Roomid) - //GetDanmuInfo - { - Cookie := make(map[string]string) - c.Cookie.Range(func(k, v interface{}) bool { - Cookie[k.(string)] = v.(string) - return true - }) - - req := c.Common.ReqPool.Get() - defer c.Common.ReqPool.Put(req) - if err := req.Reqf(reqf.Rval{ - Url: "https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?type=0&id=" + Roomid, - Header: map[string]string{ - `Referer`: "https://live.bilibili.com/" + Roomid, - `Cookie`: reqf.Map_2_Cookies_String(Cookie), - }, - Proxy: c.Proxy, - Timeout: 10 * 1000, - }); err != nil { - apilog.L(`E: `, err) - return - } - - var j J.GetDanmuInfo - - if e := json.Unmarshal([]byte(req.Respon), &j); e != nil { - apilog.L(`E: `, e) - return - } else if j.Code != 0 { - apilog.L(`E: `, j.Message) - return - } - - //弹幕钥 - c.Token = j.Data.Token - //弹幕链接 - var tmp []string - for _, v := range j.Data.HostList { - if v.WssPort != 443 { - tmp = append(tmp, "wss://"+v.Host+":"+strconv.Itoa(v.WssPort)+"/sub") - } else { - tmp = append(tmp, "wss://"+v.Host+"/sub") - } - } - c.WSURL = tmp + if err, res := biliApi.GetDanmuInfo(c.Roomid); err != nil { + apilog.L(`E: `, err) + return + } else { + c.Token = res.Token + c.WSURL = res.WSURL } return } diff --git a/F/biliApiInterface.go b/F/biliApiInterface.go index 0683fd6..7c97ce9 100644 --- a/F/biliApiInterface.go +++ b/F/biliApiInterface.go @@ -38,7 +38,7 @@ type biliApiInter interface { Note string Locked bool }) - GetRoomPlayInfo(Roomid int) (err error, res struct { + GetRoomPlayInfo(Roomid int, Qn int) (err error, res struct { UpUid int RoomID int LiveStartTime time.Time @@ -64,4 +64,8 @@ type biliApiInter interface { } } }) + GetDanmuInfo(Roomid int) (err error, res struct { + Token string + WSURL []string + }) } diff --git a/go.mod b/go.mod index 415fa19..d7e5d90 100644 --- a/go.mod +++ b/go.mod @@ -13,7 +13,7 @@ require ( require ( github.com/google/uuid v1.6.0 - github.com/qydysky/biliApi v0.0.0-20240423154430-977d1e8a7603 + github.com/qydysky/biliApi v0.0.0-20240423184039-6ce021146928 golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f ) diff --git a/go.sum b/go.sum index 28c46b2..89f7f99 100644 --- a/go.sum +++ b/go.sum @@ -46,8 +46,8 @@ github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdh github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/qydysky/biliApi v0.0.0-20240423154430-977d1e8a7603 h1:UJ5DOY3EA9BSSfWE92I6gUjP1frgz1xnDGhW8/LuvWI= -github.com/qydysky/biliApi v0.0.0-20240423154430-977d1e8a7603/go.mod h1:nhubuLaNo2LIkgoCt++A5KNtra53ttGPXsahjaGXCl4= +github.com/qydysky/biliApi v0.0.0-20240423184039-6ce021146928 h1:3rL0AIxeBNt+0MYDTeS3ZgDDcbMrXXAIOGq8mpitzBM= +github.com/qydysky/biliApi v0.0.0-20240423184039-6ce021146928/go.mod h1:nhubuLaNo2LIkgoCt++A5KNtra53ttGPXsahjaGXCl4= github.com/qydysky/part v0.28.20240422183953 h1:r8kZFzSIqNbFOOuFBJw5aobwLZ/fQ6GaEez5082aJEY= github.com/qydysky/part v0.28.20240422183953/go.mod h1:VMq3GnrK1/7zUufW3foG5oLAk9dw567JmuMyKPTGswc= github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= -- 2.39.2