From b6eb2c0da4e653c6fdd278bcbdcd55ec376cd481 Mon Sep 17 00:00:00 2001 From: Keuin Date: Sun, 2 Jul 2023 14:48:47 +0800 Subject: Refactor: move RoomId type to the correct package. Type all room ID usages. --- bilibili/danmaku_server_info.go | 3 +-- bilibili/danmaku_server_info_test.go | 6 ++--- bilibili/play_url.go | 3 +-- bilibili/play_url_test.go | 6 ++--- bilibili/room_profile.go | 7 +++--- bilibili/room_profile_test.go | 8 +++--- bilibili/room_status.go | 3 +-- bilibili/room_status_test.go | 6 ++--- bilibili/streaming.go | 2 +- bilibili/streaming_test.go | 6 ++--- common/testing/testutil.go | 47 ++++++++++++++++++++++++++++++++++++ common/testutil.go | 46 ----------------------------------- common/types.go | 3 --- danmaku/client.go | 4 +-- danmaku/dmpkg/auth.go | 18 +++++++------- main.go | 3 +-- recording/config.go | 3 +-- 17 files changed, 84 insertions(+), 90 deletions(-) create mode 100644 common/testing/testutil.go delete mode 100644 common/testutil.go delete mode 100644 common/types.go diff --git a/bilibili/danmaku_server_info.go b/bilibili/danmaku_server_info.go index 1a91b14..b68d4b5 100644 --- a/bilibili/danmaku_server_info.go +++ b/bilibili/danmaku_server_info.go @@ -2,7 +2,6 @@ package bilibili import ( "fmt" - "github.com/keuin/slbr/common" ) type DanmakuServerInfoResponse = BaseResponse[danmakuInfo] @@ -22,7 +21,7 @@ type danmakuInfo struct { } `json:"host_list"` } -func (b Bilibili) GetDanmakuServerInfo(roomId common.RoomId) (resp DanmakuServerInfoResponse, err error) { +func (b Bilibili) GetDanmakuServerInfo(roomId RoomId) (resp DanmakuServerInfoResponse, err error) { url := fmt.Sprintf("https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?id=%d&type=0", roomId) return callGet[DanmakuServerInfoResponse](b, url) } diff --git a/bilibili/danmaku_server_info_test.go b/bilibili/danmaku_server_info_test.go index 3166304..7cfa8fe 100644 --- a/bilibili/danmaku_server_info_test.go +++ b/bilibili/danmaku_server_info_test.go @@ -1,7 +1,7 @@ package bilibili import ( - "github.com/keuin/slbr/common" + testing2 "github.com/keuin/slbr/common/testing" "github.com/keuin/slbr/logging" "log" "testing" @@ -9,7 +9,7 @@ import ( func TestBilibili_GetDanmakuServerInfo(t *testing.T) { // get an online live room for testing - liveList, err := common.GetLiveListForGuestUser() + liveList, err := testing2.GetLiveListForGuestUser() if err != nil { t.Fatalf("Cannot get live list for testing: %v", err) } @@ -17,7 +17,7 @@ func TestBilibili_GetDanmakuServerInfo(t *testing.T) { if len(lives) <= 0 { t.Fatalf("No available live for guest user") } - roomId := common.RoomId(lives[0].Roomid) + roomId := lives[0].Roomid logger := log.Default() bi := NewBilibili(logging.NewWrappedLogger(logger, "test-logger")) diff --git a/bilibili/play_url.go b/bilibili/play_url.go index 36882bc..49e8298 100644 --- a/bilibili/play_url.go +++ b/bilibili/play_url.go @@ -2,7 +2,6 @@ package bilibili import ( "fmt" - "github.com/keuin/slbr/common" ) type RoomUrlInfoResponse = BaseResponse[roomUrlInfo] @@ -28,7 +27,7 @@ type StreamingUrlInfo struct { P2pType int `json:"p2p_type"` } -func (b Bilibili) GetStreamingInfo(roomId common.RoomId) (resp RoomUrlInfoResponse, err error) { +func (b Bilibili) GetStreamingInfo(roomId RoomId) (resp 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[RoomUrlInfoResponse](b, url) diff --git a/bilibili/play_url_test.go b/bilibili/play_url_test.go index 7ec17ee..1ec0729 100644 --- a/bilibili/play_url_test.go +++ b/bilibili/play_url_test.go @@ -1,7 +1,7 @@ package bilibili import ( - "github.com/keuin/slbr/common" + testing2 "github.com/keuin/slbr/common/testing" "github.com/keuin/slbr/logging" "log" "testing" @@ -9,7 +9,7 @@ import ( func TestBilibili_GetStreamingInfo(t *testing.T) { // get an online live room for testing - liveList, err := common.GetLiveListForGuestUser() + liveList, err := testing2.GetLiveListForGuestUser() if err != nil { t.Fatalf("cannot get live list for testing: %v", err) } @@ -17,7 +17,7 @@ func TestBilibili_GetStreamingInfo(t *testing.T) { if len(lives) <= 0 { t.Fatalf("no live for guest available") } - roomId := common.RoomId(lives[0].Roomid) + roomId := lives[0].Roomid logger := log.Default() bi := NewBilibili(logging.NewWrappedLogger(logger, "test-logger")) diff --git a/bilibili/room_profile.go b/bilibili/room_profile.go index 9eed175..98cc790 100644 --- a/bilibili/room_profile.go +++ b/bilibili/room_profile.go @@ -2,12 +2,11 @@ package bilibili import ( "fmt" - "github.com/keuin/slbr/common" ) type roomProfile struct { UID int `json:"uid"` - RoomID int `json:"room_id"` + RoomID RoomId `json:"room_id"` ShortID int `json:"short_id"` Attention int `json:"attention"` Online int `json:"online"` @@ -80,7 +79,9 @@ type roomProfile struct { type RoomProfileResponse = BaseResponse[roomProfile] -func (b Bilibili) GetRoomProfile(roomId common.RoomId) (resp RoomProfileResponse, err error) { +func (b Bilibili) GetRoomProfile(roomId RoomId) (resp RoomProfileResponse, err error) { url := fmt.Sprintf("https://api.live.bilibili.com/room/v1/Room/get_info?room_id=%d", roomId) return callGet[RoomProfileResponse](b, url) } + +type RoomId uint64 diff --git a/bilibili/room_profile_test.go b/bilibili/room_profile_test.go index 0abd8dd..a1d68c5 100644 --- a/bilibili/room_profile_test.go +++ b/bilibili/room_profile_test.go @@ -1,7 +1,7 @@ package bilibili import ( - "github.com/keuin/slbr/common" + testing2 "github.com/keuin/slbr/common/testing" "github.com/keuin/slbr/logging" "log" "testing" @@ -9,7 +9,7 @@ import ( func TestBilibili_GetRoomProfile(t *testing.T) { // get an online live room for testing - liveList, err := common.GetLiveListForGuestUser() + liveList, err := testing2.GetLiveListForGuestUser() if err != nil { t.Fatalf("cannot get live list for testing: %v", err) } @@ -17,7 +17,7 @@ func TestBilibili_GetRoomProfile(t *testing.T) { if len(lives) <= 0 { t.Fatalf("no live for guest available") } - roomId := common.RoomId(lives[0].Roomid) + roomId := lives[0].Roomid logger := log.Default() bi := NewBilibili(logging.NewWrappedLogger(logger, "test-logger")) @@ -28,7 +28,7 @@ func TestBilibili_GetRoomProfile(t *testing.T) { if resp.Code != 0 || resp.Message != "ok" || resp.Data.UID <= 0 || - resp.Data.RoomID != int(roomId) || + resp.Data.RoomID != roomId || resp.Data.LiveStatus != int(Streaming) || resp.Data.Title == "" { t.Fatalf("Invalid GetRoomProfile response: %v", resp) diff --git a/bilibili/room_status.go b/bilibili/room_status.go index fc6219a..cbb8bc4 100644 --- a/bilibili/room_status.go +++ b/bilibili/room_status.go @@ -6,7 +6,6 @@ package bilibili import ( "fmt" - "github.com/keuin/slbr/common" ) type LiveStatus int @@ -51,7 +50,7 @@ func (s LiveStatus) String() string { return liveStatusStringMap[s] } -func (b Bilibili) GetRoomPlayInfo(roomId common.RoomId) (resp RoomPlayInfoResponse, err error) { +func (b Bilibili) GetRoomPlayInfo(roomId RoomId) (resp 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[RoomPlayInfoResponse](b, url) diff --git a/bilibili/room_status_test.go b/bilibili/room_status_test.go index f97cf51..dde99f7 100644 --- a/bilibili/room_status_test.go +++ b/bilibili/room_status_test.go @@ -1,7 +1,7 @@ package bilibili import ( - "github.com/keuin/slbr/common" + testing2 "github.com/keuin/slbr/common/testing" "github.com/keuin/slbr/logging" "log" "testing" @@ -9,7 +9,7 @@ import ( func TestBilibili_GetRoomPlayInfo(t *testing.T) { // get an online live room for testing - liveList, err := common.GetLiveListForGuestUser() + liveList, err := testing2.GetLiveListForGuestUser() if err != nil { t.Fatalf("cannot get live list for testing: %v", err) } @@ -17,7 +17,7 @@ func TestBilibili_GetRoomPlayInfo(t *testing.T) { if len(lives) <= 0 { t.Fatalf("no live for guest available") } - roomId := common.RoomId(lives[0].Roomid) + roomId := lives[0].Roomid logger := log.Default() bi := NewBilibili(logging.NewWrappedLogger(logger, "test-logger")) diff --git a/bilibili/streaming.go b/bilibili/streaming.go index 97b8e23..8f6c491 100644 --- a/bilibili/streaming.go +++ b/bilibili/streaming.go @@ -16,7 +16,7 @@ const InitReadBytes = 4096 // 4KiB // CopyLiveStream read data from a livestream video stream, copy them to a writer. func (b Bilibili) CopyLiveStream( ctx context.Context, - roomId common.RoomId, + roomId RoomId, stream StreamingUrlInfo, fileCreator func() (*os.File, error), bufSize int64, diff --git a/bilibili/streaming_test.go b/bilibili/streaming_test.go index ef438db..3b5bd3a 100644 --- a/bilibili/streaming_test.go +++ b/bilibili/streaming_test.go @@ -4,7 +4,7 @@ import ( "context" "errors" "fmt" - "github.com/keuin/slbr/common" + testing2 "github.com/keuin/slbr/common/testing" "github.com/keuin/slbr/logging" "log" "os" @@ -13,7 +13,7 @@ import ( func TestBilibili_CopyLiveStream(t *testing.T) { // get an online live room for testing - liveList, err := common.GetLiveListForGuestUser() + liveList, err := testing2.GetLiveListForGuestUser() if err != nil { t.Fatalf("cannot get live list for testing: %v", err) } @@ -21,7 +21,7 @@ func TestBilibili_CopyLiveStream(t *testing.T) { if len(lives) <= 0 { t.Fatalf("no live for guest available") } - roomId := common.RoomId(lives[0].Roomid) + roomId := lives[0].Roomid logger := log.Default() bi := NewBilibili(logging.NewWrappedLogger(logger, "test-logger")) diff --git a/common/testing/testutil.go b/common/testing/testutil.go new file mode 100644 index 0000000..509c619 --- /dev/null +++ b/common/testing/testutil.go @@ -0,0 +1,47 @@ +package testing + +import ( + "encoding/json" + "fmt" + "github.com/keuin/slbr/bilibili" + "io" + "net/http" +) + +/* +Some utility function for test-purpose only. +*/ + +type LiveList struct { + Code int `json:"code"` + Message string `json:"message"` + TTL int `json:"ttl"` + Data struct { + Count int `json:"count"` + Data []struct { + Face string `json:"face"` + Link string `json:"link"` + Roomid bilibili.RoomId `json:"roomid"` + Roomname string `json:"roomname"` + Nickname string `json:"nickname"` + } `json:"data"` + } `json:"data"` +} + +func GetLiveListForGuestUser() (liveList LiveList, err error) { + url := "https://api.live.bilibili.com/xlive/web-interface/v1/index/WebGetUnLoginRecList" + resp, err := http.Get(url) + if err != nil { + return + } + if resp.StatusCode != http.StatusOK { + err = fmt.Errorf("bad http response: %v", resp.StatusCode) + return + } + b, err := io.ReadAll(resp.Body) + if err != nil { + return + } + err = json.Unmarshal(b, &liveList) + return +} diff --git a/common/testutil.go b/common/testutil.go deleted file mode 100644 index d6c3899..0000000 --- a/common/testutil.go +++ /dev/null @@ -1,46 +0,0 @@ -package common - -import ( - "encoding/json" - "fmt" - "io" - "net/http" -) - -/* -Some utility function for test-purpose only. -*/ - -type LiveList struct { - Code int `json:"code"` - Message string `json:"message"` - TTL int `json:"ttl"` - Data struct { - Count int `json:"count"` - Data []struct { - Face string `json:"face"` - Link string `json:"link"` - Roomid int `json:"roomid"` - Roomname string `json:"roomname"` - Nickname string `json:"nickname"` - } `json:"data"` - } `json:"data"` -} - -func GetLiveListForGuestUser() (liveList LiveList, err error) { - url := "https://api.live.bilibili.com/xlive/web-interface/v1/index/WebGetUnLoginRecList" - resp, err := http.Get(url) - if err != nil { - return - } - if resp.StatusCode != http.StatusOK { - err = fmt.Errorf("bad http response: %v", resp.StatusCode) - return - } - b, err := io.ReadAll(resp.Body) - if err != nil { - return - } - err = json.Unmarshal(b, &liveList) - return -} diff --git a/common/types.go b/common/types.go deleted file mode 100644 index 81456b4..0000000 --- a/common/types.go +++ /dev/null @@ -1,3 +0,0 @@ -package common - -type RoomId uint64 diff --git a/danmaku/client.go b/danmaku/client.go index b785e23..c74a449 100644 --- a/danmaku/client.go +++ b/danmaku/client.go @@ -9,7 +9,7 @@ package danmaku import ( "context" "fmt" - "github.com/keuin/slbr/common" + "github.com/keuin/slbr/bilibili" "github.com/keuin/slbr/danmaku/dmpkg" "nhooyr.io/websocket" @@ -87,7 +87,7 @@ func (d *DanmakuClient) Disconnect() error { return ws.Close(websocket.StatusInternalError, "disconnected") } -func (d *DanmakuClient) Authenticate(roomId common.RoomId, authKey string) error { +func (d *DanmakuClient) Authenticate(roomId bilibili.RoomId, authKey string) error { pkg := dmpkg.NewAuth(dmpkg.ProtoPlainJson, roomId, authKey) data, err := pkg.Marshal() if err != nil { diff --git a/danmaku/dmpkg/auth.go b/danmaku/dmpkg/auth.go index 5caf868..bb66f1d 100644 --- a/danmaku/dmpkg/auth.go +++ b/danmaku/dmpkg/auth.go @@ -8,23 +8,23 @@ package dmpkg import ( "encoding/json" "fmt" - "github.com/keuin/slbr/common" + "github.com/keuin/slbr/bilibili" ) type authInfo struct { - UID uint64 `json:"uid"` - RoomId uint64 `json:"roomid"` - ProtoVer int `json:"protover"` - Platform string `json:"platform"` - Type int `json:"type"` - Key string `json:"key"` + UID uint64 `json:"uid"` + RoomId bilibili.RoomId `json:"roomid"` + ProtoVer int `json:"protover"` + Platform string `json:"platform"` + Type int `json:"type"` + Key string `json:"key"` } // NewAuth creates a new authentication exchange. -func NewAuth(protocol ProtocolVer, roomId common.RoomId, authKey string) (exc DanmakuExchange) { +func NewAuth(protocol ProtocolVer, roomId bilibili.RoomId, authKey string) (exc DanmakuExchange) { exc, _ = NewPlainExchange(OpConnect, authInfo{ UID: UidGuest, - RoomId: uint64(roomId), + RoomId: roomId, ProtoVer: int(protocol), Platform: PlatformWeb, Type: AuthTypeDefault, diff --git a/main.go b/main.go index b53b63b..7d3fc37 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,6 @@ import ( "fmt" "github.com/akamensky/argparse" "github.com/keuin/slbr/bilibili" - "github.com/keuin/slbr/common" "github.com/keuin/slbr/logging" "github.com/keuin/slbr/recording" "github.com/mitchellh/mapstructure" @@ -139,7 +138,7 @@ func getTasks() (tasks []recording.TaskConfig) { } for i := 0; i < taskCount; i++ { tasks[i] = recording.TaskConfig{ - RoomId: common.RoomId((*rooms)[i]), + RoomId: bilibili.RoomId((*rooms)[i]), Transport: recording.DefaultTransportConfig(), Download: recording.DownloadConfig{ DiskWriteBufferBytes: int64(diskBufSize), diff --git a/recording/config.go b/recording/config.go index 7dbf189..9a95e62 100644 --- a/recording/config.go +++ b/recording/config.go @@ -3,11 +3,10 @@ package recording import ( "fmt" "github.com/keuin/slbr/bilibili" - "github.com/keuin/slbr/common" ) type TaskConfig struct { - RoomId common.RoomId `mapstructure:"room_id"` + RoomId bilibili.RoomId `mapstructure:"room_id"` Transport TransportConfig `mapstructure:"transport"` Download DownloadConfig `mapstructure:"download"` Watch WatchConfig `mapstructure:"watch"` -- cgit v1.2.3