]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
自定义KV配置-c
authorqydysky <32743305+qydysky@users.noreply.github.com>
Tue, 1 Nov 2022 12:15:48 +0000 (20:15 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Tue, 1 Nov 2022 12:15:48 +0000 (20:15 +0800)
CV/Var.go
README.md
Send/Send.go
bili_danmu.go

index 67ff89c6f7120a94f9edb0dad5fbc009a2a34e70..c47a488512967f99a1a49f2fc80dd8bc7af52c8a 100644 (file)
--- 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)
index dc8032ec63fedd7b59ecac9ab6dc8e92a8554124..9fcedffc8d7af1b303525a59001b4fe1d9fcd6a5 100644 (file)
--- a/README.md
+++ b/README.md
 ### 说明
 本项目使用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部署
index a0100699d4079c03c9b74c6fe3631df90252da9a..71763e40ddc2b9bbd0658603bab0c5faeb16a3cc 100644 (file)
@@ -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
        }
index e4176549bdcb1da2f9945f1be4bd2a0c0bdde442..f0b594ed1bb2936d5e14b9802e38ddfcb2d8a0e7 100644 (file)
@@ -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()