update api

This commit is contained in:
aynakeya
2024-04-07 23:46:17 -07:00
parent 77e5a27ca9
commit 5e144eab2b
12 changed files with 70 additions and 24 deletions

View File

@@ -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"
)

27
api/api.go Normal file
View File

@@ -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}
}

View File

@@ -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 {

View File

@@ -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"

View File

@@ -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"
)

View File

@@ -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"
)

2
go.mod
View File

@@ -1,4 +1,4 @@
module github.com/Akegarasu/blivedm-go
module github.com/AynaLivePlayer/blivedm-go
go 1.16

View File

@@ -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"

View File

@@ -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"
)

View File

@@ -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"
)

View File

@@ -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"
)

View File

@@ -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"
)