From: qydysky Date: Thu, 6 Feb 2025 16:39:54 +0000 (+0800) Subject: Add 弹幕分值 (#161) X-Git-Tag: v0.15.7~5 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=bf49ff8eeeefc00e88e6488e2f39e0cf94b412f4;p=bili_danmu%2F.git Add 弹幕分值 (#161) --- diff --git a/Reply/F/comp.go b/Reply/F/comp.go index 37a1688..7003159 100644 --- a/Reply/F/comp.go +++ b/Reply/F/comp.go @@ -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 { diff --git a/Reply/F/danmuCountPerMin/danmuCountPerMin.go b/Reply/F/danmuCountPerMin/danmuCountPerMin.go index 4f1dd86..b0a1830 100644 --- a/Reply/F/danmuCountPerMin/danmuCountPerMin.go +++ b/Reply/F/danmuCountPerMin/danmuCountPerMin.go @@ -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}) } diff --git a/Reply/Reply.go b/Reply/Reply.go index 58851f8..a20a0b8 100644 --- a/Reply/Reply.go +++ b/Reply/Reply.go @@ -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) diff --git a/Reply/stream.go b/Reply/stream.go index a5cb505..5793212 100644 --- a/Reply/stream.go +++ b/Reply/stream.go @@ -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 { diff --git a/cmd/cmd.go b/cmd/cmd.go index d4c8252..457e7f5 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -48,7 +48,7 @@ func Cmd() { fmt.Println("搜索主播->输入' sea关键词'回车") fmt.Println("房间信息->输入' room'回车") fmt.Println("开始结束录制->输入' rec'回车") - fmt.Println("录播切片->输入' cut'回车") + fmt.Println("录播分段->输入' 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+")") - //录播切片 + //录播分段 if strings.Contains(inputs, ` cut`) { if c.C.Roomid != 0 && reply.StreamOStatus(c.C.Roomid) { reply.StreamOCut(c.C.Roomid) diff --git a/demo/config/config_K_v.json b/demo/config/config_K_v.json index a4bac5c..9595140 100644 --- a/demo/config/config_K_v.json +++ b/demo/config/config_K_v.json @@ -233,5 +233,16 @@ "type": "", "env": "" } - ] + ], + "弹幕分值-help":"对每条弹幕进行评分,每分钟输出分值到danmuCountPerMin.json,无有效规则时不生成json,用于后续评估", + "弹幕分值":{ + "danmu-help":"key为正则表达式,匹配成功时分值加value,value为0时忽略", + "danmu": { + ".":1 + }, + "uid-help":"key为uid,相等时分值加value,value为0时忽略", + "uid": { + "":0 + } + } }