From: qydysky Date: Thu, 9 Jan 2025 11:46:57 +0000 (+0800) Subject: Improve 表情保存 (#145) X-Git-Tag: v0.15.0~4 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=dcc9be8c21ee7ad7883d491bbe506e1f820daf1a;p=bili_danmu%2F.git Improve 表情保存 (#145) --- diff --git a/Reply/F.go b/Reply/F.go index 1819727..0871382 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -29,7 +29,6 @@ import ( c "github.com/qydysky/bili_danmu/CV" F "github.com/qydysky/bili_danmu/F" replyFunc "github.com/qydysky/bili_danmu/Reply/F" - danmuemotes "github.com/qydysky/bili_danmu/Reply/F/danmuEmotes" "github.com/qydysky/bili_danmu/Reply/F/danmuXml" "github.com/qydysky/bili_danmu/Reply/F/keepMedalLight" videoInfo "github.com/qydysky/bili_danmu/Reply/F/videoInfo" @@ -1012,7 +1011,7 @@ func init() { } if strings.HasPrefix(p, "emots/") { - p, _ = danmuemotes.Hashr.Run(context.Background(), p) + p = replyFunc.DanmuEmotes.Hashr(p) } else { p = "html/artPlayer/" + p } diff --git a/Reply/F/comp.go b/Reply/F/comp.go index f6a4a7b..7add94a 100644 --- a/Reply/F/comp.go +++ b/Reply/F/comp.go @@ -7,8 +7,10 @@ import ( _ "github.com/qydysky/bili_danmu/Reply/F/ass" _ "github.com/qydysky/bili_danmu/Reply/F/danmuCountPerMin" + _ "github.com/qydysky/bili_danmu/Reply/F/danmuEmotes" _ "github.com/qydysky/bili_danmu/Reply/F/danmuji" comp "github.com/qydysky/part/component2" + log "github.com/qydysky/part/log" ) var DanmuCountPerMin = comp.Get[interface { @@ -27,3 +29,19 @@ var Danmuji = comp.Get[interface { Danmujif(s string, then func(string)) Danmuji_auto(ctx context.Context, danmus []any, waitSec float64, then func(string)) }](`danmuji`) + +type DanmuEmotesS struct { + Logg *log.Log_interface + Info []any + Msg *string +} + +var DanmuEmotes = comp.Get[interface { + SaveEmote(ctx context.Context, ptr struct { + Logg *log.Log_interface + Info []any + Msg *string + }) (ret any, err error) + Hashr(s string) (r string) + SetLayerN(n int) +}](`danmuEmotes`) diff --git a/Reply/F/danmuEmotes/danmuEmotes.go b/Reply/F/danmuEmotes/danmuEmotes.go index bca94ee..d4fc357 100644 --- a/Reply/F/danmuEmotes/danmuEmotes.go +++ b/Reply/F/danmuEmotes/danmuEmotes.go @@ -6,47 +6,48 @@ import ( "strings" c "github.com/qydysky/bili_danmu/CV" - comp "github.com/qydysky/part/component" + comp "github.com/qydysky/part/component2" file "github.com/qydysky/part/file" phash "github.com/qydysky/part/hash" log "github.com/qydysky/part/log" reqf "github.com/qydysky/part/reqf" ) -// path -var ( - SaveEmote = comp.NewComp(saveEmote) - Hashr = comp.NewComp(func(ctx context.Context, s string) (r string, e error) { - return hashr(s), nil - }) - // danmuPool = ppool.New(ppool.PoolFunc[pslice.Buf[byte]]{ - // New: func() *pslice.Buf[byte] { - // return pslice.New[byte]() - // }, - // InUse: func(b *pslice.Buf[byte]) bool { - // return !b.IsEmpty() - // }, - // Reuse: func(b *pslice.Buf[byte]) *pslice.Buf[byte] { - // return b - // }, - // Pool: func(b *pslice.Buf[byte]) *pslice.Buf[byte] { - // b.Reset() - // return b - // }, - // }, 100) -) - -type Danmu struct { - Logg *log.Log_interface - Info []any - Msg *string +type TargetInterface interface { + SaveEmote(ctx context.Context, ptr struct { + Logg *log.Log_interface + Info []any + Msg *string + }) (ret any, err error) + Hashr(s string) (r string) + SetLayerN(n int) } func init() { - _, _ = file.New("emots/README.md", 0, true).Write([]byte(""), false) + i := danmuEmotes{ + Dir: "emots/", + LayerN: 0, + } + if e := comp.Register[TargetInterface]("danmuEmotes", &i); e != nil { + panic(e) + } + _, _ = file.New(i.Dir+"README.md", 0, true).Write([]byte(""), false) } -func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) { +type danmuEmotes struct { + Dir string + LayerN int +} + +func (t *danmuEmotes) SetLayerN(n int) { + t.LayerN = n +} + +func (t *danmuEmotes) SaveEmote(ctx context.Context, ptr struct { + Logg *log.Log_interface + Info []any + Msg *string +}) (ret any, err error) { if m, ok := ptr.Info[13].(map[string]any); ok { if url, ok := m[`url`].(string); ok { if !strings.Contains(*ptr.Msg, "[") { @@ -54,7 +55,7 @@ func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) { *ptr.Msg = "[" + *ptr.Msg + emoticon_unique + "]" } } - savePath := "emots/" + hashr(*ptr.Msg) + ".png" + savePath := t.Dir + t.Hashr(*ptr.Msg) + ".png" if !file.New(savePath, 0, true).IsExist() { go func() { req := c.C.ReqPool.Get() @@ -100,7 +101,7 @@ func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) { continue } - savePath := "emots/" + hashr(k) + ".png" + savePath := t.Dir + t.Hashr(k) + ".png" if file.New(savePath, 0, true).IsExist() { continue } @@ -134,45 +135,19 @@ func saveEmote(ctx context.Context, ptr Danmu) (ret any, err error) { return } -func hashr(s string) (r string) { - return phash.Md5String(s) - - // buf := danmuPool.Get() - // defer danmuPool.Put(buf) - - // emoteB := false - // for i := 0; i < len(s); i++ { - // if !emoteB { - // _ = buf.Append([]byte{s[i]}) - // emoteB = s[i] == '[' - // continue - // } else if s[i] == ']' { - // _ = buf.Append([]byte{s[i]}) - // emoteB = false - // continue - // } - // switch s[i] { - // case '\\': - // _ = buf.Append([]byte("_1")) - // case '/': - // _ = buf.Append([]byte("_2")) - // case '*': - // _ = buf.Append([]byte("_3")) - // case '<': - // _ = buf.Append([]byte("_4")) - // case '>': - // _ = buf.Append([]byte("_5")) - // case '|': - // _ = buf.Append([]byte("_6")) - // case ':': - // _ = buf.Append([]byte(":")) - // case '?': - // _ = buf.Append([]byte("?")) - // case '"': - // _ = buf.Append([]byte("“")) - // default: - // _ = buf.Append([]byte{s[i]}) - // } - // } - // return string(buf.GetCopyBuf()) +func (t *danmuEmotes) Hashr(s string) (r string) { + rs := phash.Md5String(s) + rr := []byte{} + layer := t.LayerN + if layer > len(rs)-1 { + layer = layer - 1 + } + for i := 0; i < len(rs); i++ { + rr = append(rr, rs[i]) + if layer > 0 { + rr = append(rr, '/') + layer -= 1 + } + } + return string(rr) } diff --git a/Reply/Reply.go b/Reply/Reply.go index 1154b96..7472d6a 100644 --- a/Reply/Reply.go +++ b/Reply/Reply.go @@ -15,7 +15,6 @@ import ( c "github.com/qydysky/bili_danmu/CV" F "github.com/qydysky/bili_danmu/F" replyFunc "github.com/qydysky/bili_danmu/Reply/F" - danmuemotes "github.com/qydysky/bili_danmu/Reply/F/danmuEmotes" "github.com/qydysky/bili_danmu/Reply/F/danmuReLiveTriger" "github.com/qydysky/bili_danmu/Reply/F/liveOver" "github.com/qydysky/bili_danmu/Reply/F/recStartEnd" @@ -1290,7 +1289,7 @@ func (t replyF) danmu(s string) { item.color = "#" + fmt.Sprintf("%x", F.Itob32(int32(i[3].(float64)))[1:]) if v, ok := t.Common.K_v.LoadV(`弹幕表情`).(bool); ok && v { - if _, e := danmuemotes.SaveEmote.Run(context.Background(), danmuemotes.Danmu{Logg: msglog, Info: i, Msg: &item.msg}); e != nil { + if _, e := replyFunc.DanmuEmotes.SaveEmote(context.Background(), replyFunc.DanmuEmotesS{Logg: msglog, Info: i, Msg: &item.msg}); e != nil { msglog.L(`E: `, e) } }