From 177d3612ffeee72618a9a4c75aa8106a0fcaf8fe Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Tue, 1 Nov 2022 20:15:48 +0800 Subject: [PATCH] =?utf8?q?=E8=87=AA=E5=AE=9A=E4=B9=89KV=E9=85=8D=E7=BD=AE-?= =?utf8?q?c?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CV/Var.go | 28 +++++++++++++++++++++++++++- README.md | 5 ++++- Send/Send.go | 4 ++-- bili_danmu.go | 24 +++++++----------------- 4 files changed, 40 insertions(+), 21 deletions(-) diff --git a/CV/Var.go b/CV/Var.go index 67ff89c..c47a488 100644 --- a/CV/Var.go +++ b/CV/Var.go @@ -3,6 +3,7 @@ package cv import ( "encoding/json" "errors" + "flag" "io" "time" @@ -45,7 +46,7 @@ type Common struct { K_v syncmap.Map //配置文件 Log *log.Log_interface //日志 Danmu_Main_mq *mq.Msgq //消息 - ReqPool *idpool.Idpool // 请求池 + ReqPool *idpool.Idpool //请求池 } func (t *Common) init() Common { @@ -69,6 +70,13 @@ func (t *Common) init() Common { t.Danmu_Main_mq = mq.New(200) + var ( + ckv = flag.String("ckv", "", "自定义配置KV文件,将会覆盖config_K_v配置") + roomIdP = flag.Int("r", 0, "roomid") + ) + flag.Parse() + t.Roomid = *roomIdP + if bb, err := file.New("config/config_K_v.json", 0, true).ReadAll(100, 1<<16); err != nil { if errors.Is(err, io.EOF) { var data map[string]interface{} @@ -81,6 +89,24 @@ func (t *Common) init() Common { } } + if t.K_v.Len() == 0 { + panic("未能加载配置") + } + + if *ckv != "" { + if bb, err := file.New(*ckv, 0, true).ReadAll(100, 1<<16); err != nil { + if errors.Is(err, io.EOF) { + var data map[string]interface{} + json.Unmarshal(bb, &data) + for k, v := range data { + t.K_v.Store(k, v) + } + } else { + panic(err) + } + } + } + go func() { for { v, ok := t.K_v.LoadV("几秒后重载").(float64) diff --git a/README.md b/README.md index dc8032e..9fcedff 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,9 @@ ### 说明 本项目使用github action自动构建,构建过程详见[yml](https://github.com/qydysky/bili_danmu/blob/master/.github/workflows/go.yml) +#### 自定义config_K_v.json +当启动时使用`-ckv 路径`,将从此路径加载config_K_v.json并覆盖默认config_K_v.json中的配置项。使用此配置,可以在有新配置项时,默认使用新配置项而保持之前其他的配置。 + #### 表情发送 当`demo/config/config_K_v.json`中`弹幕_识别表情代码`为`true`时,发送特定的文字将发送表情。 @@ -392,7 +395,7 @@ go build [-tags "gtk"] main.go clone/下载本项目。进入`demo`目录(文件夹),运行: ``` -go run [-tags "gtk"] main.go [-r 房间ID] +go run [-tags "gtk"] main.go [-r 房间ID] [-ckv 自定义config_K_v.json] ``` 3. docker部署 diff --git a/Send/Send.go b/Send/Send.go index a010069..71763e4 100644 --- a/Send/Send.go +++ b/Send/Send.go @@ -2,11 +2,11 @@ package send import ( "encoding/json" - "io/ioutil" "strconv" c "github.com/qydysky/bili_danmu/CV" + file "github.com/qydysky/part/file" limit "github.com/qydysky/part/limit" reqf "github.com/qydysky/part/reqf" sys "github.com/qydysky/part/sys" @@ -18,7 +18,7 @@ var damnu_official = make(map[string]string) // 初始化表情代码 func init() { - bb, err := ioutil.ReadFile("config/config_danmu_official.json") + bb, err := file.New("config/config_danmu_official.json", 0, true).ReadAll(1000, 1<<16) if err != nil { return } diff --git a/bili_danmu.go b/bili_danmu.go index e417654..f0b594e 100644 --- a/bili_danmu.go +++ b/bili_danmu.go @@ -2,7 +2,6 @@ package bili_danmu import ( _ "embed" - "flag" "fmt" "net/url" "os" @@ -38,7 +37,7 @@ func init() { }() } -func Start(roomid ...int) { +func Start() { var danmulog = c.C.Log.Base(`bilidanmu`) defer danmulog.Block(1000) @@ -68,36 +67,27 @@ func Start(roomid ...int) { } { - var groomid = flag.Int("r", 0, "roomid") - flag.Parse() - var ( change_room_chan = make(chan struct{}) flash_room_chan = make(chan struct{}) ) - //-r 房间初始化 - var room = *groomid - if room == 0 && len(roomid) != 0 { - room = roomid[0] - } - //如果连接中断,则等待 F.KeepConnect() //获取cookie F.Get(&c.C).Get(`Cookie`) //获取LIVE_BUVID F.Get(&c.C).Get(`LIVE_BUVID`) - if room == 0 { + + // 房间初始化 + if c.C.Roomid == 0 { c.C.Log.Block(1000) //等待所有日志输出完毕 fmt.Println("输入房间号或` live`获取正在直播的主播") } else { - fmt.Print("房间号: ", strconv.Itoa(room), "\n") - if c.C.Roomid == 0 { - c.C.Roomid = room - go func() { change_room_chan <- struct{}{} }() - } + fmt.Print("房间号: ", strconv.Itoa(c.C.Roomid), "\n") + go func() { change_room_chan <- struct{}{} }() } + //命令行操作 切换房间 发送弹幕 go Cmd.Cmd() -- 2.39.2