From 5e144eab2b51d2d03f5147b0b409013133542074 Mon Sep 17 00:00:00 2001 From: aynakeya Date: Sun, 7 Apr 2024 23:46:17 -0700 Subject: [PATCH] update api --- README.md | 6 +++--- api/api.go | 27 +++++++++++++++++++++++++++ client/client.go | 29 ++++++++++++++++++++++++----- client/handler.go | 6 +++--- example/dump/main.go | 4 ++-- example/main.go | 8 ++++---- go.mod | 2 +- message/danmu.go | 4 ++-- message/gift.go | 2 +- message/guard.go | 2 +- message/superchat.go | 2 +- message/toast.go | 2 +- 12 files changed, 70 insertions(+), 24 deletions(-) create mode 100644 api/api.go diff --git a/README.md b/README.md index fa92faf..082ce67 100644 --- a/README.md +++ b/README.md @@ -24,9 +24,9 @@ package main import ( "fmt" - "github.com/Akegarasu/blivedm-go/client" - "github.com/Akegarasu/blivedm-go/message" - _ "github.com/Akegarasu/blivedm-go/utils" + "github.com/AynaLivePlayer/blivedm-go/client" + "github.com/AynaLivePlayer/blivedm-go/message" + _ "github.com/AynaLivePlayer/blivedm-go/utils" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" ) diff --git a/api/api.go b/api/api.go new file mode 100644 index 0000000..0779ba4 --- /dev/null +++ b/api/api.go @@ -0,0 +1,27 @@ +package api + +type IApi interface { + GetUid() (int, error) + GetDanmuInfo(roomID int) (*DanmuInfo, error) + GetRoomInfo(roomID int) (*RoomInfo, error) +} + +type defaultClient struct { + cookie string +} + +func (d *defaultClient) GetUid() (int, error) { + return GetUid(d.cookie) +} + +func (d *defaultClient) GetDanmuInfo(roomID int) (*DanmuInfo, error) { + return GetDanmuInfo(roomID, d.cookie) +} + +func (d *defaultClient) GetRoomInfo(roomID int) (*RoomInfo, error) { + return GetRoomInfo(roomID) +} + +func NewDefaultClient(cookie string) IApi { + return &defaultClient{cookie: cookie} +} diff --git a/client/client.go b/client/client.go index 6287ed9..f8af45c 100644 --- a/client/client.go +++ b/client/client.go @@ -9,8 +9,8 @@ import ( "strings" "time" - "github.com/Akegarasu/blivedm-go/api" - "github.com/Akegarasu/blivedm-go/packet" + "github.com/AynaLivePlayer/blivedm-go/api" + "github.com/AynaLivePlayer/blivedm-go/packet" "github.com/gorilla/websocket" log "github.com/sirupsen/logrus" ) @@ -29,6 +29,7 @@ type Client struct { customEventHandlers *customEventHandlers cancel context.CancelFunc done <-chan struct{} + api api.IApi } // NewClient 创建一个新的弹幕 client @@ -41,6 +42,21 @@ func NewClient(roomID int) *Client { customEventHandlers: &customEventHandlers{}, done: ctx.Done(), cancel: cancel, + api: nil, + } +} + +// NewClient 创建一个新的弹幕 client +func NewClientWithApi(roomID int, iApi api.IApi) *Client { + ctx, cancel := context.WithCancel(context.Background()) + return &Client{ + RoomID: roomID, + retryCount: 0, + eventHandlers: &eventHandlers{}, + customEventHandlers: &customEventHandlers{}, + done: ctx.Done(), + cancel: cancel, + api: iApi, } } @@ -50,12 +66,15 @@ func (c *Client) SetCookie(cookie string) { // init 初始化 获取真实 RoomID 和 弹幕服务器 host func (c *Client) init() error { + if c.api == nil { + c.api = api.NewDefaultClient(c.Cookie) + } if c.Cookie != "" { if !strings.Contains(c.Cookie, "bili_jct") || !strings.Contains(c.Cookie, "SESSDATA") { log.Errorf("cannot found account token") return errors.New("账号未登录") } - uid, err := api.GetUid(c.Cookie) + uid, err := c.api.GetUid() if err != nil { log.Error(err) } @@ -66,14 +85,14 @@ func (c *Client) init() error { c.Buvid = result[0][1] } } - roomInfo, err := api.GetRoomInfo(c.RoomID) + roomInfo, err := c.api.GetRoomInfo(c.RoomID) // 失败降级 if err != nil || roomInfo.Code != 0 { log.Errorf("room=%s init GetRoomInfo fialed, %s", c.RoomID, err) } c.RoomID = roomInfo.Data.RoomId if c.host == "" { - info, err := api.GetDanmuInfo(c.RoomID, c.Cookie) + info, err := c.api.GetDanmuInfo(c.RoomID) if err != nil { c.hostList = []string{"broadcastlv.chat.bilibili.com"} } else { diff --git a/client/handler.go b/client/handler.go index 57b6f72..5841325 100644 --- a/client/handler.go +++ b/client/handler.go @@ -1,9 +1,9 @@ package client import ( - "github.com/Akegarasu/blivedm-go/message" - "github.com/Akegarasu/blivedm-go/packet" - "github.com/Akegarasu/blivedm-go/utils" + "github.com/AynaLivePlayer/blivedm-go/message" + "github.com/AynaLivePlayer/blivedm-go/packet" + "github.com/AynaLivePlayer/blivedm-go/utils" log "github.com/sirupsen/logrus" "regexp" "runtime/debug" diff --git a/example/dump/main.go b/example/dump/main.go index 334ac9a..6eb0bfe 100644 --- a/example/dump/main.go +++ b/example/dump/main.go @@ -3,8 +3,8 @@ package main import ( "fmt" - "github.com/Akegarasu/blivedm-go/client" - _ "github.com/Akegarasu/blivedm-go/utils" + "github.com/AynaLivePlayer/blivedm-go/client" + _ "github.com/AynaLivePlayer/blivedm-go/utils" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" ) diff --git a/example/main.go b/example/main.go index b2e1ccd..9308157 100644 --- a/example/main.go +++ b/example/main.go @@ -3,10 +3,10 @@ package main import ( "fmt" - "github.com/Akegarasu/blivedm-go/api" - "github.com/Akegarasu/blivedm-go/client" - "github.com/Akegarasu/blivedm-go/message" - _ "github.com/Akegarasu/blivedm-go/utils" + "github.com/AynaLivePlayer/blivedm-go/api" + "github.com/AynaLivePlayer/blivedm-go/client" + "github.com/AynaLivePlayer/blivedm-go/message" + _ "github.com/AynaLivePlayer/blivedm-go/utils" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" ) diff --git a/go.mod b/go.mod index 76df81e..7a717ee 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/Akegarasu/blivedm-go +module github.com/AynaLivePlayer/blivedm-go go 1.16 diff --git a/message/danmu.go b/message/danmu.go index b20eb08..6044acc 100644 --- a/message/danmu.go +++ b/message/danmu.go @@ -1,8 +1,8 @@ package message import ( - "github.com/Akegarasu/blivedm-go/pb" - "github.com/Akegarasu/blivedm-go/utils" + "github.com/AynaLivePlayer/blivedm-go/pb" + "github.com/AynaLivePlayer/blivedm-go/utils" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" "google.golang.org/protobuf/proto" diff --git a/message/gift.go b/message/gift.go index 9c7efdb..30db519 100644 --- a/message/gift.go +++ b/message/gift.go @@ -1,7 +1,7 @@ package message import ( - "github.com/Akegarasu/blivedm-go/utils" + "github.com/AynaLivePlayer/blivedm-go/utils" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" ) diff --git a/message/guard.go b/message/guard.go index 799565f..8417d1b 100644 --- a/message/guard.go +++ b/message/guard.go @@ -1,7 +1,7 @@ package message import ( - "github.com/Akegarasu/blivedm-go/utils" + "github.com/AynaLivePlayer/blivedm-go/utils" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" ) diff --git a/message/superchat.go b/message/superchat.go index 86b06ba..0cd22a6 100644 --- a/message/superchat.go +++ b/message/superchat.go @@ -1,7 +1,7 @@ package message import ( - "github.com/Akegarasu/blivedm-go/utils" + "github.com/AynaLivePlayer/blivedm-go/utils" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" ) diff --git a/message/toast.go b/message/toast.go index aa6eea7..e19de29 100644 --- a/message/toast.go +++ b/message/toast.go @@ -1,7 +1,7 @@ package message import ( - "github.com/Akegarasu/blivedm-go/utils" + "github.com/AynaLivePlayer/blivedm-go/utils" log "github.com/sirupsen/logrus" "github.com/tidwall/gjson" )