]> 127.0.0.1 Git - bili_danmu/.git/commitdiff
Add 弹幕分值 (#161)
authorqydysky <qydysky@foxmail.com>
Thu, 6 Feb 2025 16:39:54 +0000 (00:39 +0800)
committerGitHub <noreply@github.com>
Thu, 6 Feb 2025 16:39:54 +0000 (00:39 +0800)
Reply/F/comp.go
Reply/F/danmuCountPerMin/danmuCountPerMin.go
Reply/Reply.go
Reply/stream.go
cmd/cmd.go
demo/config/config_K_v.json

index 37a1688ae88672c79c76c318ff61aea40513068b..700315963e33e6a9091dc7a0f3c6dbdf69935daf 100644 (file)
@@ -19,8 +19,8 @@ import (
 var DanmuCountPerMin = comp.Get[interface {
        // will WriteHeader
        GetRec(savePath string, r *http.Request, w http.ResponseWriter) error
-       Rec(ctx context.Context, roomid int, savePath string)
-       Do(roomid int)
+       Rec(ctx context.Context, roomid int, savePath string) func(map[string]any)
+       Do(roomid int, msg string, uid string)
 }](`danmuCountPerMin`)
 
 var Ass = comp.Get[interface {
index 4f1dd86ce3c58f6e88521d4ae84d4bb56b62af6a..b0a1830a09d80d6774afa1ac50555b2bbf5c0d81 100644 (file)
@@ -6,6 +6,7 @@ import (
        "fmt"
        "net/http"
        "os"
+       "regexp"
        "time"
 
        comp "github.com/qydysky/part/component2"
@@ -18,13 +19,13 @@ import (
 type TargetInterface interface {
        // will WriteHeader
        GetRec(savePath string, r *http.Request, w http.ResponseWriter) error
-       Rec(ctx context.Context, roomid int, savePath string)
-       Do(roomid int)
+       Rec(ctx context.Context, roomid int, savePath string) func(map[string]any)
+       Do(roomid int, msg string, uid string)
 }
 
 func init() {
        if e := comp.Register[TargetInterface]("danmuCountPerMin", &danmuCountPerMin{
-               m: msgq.NewType[int](),
+               m: msgq.NewType[mi](),
        }); e != nil {
                panic(e)
        }
@@ -34,8 +35,13 @@ const filename = "danmuCountPerMin.json"
 
 var noFoundModT, _ = time.Parse(time.DateTime, "2006-01-02 15:04:05")
 
+type mi struct {
+       msg string
+       uid string
+}
+
 type danmuCountPerMin struct {
-       m *msgq.MsgType[int]
+       m *msgq.MsgType[mi]
 }
 
 func (t *danmuCountPerMin) GetRec(savePath string, r *http.Request, w http.ResponseWriter) error {
@@ -61,43 +67,82 @@ func (t *danmuCountPerMin) GetRec(savePath string, r *http.Request, w http.Respo
        return f.CopyToIoWriter(w, part.CopyConfig{})
 }
 
-func (t *danmuCountPerMin) Rec(ctx context.Context, rid int, savePath string) {
-       go func() {
-               var cpm []int
-               var startT = time.Now()
+func (t *danmuCountPerMin) Rec(ctx context.Context, rid int, savePath string) func(map[string]any) {
+       return func(cfg map[string]any) {
+               cfgMsg := make(map[*regexp.Regexp]int)
+               cfgUid := make(map[string]int)
+
+               if m, ok := cfg["danmu"].(map[string]any); ok {
+                       for k, v := range m {
+                               if point, ok := v.(float64); ok && point != 0 {
+                                       if reg, err := regexp.Compile(k); err == nil {
+                                               cfgMsg[reg] = int(point)
+                                       }
+                               }
+                       }
+               }
+
+               if m, ok := cfg["uid"].(map[string]any); ok {
+                       for k, v := range m {
+                               if point, ok := v.(float64); ok && point != 0 {
+                                       cfgUid[k] = int(point)
+                               }
+                       }
+               }
+
+               if len(cfgMsg)+len(cfgUid) == 0 {
+                       return
+               }
 
-               cancel := t.m.Pull_tag_only(`do`, func(roomid int) (disable bool) {
-                       if rid == roomid {
+               go func() {
+                       var cpm []int
+                       var startT = time.Now()
+
+                       cancel := t.m.Pull_tag_only(fmt.Sprintf("do%d", rid), func(i mi) (disable bool) {
+                               point := 0
+                               for k, v := range cfgMsg {
+                                       if k.MatchString(i.msg) {
+                                               point += v
+                                       }
+                               }
+                               for k, v := range cfgUid {
+                                       if k == i.uid {
+                                               point += v
+                                       }
+                               }
+                               if point == 0 {
+                                       return false
+                               }
                                cu := int(time.Since(startT).Minutes())
                                if len(cpm) < cu+1 {
                                        cpm = append(cpm, make([]int, cu+1-len(cpm))...)
                                }
-                               cpm[cu] += 1
-                       }
-                       return false
-               })
+                               cpm[cu] += point
+                               return false
+                       })
 
-               <-ctx.Done()
-               cancel()
+                       <-ctx.Done()
+                       cancel()
 
-               cu := int(time.Since(startT).Minutes())
-               if len(cpm) < cu+1 {
-                       cpm = append(cpm, make([]int, cu+1-len(cpm))...)
-               }
+                       cu := int(time.Since(startT).Minutes())
+                       if len(cpm) < cu+1 {
+                               cpm = append(cpm, make([]int, cu+1-len(cpm))...)
+                       }
 
-               if data, e := json.MarshalIndent(cpm, "", " "); e != nil {
-                       fmt.Println(e)
-               } else {
-                       f := file.New(savePath+filename, 0, true)
-                       defer f.Close()
-                       _ = f.Delete()
-                       if _, e = f.Write(data, false); e != nil {
+                       if data, e := json.MarshalIndent(cpm, "", " "); e != nil {
                                fmt.Println(e)
+                       } else {
+                               f := file.New(savePath+filename, 0, true)
+                               defer f.Close()
+                               _ = f.Delete()
+                               if _, e = f.Write(data, false); e != nil {
+                                       fmt.Println(e)
+                               }
                        }
-               }
-       }()
+               }()
+       }
 }
 
-func (t *danmuCountPerMin) Do(roomid int) {
-       t.m.Push_tag(`do`, roomid)
+func (t *danmuCountPerMin) Do(roomid int, msg string, uid string) {
+       t.m.Push_tag(fmt.Sprintf("do%d", roomid), mi{msg, uid})
 }
index 58851f813fa58d554b7ef510de9795ff52703427..a20a0b8d28e48106daecdfab29192c2f6b7a40ab 100644 (file)
@@ -1318,7 +1318,7 @@ func (t replyF) danmu(s string) {
 
        { // 附加功能 弹幕机 封禁 弹幕合并
                // 弹幕统计
-               replyFunc.DanmuCountPerMin.Do(item.roomid)
+               replyFunc.DanmuCountPerMin.Do(item.roomid, item.msg, item.uid)
                // 保存弹幕至db
                saveDanmuToDB.init(t.Common)
                saveDanmuToDB.danmu(item)
index a5cb5051b88f5534e6aff0e8e77294b5c0a7d1b1..57932125e81db2bfd1a842a33ed238ea471e8a21 100644 (file)
@@ -1405,7 +1405,10 @@ func (t *M4SStream) Start() bool {
                                        l := ms.log.Base_add(`文件保存`)
                                        startf := func(_ *M4SStream) error {
                                                l.L(`T: `, `开始`)
-                                               go replyFunc.DanmuCountPerMin.Rec(contextC, ms.common.Roomid, ms.GetSavePath()) //弹幕数量统计
+                                               //弹幕分值统计
+                                               if m, ok := ms.common.K_v.LoadV("弹幕分值").(map[string]any); ok {
+                                                       replyFunc.DanmuCountPerMin.Rec(contextC, ms.common.Roomid, ms.GetSavePath())(m)
+                                               }
                                                return nil
                                        }
                                        stopf := func(_ *M4SStream) error {
index d4c8252b0ea29d9d819a7d2a6408ebf3afc21978..457e7f530ec32b7e78ec5a03c7e5002a2b89ff35 100644 (file)
@@ -48,7 +48,7 @@ func Cmd() {
                        fmt.Println("搜索主播->输入' sea关键词'回车")
                        fmt.Println("房间信息->输入' room'回车")
                        fmt.Println("开始结束录制->输入' rec'回车")
-                       fmt.Println("å½\95æ\92­å\88\87ç\89\87->输入' cut'回车")
+                       fmt.Println("å½\95æ\92­å\88\86段->输入' cut'回车")
                        fmt.Println("退出当前房间->输入' exit'回车")
                        fmt.Println("其他输出隔断不影响")
                        fmt.Print("\n")
@@ -56,7 +56,7 @@ func Cmd() {
                        cmdlog.L(`W: `, "不支持功能键")
                } else if inputs[0] == 32 { // 开头
                        cmdlog.L(`T: `, "指令("+inputs+")")
-                       //å½\95æ\92­å\88\87ç\89\87
+                       //å½\95æ\92­å\88\86段
                        if strings.Contains(inputs, ` cut`) {
                                if c.C.Roomid != 0 && reply.StreamOStatus(c.C.Roomid) {
                                        reply.StreamOCut(c.C.Roomid)
index a4bac5cc51e011071c78043582c0c6dc87df21c7..95951401f704017947625d2c57f904b50b8dd633 100644 (file)
             "type": "",
             "env": ""
         }
-    ]
+    ],
+    "弹幕分值-help":"对每条弹幕进行评分,每分钟输出分值到danmuCountPerMin.json,无有效规则时不生成json,用于后续评估",
+    "弹幕分值":{
+        "danmu-help":"key为正则表达式,匹配成功时分值加value,value为0时忽略",
+        "danmu": {
+            ".":1
+        }, 
+        "uid-help":"key为uid,相等时分值加value,value为0时忽略",
+        "uid": {
+            "":0
+        }
+    }
 }