From 2ace07d2d88c01a56b4d0f3194bd74a518a769c8 Mon Sep 17 00:00:00 2001 From: Keuin Date: Fri, 14 Jul 2023 22:25:27 +0800 Subject: Refactor: use pointer type of `Bilibili` to prevent unexpected copy. --- bilibili/client.go | 8 ++++---- bilibili/danmaku_server_info.go | 2 +- bilibili/play_url.go | 2 +- bilibili/request.go | 8 ++++---- bilibili/room_profile.go | 2 +- bilibili/room_status.go | 2 +- bilibili/streaming.go | 2 +- recording/runner.go | 4 ++-- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/bilibili/client.go b/bilibili/client.go index 35d06e4..17a42d6 100644 --- a/bilibili/client.go +++ b/bilibili/client.go @@ -26,7 +26,7 @@ type Bilibili struct { logger logging.Logger } -func NewBilibiliWithContext(ctx context.Context, netTypes []types.IpNetType, logger logging.Logger) Bilibili { +func NewBilibiliWithContext(ctx context.Context, netTypes []types.IpNetType, logger logging.Logger) *Bilibili { var nets []types.IpNetType nets = append(nets, netTypes...) if len(nets) == 0 { @@ -39,7 +39,7 @@ func NewBilibiliWithContext(ctx context.Context, netTypes []types.IpNetType, log transport.DialTLSContext = nil transport.DialContext, _ = np.NextNetworkType(dialer) - return Bilibili{ + return &Bilibili{ logger: logger, userAgent: kUserAgent, http: http.DefaultClient, @@ -48,11 +48,11 @@ func NewBilibiliWithContext(ctx context.Context, netTypes []types.IpNetType, log } } -func NewBilibiliWithNetType(netTypes []types.IpNetType, logger logging.Logger) Bilibili { +func NewBilibiliWithNetType(netTypes []types.IpNetType, logger logging.Logger) *Bilibili { ctx := context.Background() return NewBilibiliWithContext(ctx, netTypes, logger) } -func NewBilibili(logger logging.Logger) Bilibili { +func NewBilibili(logger logging.Logger) *Bilibili { return NewBilibiliWithNetType(nil, logger) } diff --git a/bilibili/danmaku_server_info.go b/bilibili/danmaku_server_info.go index e3cfbc0..4d26873 100644 --- a/bilibili/danmaku_server_info.go +++ b/bilibili/danmaku_server_info.go @@ -5,7 +5,7 @@ import ( "github.com/keuin/slbr/types" ) -func (b Bilibili) GetDanmakuServerInfo(roomId types.RoomId) (resp types.DanmakuServerInfoResponse, err error) { +func (b *Bilibili) GetDanmakuServerInfo(roomId types.RoomId) (resp types.DanmakuServerInfoResponse, err error) { url := fmt.Sprintf("https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=%d&type=0", roomId) return callGet[types.DanmakuServerInfoResponse](b, url) } diff --git a/bilibili/play_url.go b/bilibili/play_url.go index c2c6709..3f38272 100644 --- a/bilibili/play_url.go +++ b/bilibili/play_url.go @@ -5,7 +5,7 @@ import ( "github.com/keuin/slbr/types" ) -func (b Bilibili) GetStreamingInfo(roomId types.RoomId) (resp types.RoomUrlInfoResponse, err error) { +func (b *Bilibili) GetStreamingInfo(roomId types.RoomId) (resp types.RoomUrlInfoResponse, err error) { url := fmt.Sprintf("https://api.live.bilibili.com/room/v1/Room/playUrl?"+ "cid=%d&otype=json&qn=10000&platform=web", roomId) return callGet[types.RoomUrlInfoResponse](b, url) diff --git a/bilibili/request.go b/bilibili/request.go index fa31b19..11ff995 100644 --- a/bilibili/request.go +++ b/bilibili/request.go @@ -10,7 +10,7 @@ import ( ) // newRequest create an HTTP request with per-instance User-Agent set. -func (b Bilibili) newRequest( +func (b *Bilibili) newRequest( method string, url string, body io.Reader, @@ -25,12 +25,12 @@ func (b Bilibili) newRequest( } // newRequest create an HTTP GET request with an empty body and per-instance User-Agent set. -func (b Bilibili) newGet(url string) (req *http.Request, err error) { +func (b *Bilibili) newGet(url string) (req *http.Request, err error) { return b.newRequest("GET", url, strings.NewReader("")) } // callGet make a GET request and parse response as a JSON document with given model. -func callGet[T types.BaseResponse[V], V any](b Bilibili, url string) (resp T, err error) { +func callGet[T types.BaseResponse[V], V any](b *Bilibili, url string) (resp T, err error) { req, err := b.newGet(url) if err != nil { b.logger.Error("Cannot create HTTP request instance on API %v: %v", url, err) @@ -67,7 +67,7 @@ func callGet[T types.BaseResponse[V], V any](b Bilibili, url string) (resp T, er return } -func (b Bilibili) Do(req *http.Request) (resp *http.Response, err error) { +func (b *Bilibili) Do(req *http.Request) (resp *http.Response, err error) { transport := http.DefaultTransport.(*http.Transport).Clone() transport.DialTLSContext = nil diff --git a/bilibili/room_profile.go b/bilibili/room_profile.go index 1d5bdf4..0d703ee 100644 --- a/bilibili/room_profile.go +++ b/bilibili/room_profile.go @@ -5,7 +5,7 @@ import ( "github.com/keuin/slbr/types" ) -func (b Bilibili) GetRoomProfile(roomId types.RoomId) (resp types.RoomProfileResponse, err error) { +func (b *Bilibili) GetRoomProfile(roomId types.RoomId) (resp types.RoomProfileResponse, err error) { url := fmt.Sprintf("https://api.live.bilibili.com/room/v1/Room/get_info?room_id=%d", roomId) return callGet[types.RoomProfileResponse](b, url) } diff --git a/bilibili/room_status.go b/bilibili/room_status.go index f33277c..8e8f27f 100644 --- a/bilibili/room_status.go +++ b/bilibili/room_status.go @@ -9,7 +9,7 @@ import ( "github.com/keuin/slbr/types" ) -func (b Bilibili) GetRoomPlayInfo(roomId types.RoomId) (resp types.RoomPlayInfoResponse, err error) { +func (b *Bilibili) GetRoomPlayInfo(roomId types.RoomId) (resp types.RoomPlayInfoResponse, err error) { url := fmt.Sprintf("https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo"+ "?room_id=%d&protocol=0,1&format=0,1,2&codec=0,1&qn=0&platform=web&ptype=8&dolby=5&panorama=1", roomId) return callGet[types.RoomPlayInfoResponse](b, url) diff --git a/bilibili/streaming.go b/bilibili/streaming.go index 2e0416c..3269cf1 100644 --- a/bilibili/streaming.go +++ b/bilibili/streaming.go @@ -16,7 +16,7 @@ import ( const InitReadBytes = 4096 // 4KiB // CopyLiveStream read data from a livestream video stream, copy them to a writer. -func (b Bilibili) CopyLiveStream( +func (b *Bilibili) CopyLiveStream( ctx context.Context, roomId types.RoomId, stream types.StreamingUrlInfo, diff --git a/recording/runner.go b/recording/runner.go index 936fcab..fcb135e 100644 --- a/recording/runner.go +++ b/recording/runner.go @@ -241,7 +241,7 @@ func tryRunTask(t *RunningTask) error { // nil (live is ended normally) func record( ctx context.Context, - bi bilibili.Bilibili, + bi *bilibili.Bilibili, task *TaskConfig, logger logging.Logger, ) error { @@ -348,7 +348,7 @@ func record( func getDanmakuServer( task *TaskConfig, - bi bilibili.Bilibili, + bi *bilibili.Bilibili, ) (string, string, error) { dmInfo, err := bi.GetDanmakuServerInfo(task.RoomId) if err != nil { -- cgit v1.2.3