From 68cca0e21d3ea12cf450e455eccbd7611ccfd472 Mon Sep 17 00:00:00 2001 From: Akiba Date: Thu, 24 Mar 2022 18:41:18 +0800 Subject: [PATCH] :sparkles: add live danmaku related apis --- api/info.go | 56 +++++++++++++++++++++++++++++++++++ api/send.go | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 140 insertions(+) create mode 100644 api/info.go create mode 100644 api/send.go diff --git a/api/info.go b/api/info.go new file mode 100644 index 0000000..b743859 --- /dev/null +++ b/api/info.go @@ -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 +} diff --git a/api/send.go b/api/send.go new file mode 100644 index 0000000..697851c --- /dev/null +++ b/api/send.go @@ -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) +}