]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
自定义配置文件支持http链接
authorqydysky <32743305+qydysky@users.noreply.github.com>
Sat, 5 Nov 2022 13:53:10 +0000 (21:53 +0800)
committerqydysky <32743305+qydysky@users.noreply.github.com>
Sat, 5 Nov 2022 13:53:10 +0000 (21:53 +0800)
CV/Var.go
README.md
Reply/F.go
Reply/Reply.go
demo/config/config_K_v.json
demo/main.go

index 35264f3aa0feb0090b6499f45c95b03712363a38..c4fc30bb1c4a81e54c3a4e2fc468ebf690ac2c10 100644 (file)
--- a/CV/Var.go
+++ b/CV/Var.go
@@ -4,7 +4,9 @@ import (
        "encoding/json"
        "errors"
        "flag"
+       "fmt"
        "io"
+       "strings"
        "testing"
        "time"
 
@@ -71,6 +73,10 @@ func (t *Common) Init() Common {
 
        t.Danmu_Main_mq = mq.New(200)
 
+       t.ReqPool = idpool.New(func() interface{} {
+               return reqf.New()
+       })
+
        var (
                ckv     = flag.String("ckv", "", "自定义配置KV文件,将会覆盖config_K_v配置")
                roomIdP = flag.Int("r", 0, "roomid")
@@ -79,30 +85,8 @@ func (t *Common) Init() Common {
        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{}
-                       json.Unmarshal(bb, &data)
-                       for k, v := range data {
-                               t.K_v.Store(k, v)
-                       }
-               } else {
-                       panic(err)
-               }
-       }
-
-       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)
-                       }
-               }
+       if e := t.loadConf(*ckv); e != nil {
+               panic(e)
        }
 
        go func() {
@@ -115,32 +99,7 @@ func (t *Common) Init() Common {
                        }
                        time.Sleep(time.Duration(int(v)) * time.Second)
 
-                       // 64k
-                       if bb, e := file.New("config/config_K_v.json", 0, true).ReadAll(100, 1<<16); e != nil {
-                               if !errors.Is(e, io.EOF) {
-                                       panic(e)
-                               } else {
-                                       var data map[string]interface{}
-                                       json.Unmarshal(bb, &data)
-                                       for k, v := range data {
-                                               t.K_v.Store(k, v)
-                                       }
-                               }
-                       }
-
-                       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)
-                                       }
-                               }
-                       }
+                       fmt.Println(t.loadConf(*ckv))
                }
        }()
 
@@ -170,12 +129,76 @@ func (t *Common) Init() Common {
                t.Log = t.Log.Level(logmap)
        }
 
-       t.ReqPool = idpool.New(func() interface{} {
-               return reqf.New()
-       })
        return *t
 }
 
+func (t *Common) loadConf(customConf string) error {
+       var data map[string]interface{}
+
+       // 64k
+       if bb, e := file.New("config/config_K_v.json", 0, true).ReadAll(100, 1<<16); e != nil {
+               if !errors.Is(e, io.EOF) {
+                       return e
+               } else {
+                       json.Unmarshal(bb, &data)
+               }
+       }
+
+       if customConf != "" {
+               if strings.Contains(customConf, "http") {
+                       //从网址读取
+                       req := t.ReqPool.Get()
+                       r := req.Item.(*reqf.Req)
+                       if e := r.Reqf(reqf.Rval{
+                               Url: customConf,
+                               Header: map[string]string{
+                                       `User-Agent`:      `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0`,
+                                       `Accept`:          `*/*`,
+                                       `Accept-Language`: `zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2`,
+                                       `Accept-Encoding`: `gzip, deflate, br`,
+                                       `Pragma`:          `no-cache`,
+                                       `Cache-Control`:   `no-cache`,
+                                       `Connection`:      `close`,
+                               },
+                               Timeout: 10 * 1000,
+                       }); e != nil {
+                               return errors.New("无法获取自定义配置文件 " + e.Error())
+                       }
+                       if r.Response == nil {
+                               return errors.New("无法获取自定义配置文件 响应为空")
+                       } else if r.Response.StatusCode != 200 {
+                               return fmt.Errorf("无法获取自定义配置文件 %d", r.Response.StatusCode)
+                       } else {
+                               var tmp map[string]interface{}
+                               json.Unmarshal(r.Respon, &tmp)
+                               for k, v := range tmp {
+                                       data[k] = v
+                               }
+                       }
+                       t.ReqPool.Put(req)
+               } else {
+                       //从文件读取
+                       if bb, err := file.New(customConf, 0, true).ReadAll(100, 1<<16); err != nil {
+                               if errors.Is(err, io.EOF) {
+                                       var tmp map[string]interface{}
+                                       json.Unmarshal(bb, &tmp)
+                                       for k, v := range tmp {
+                                               data[k] = v
+                                       }
+                               } else {
+                                       return err
+                               }
+                       }
+               }
+       }
+
+       for k, v := range data {
+               t.K_v.Store(k, v)
+       }
+
+       return nil
+}
+
 var C = new(Common).Init()
 
 // 消息队列
index f78ddf84679abed24a3c1416d8dd144e464eb92d..5b1b3b5d0d9eee80d0538f0e3080a661c7a30562 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中的配置项。使用此配置,可以在有新配置项时,默认使用新配置项而保持之前其他的配置。
+当启动时使用`-ckv 路径`,将从此路径(或http地址)加载config_K_v.json并覆盖默认config_K_v.json中的配置项。
+使用此配置,可以在有新配置项时,默认使用新配置项而保持之前其他的配置。
 
 #### 表情发送
 当`demo/config/config_K_v.json`中`弹幕_识别表情代码`为`true`时,发送特定的文字将发送表情。
index 8d2d5e6a86da5753882e6bf75152402ec94b306e..ac3a4b3ca76f5188013251c05b33635904bc60e6 100644 (file)
@@ -1165,7 +1165,7 @@ func init() {
                                                } else {
                                                        w.Header().Set("Retry-After", "1")
                                                        w.WriteHeader(http.StatusServiceUnavailable)
-                                                       flog.L(`E: `, "未找到流文件", v)
+                                                       flog.L(`I: `, "未找到流文件", v)
                                                        return
                                                }
 
index 2ebb3bc3940b8ac7c7b0191409a67e8ce6cee268..3ec4ead7a17f10c53b0b451f9b3ae937a26a1168 100644 (file)
@@ -330,6 +330,9 @@ var (
 )
 
 func (replyF) heartbeat(s int) {
+       if v, ok := c.C.K_v.LoadV("下播后不记录人气观看人数").(bool); ok && v && !c.C.Liveing {
+               return
+       }
        c.C.Danmu_Main_mq.Push_tag(`c.Renqi`, s) //使用连续付费的新舰长无法区分,刷新舰长数
        if s == 1 {
                return
@@ -387,6 +390,9 @@ func (replyF) win_activity(s string) {
 
 // Msg-观看人数
 func (replyF) watched_change(s string) {
+       if v, ok := c.C.K_v.LoadV("下播后不记录人气观看人数").(bool); ok && v && !c.C.Liveing {
+               return
+       }
        var data ws_msg.WATCHED_CHANGE
        json.Unmarshal([]byte(s), &data)
        // fmt.Printf("\t观看人数:%d\n", watched)
index 99df4b2bcddfc9aa118720c1d0addb8989984d8d..095841bf03580ee2a3af14088416a49a63ee516a 100644 (file)
@@ -99,5 +99,6 @@
     "http代理地址":"",
     "启动时显示ip":true,
     "几秒后重载-help":"最少间隔60s,-1时不重载",
-    "几秒后重载":60
+    "几秒后重载":60,
+    "下播后不记录人气观看人数":true
 }
\ No newline at end of file
index a0a5dcfaf5c5ec00548d0adc9b6de7fd33203db8..79af7380ed43bfb58a98d7a2ad047920e705455c 100644 (file)
@@ -1,7 +1,6 @@
 package main
 
 import (
-       "time"
        // "runtime"
        // "runtime/pprof"
        "os"
@@ -9,7 +8,6 @@ import (
        "fmt"
        // "net/http"
        // _ "net/http/pprof"
-       "runtime/debug"
 
        // "github.com/skratchdot/open-golang/open"
        q "github.com/qydysky/bili_danmu"
@@ -23,16 +21,16 @@ func main() {
        //      open.Run("http://127.0.0.1:8899/debug/pprof/goroutine?debug=2")
        //      time.Sleep(time.Duration(3)*time.Second)
        // }()
-       go func() {
-               fmt.Printf("PID:%d\n", os.Getpid())
-               for {
-                       View()
-                       time.Sleep(time.Duration(60) * time.Second)
-                       {
-                               debug.FreeOSMemory()
-                       }
-               }
-       }()
+       // go func() {
+       fmt.Printf("PID:%d\n", os.Getpid())
+       // for {
+       //      View()
+       //      time.Sleep(time.Duration(60) * time.Second)
+       //      {
+       //              debug.FreeOSMemory()
+       //      }
+       // }
+       // }()
        // f, err := os.OpenFile("cpu.pprof", os.O_RDWR|os.O_CREATE, 0644)
        // if err != nil {
        //     log.Fatal(err)
@@ -45,12 +43,12 @@ func main() {
        // pprof.StopCPUProfile()
 }
 
-func View() {
-       // var memStats runtime.MemStats
-       // runtime.ReadMemStats(&memStats)
-       // fmt.Printf("=====\n")
-       // fmt.Printf("总内存:%v MB\n",memStats.Alloc/1024e2/8)
-       // fmt.Printf("GC次数:%v \n",memStats.NumGC)
-       // fmt.Printf("堆 :%v %v MB\n",memStats.HeapInuse/1024e2/8,(memStats.HeapIdle - memStats.HeapReleased)/1024e2/8)
-       // fmt.Printf("=====\n")
-}
+// func View() {
+// var memStats runtime.MemStats
+// runtime.ReadMemStats(&memStats)
+// fmt.Printf("=====\n")
+// fmt.Printf("总内存:%v MB\n",memStats.Alloc/1024e2/8)
+// fmt.Printf("GC次数:%v \n",memStats.NumGC)
+// fmt.Printf("堆 :%v %v MB\n",memStats.HeapInuse/1024e2/8,(memStats.HeapIdle - memStats.HeapReleased)/1024e2/8)
+// fmt.Printf("=====\n")
+// }