mirror of
https://github.com/AynaLivePlayer/blivedm-go.git
synced 2025-12-07 20:02:50 +08:00
✨ add live danmaku related apis
This commit is contained in:
56
api/info.go
Normal file
56
api/info.go
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
// RoomInfo
|
||||||
|
// api https://api.live.bilibili.com/room/v1/Room/room_init?id={} response
|
||||||
|
type RoomInfo struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Data struct {
|
||||||
|
RoomId int `json:"room_id"`
|
||||||
|
ShortId int `json:"short_id"`
|
||||||
|
Uid int `json:"uid"`
|
||||||
|
NeedP2P int `json:"need_p2p"`
|
||||||
|
IsHidden bool `json:"is_hidden"`
|
||||||
|
IsLocked bool `json:"is_locked"`
|
||||||
|
IsPortrait bool `json:"is_portrait"`
|
||||||
|
LiveStatus int `json:"live_status"`
|
||||||
|
HiddenTill int `json:"hidden_till"`
|
||||||
|
LockTill int `json:"lock_till"`
|
||||||
|
Encrypted bool `json:"encrypted"`
|
||||||
|
PwdVerified bool `json:"pwd_verified"`
|
||||||
|
LiveTime int64 `json:"live_time"`
|
||||||
|
RoomShield int `json:"room_shield"`
|
||||||
|
IsSp int `json:"is_sp"`
|
||||||
|
SpecialType int `json:"special_type"`
|
||||||
|
} `json:"data"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func getRoomInfo(roomID string) (*RoomInfo, error) {
|
||||||
|
url := fmt.Sprintf("https://api.live.bilibili.com/room/v1/Room/room_init?id=%s", roomID)
|
||||||
|
resp, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
result := &RoomInfo{}
|
||||||
|
if err = json.NewDecoder(resp.Body).Decode(result); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetRoomRealID(roomID string) (string, error) {
|
||||||
|
res, err := getRoomInfo(roomID)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return strconv.Itoa(res.Data.RoomId), nil
|
||||||
|
}
|
||||||
84
api/send.go
Normal file
84
api/send.go
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
package api
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"net/url"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type DanmakuRequest struct {
|
||||||
|
Msg string
|
||||||
|
RoomID string
|
||||||
|
Bubble string
|
||||||
|
Color string
|
||||||
|
FontSize string
|
||||||
|
Mode string
|
||||||
|
DmType string
|
||||||
|
}
|
||||||
|
|
||||||
|
type SendDanmakuResp struct {
|
||||||
|
Code int `json:"code"`
|
||||||
|
Data struct {
|
||||||
|
ModeInfo struct {
|
||||||
|
Mode int `json:"mode"`
|
||||||
|
ShowPlayerType int `json:"show_player_type"`
|
||||||
|
Extra string `json:"extra"`
|
||||||
|
} `json:"mode_info"`
|
||||||
|
} `json:"data"`
|
||||||
|
Message string `json:"message"`
|
||||||
|
Msg string `json:"msg"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type BiliVerify struct {
|
||||||
|
Csrf string
|
||||||
|
SessData string
|
||||||
|
}
|
||||||
|
|
||||||
|
// SendDanmaku https://api.live.bilibili.com/msg/send
|
||||||
|
func SendDanmaku(d *DanmakuRequest, v *BiliVerify) (*SendDanmakuResp, error) {
|
||||||
|
client := &http.Client{}
|
||||||
|
result := &SendDanmakuResp{}
|
||||||
|
form := url.Values{
|
||||||
|
"bubble": {d.Bubble},
|
||||||
|
"color": {d.Color},
|
||||||
|
"fontsize": {d.FontSize},
|
||||||
|
"mode": {d.Mode},
|
||||||
|
"msg": {d.Msg},
|
||||||
|
"roomid": {d.RoomID},
|
||||||
|
"csrf": {v.Csrf},
|
||||||
|
"csrf_token": {v.Csrf},
|
||||||
|
"rnd": {"1"},
|
||||||
|
}
|
||||||
|
// dm_type 为 1 时,发送的是表情弹幕
|
||||||
|
if d.DmType != "" {
|
||||||
|
form.Add("dm_type", d.DmType)
|
||||||
|
}
|
||||||
|
req, err := http.NewRequest("POST", "https://api.live.bilibili.com/msg/send", strings.NewReader(form.Encode()))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
req.Header.Set("Cookie", fmt.Sprintf("bili_jct=%s;SESSDATA=%s", v.Csrf, v.SessData))
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if err := json.NewDecoder(resp.Body).Decode(result); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return result, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SendDefaultDanmaku(roomID string, message string, verify *BiliVerify) (*SendDanmakuResp, error) {
|
||||||
|
req := &DanmakuRequest{
|
||||||
|
Msg: message,
|
||||||
|
RoomID: roomID,
|
||||||
|
Bubble: "0",
|
||||||
|
Color: "16777215",
|
||||||
|
FontSize: "25",
|
||||||
|
Mode: "1",
|
||||||
|
DmType: "1",
|
||||||
|
}
|
||||||
|
return SendDanmaku(req, verify)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user