From c9c760a47a6f723dbf41069a40ece0bede9710b3 Mon Sep 17 00:00:00 2001 From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Sat, 5 Nov 2022 21:53:10 +0800 Subject: [PATCH] =?utf8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E9=85=8D=E7=BD=AE?= =?utf8?q?=E6=96=87=E4=BB=B6=E6=94=AF=E6=8C=81http=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- CV/Var.go | 129 +++++++++++++++++++++--------------- README.md | 3 +- Reply/F.go | 2 +- Reply/Reply.go | 6 ++ demo/config/config_K_v.json | 3 +- demo/main.go | 40 ++++++----- 6 files changed, 106 insertions(+), 77 deletions(-) diff --git a/CV/Var.go b/CV/Var.go index 35264f3..c4fc30b 100644 --- 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() // 消息队列 diff --git a/README.md b/README.md index f78ddf8..5b1b3b5 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,8 @@ 本项目使用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`时,发送特定的文字将发送表情。 diff --git a/Reply/F.go b/Reply/F.go index 8d2d5e6..ac3a4b3 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -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 } diff --git a/Reply/Reply.go b/Reply/Reply.go index 2ebb3bc..3ec4ead 100644 --- a/Reply/Reply.go +++ b/Reply/Reply.go @@ -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) diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json index 99df4b2..095841b 100644 --- a/demo/config/config_K_v.json +++ b/demo/config/config_K_v.json @@ -99,5 +99,6 @@ "http代理地址":"", "启动时显示ip":true, "几秒后重载-help":"最少间隔60s,-1时不重载", - "几秒后重载":60 + "几秒后重载":60, + "下播后不记录人气观看人数":true } \ No newline at end of file diff --git a/demo/main.go b/demo/main.go index a0a5dcf..79af738 100644 --- a/demo/main.go +++ b/demo/main.go @@ -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") +// } -- 2.39.2