diff --git a/cmd/bot.go b/cmd/bot.go index e668717..aae7626 100644 --- a/cmd/bot.go +++ b/cmd/bot.go @@ -181,7 +181,7 @@ func (c *BotCommand) Execute(ctx context.Context, f *flag.FlagSet, args ...inter scheduler := gocron.NewScheduler(time.FixedZone("JST", 9*60*60)) // scheduler.SetMaxConcurrentJobs(10, gocron.RescheduleMode) - usecase, err := dtv.NewDTVUsecase(config, asynqClient, asynqInspector, discordClient, mirakcClient, scheduler, queries, logger) + usecase, err := dtv.NewDTVUsecase(config, asynqClient, asynqInspector, discordClient, mirakcClient, scheduler, queries, logger, config.Match.KanaMatch) if err != nil { logger.Error("can't create DTVUsecase", zap.Error(err)) } diff --git a/config/config.go b/config/config.go index fabb493..3a05f49 100644 --- a/config/config.go +++ b/config/config.go @@ -31,4 +31,7 @@ type Config struct { OutputPathTemplate string `required:"true" env:"ENCODING_OUTPUT_PATH_TEMPLATE"` EncodeCommandTemplate string `required:"true" env:"ENCODING_COMMAND"` } + Match struct { + KanaMatch bool `default:"true" env:"KANA_MATCH"` + } } diff --git a/docker-compose/config.yml.example b/docker-compose/config.yml.example index 77fa97f..1c59dc3 100644 --- a/docker-compose/config.yml.example +++ b/docker-compose/config.yml.example @@ -21,3 +21,5 @@ encoding: basepath: "/encoded" outputpathtemplate: "{{.Program.Name | fold}}-{{.Program.StartTime.Format \"20060102-1504\"}}-{{.Service.Name | fold}}.mp4" encodecommandtemplate: "ffmpeg -i {{.InputPath}} {{.OutputPath}} -y" +match: + kanamatch: true diff --git a/dtv/auto_search.go b/dtv/auto_search.go index 10366b9..b8c3060 100644 --- a/dtv/auto_search.go +++ b/dtv/auto_search.go @@ -4,6 +4,7 @@ import ( "strings" "github.com/bwmarrin/discordgo" + "github.com/ikawaha/kagome/tokenizer" "github.com/kounoike/dtv-discord-go/db" "github.com/kounoike/dtv-discord-go/discord" "go.uber.org/zap" @@ -25,15 +26,31 @@ type AutoSearchProgram struct { Genre string } -func NewAutoSearchProgram(p db.Program) *AutoSearchProgram { +func NewAutoSearchProgram(p db.Program, kanaMatch bool) *AutoSearchProgram { return &AutoSearchProgram{ - Title: normalizeString(p.Name), - Genre: normalizeString(p.Genre), + Title: normalizeString(p.Name, kanaMatch), + Genre: normalizeString(p.Genre, kanaMatch), } } -func normalizeString(str string) string { - return strings.ToLower(width.Fold.String(str)) +func normalizeString(str string, kanaMatch bool) string { + normalized := strings.ToLower(width.Fold.String(str)) + if kanaMatch { + retKana := "" + t := tokenizer.New() + tokens := t.Tokenize(normalized) + normalizedRune := []rune(normalized) + for _, token := range tokens { + if len(token.Features()) > 7 { + retKana += token.Features()[7] + } else { + retKana += string(normalizedRune[token.Start:token.End]) + } + } + return retKana + } else { + return normalized + } } func (a *AutoSearch) IsMatchProgram(program *AutoSearchProgram) bool { @@ -46,8 +63,8 @@ func (a *AutoSearch) IsMatchProgram(program *AutoSearchProgram) bool { return true } -func (a *AutoSearch) IsMatchService(serviceName string) bool { - if a.Channel == "" || strings.Contains(normalizeString(serviceName), normalizeString(a.Channel)) { +func (a *AutoSearch) IsMatchService(serviceName string, kanaMatch bool) bool { + if a.Channel == "" || strings.Contains(normalizeString(serviceName, kanaMatch), normalizeString(a.Channel, kanaMatch)) { return true } else { return false @@ -61,9 +78,9 @@ func (dtv *DTVUsecase) getAutoSeachFromMessage(msg *discordgo.Message) (*AutoSea return nil, err } autoSearch := AutoSearch{} - autoSearch.Channel = normalizeString(iniContent.Section("").Key("チャンネル").String()) - autoSearch.Genre = normalizeString(iniContent.Section("").Key("ジャンル").String()) - autoSearch.Title = normalizeString(iniContent.Section("").Key("タイトル").String()) + autoSearch.Channel = normalizeString(iniContent.Section("").Key("チャンネル").String(), dtv.kanaMatch) + autoSearch.Genre = normalizeString(iniContent.Section("").Key("ジャンル").String(), dtv.kanaMatch) + autoSearch.Title = normalizeString(iniContent.Section("").Key("タイトル").String(), dtv.kanaMatch) notifyUsers, err := dtv.discord.GetMessageReactions(msg.ChannelID, msg.ID, discord.NotifyReactionEmoji) if err != nil { @@ -88,7 +105,7 @@ func (dtv *DTVUsecase) ListAutoSearchForServiceName(serviceName string) ([]*Auto if err != nil { return nil, err } - serviceNameNormalized := normalizeString(serviceName) + serviceNameNormalized := normalizeString(serviceName, dtv.kanaMatch) autoSearchList := make([]*AutoSearch, 0) @@ -98,8 +115,8 @@ func (dtv *DTVUsecase) ListAutoSearchForServiceName(serviceName string) ([]*Auto dtv.logger.Warn("thread message yaml unmarshal error", zap.Error(err)) continue } - if autoSearch.Channel == "" || strings.Contains(serviceNameNormalized, normalizeString(autoSearch.Channel)) { - autoSearch.Title = normalizeString(autoSearch.Title) + if autoSearch.Channel == "" || strings.Contains(serviceNameNormalized, normalizeString(autoSearch.Channel, dtv.kanaMatch)) { + autoSearch.Title = normalizeString(autoSearch.Title, dtv.kanaMatch) autoSearchList = append(autoSearchList, autoSearch) } } diff --git a/dtv/dtv_usecase.go b/dtv/dtv_usecase.go index 6403c2a..69eef75 100644 --- a/dtv/dtv_usecase.go +++ b/dtv/dtv_usecase.go @@ -25,13 +25,14 @@ type DTVUsecase struct { contentPathTmpl *template.Template outputPathTmpl *template.Template autoSearchChannel *discordgo.Channel + kanaMatch bool } func fold(str string) string { return width.Fold.String(str) } -func NewDTVUsecase(cfg config.Config, asynqClient *asynq.Client, inspector *asynq.Inspector, discordClient *discord_client.DiscordClient, mirakcClient *mirakc_client.MirakcClient, scheduler *gocron.Scheduler, queries *db.Queries, logger *zap.Logger) (*DTVUsecase, error) { +func NewDTVUsecase(cfg config.Config, asynqClient *asynq.Client, inspector *asynq.Inspector, discordClient *discord_client.DiscordClient, mirakcClient *mirakc_client.MirakcClient, scheduler *gocron.Scheduler, queries *db.Queries, logger *zap.Logger, kanaMatch bool) (*DTVUsecase, error) { funcMap := map[string]interface{}{ "fold": fold, } @@ -53,5 +54,6 @@ func NewDTVUsecase(cfg config.Config, asynqClient *asynq.Client, inspector *asyn logger: logger, contentPathTmpl: contentTmpl, outputPathTmpl: outputTmpl, + kanaMatch: kanaMatch, }, nil } diff --git a/dtv/on_ok_emoji_add.go b/dtv/on_ok_emoji_add.go index 64bd1d3..a6adff2 100644 --- a/dtv/on_ok_emoji_add.go +++ b/dtv/on_ok_emoji_add.go @@ -52,14 +52,14 @@ func (dtv *DTVUsecase) OnOkEmojiAdd(ctx context.Context, reaction *discordgo.Mes } for _, service := range services { - if autoSearch.IsMatchService(service.Name) { + if autoSearch.IsMatchService(service.Name, dtv.kanaMatch) { programs, err := dtv.mirakc.ListPrograms(uint(service.ID)) if err != nil { dtv.logger.Warn("ListPrograms error", zap.Error(err)) continue } for _, program := range programs { - asp := NewAutoSearchProgram(program) + asp := NewAutoSearchProgram(program, dtv.kanaMatch) if autoSearch.IsMatchProgram(asp) { // NOTE: DBに入ってるか確認する _, err := dtv.queries.GetProgram(ctx, program.ID) diff --git a/dtv/on_program_updated.go b/dtv/on_program_updated.go index 6effc2f..54ad5ca 100644 --- a/dtv/on_program_updated.go +++ b/dtv/on_program_updated.go @@ -71,7 +71,7 @@ func (dtv *DTVUsecase) onProgramsUpdated(ctx context.Context, serviceId uint) er if err != nil { return err } - asp := NewAutoSearchProgram(p) + asp := NewAutoSearchProgram(p, dtv.kanaMatch) for _, as := range autoSearchList { dtv.logger.Debug("matching", zap.String("p.Name", p.Name), zap.String("asp.Title", asp.Title), zap.String("as.Title", as.Title), zap.Bool("isMatch", as.IsMatchProgram(asp))) if as.IsMatchProgram(asp) { diff --git a/go.mod b/go.mod index 0a0b60e..0e2ef4a 100644 --- a/go.mod +++ b/go.mod @@ -33,6 +33,7 @@ require ( github.com/google/go-cmp v0.5.8 // indirect github.com/google/uuid v1.3.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect + github.com/ikawaha/kagome v1.11.2 // indirect github.com/lestrrat-go/option v1.0.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect diff --git a/go.sum b/go.sum index c6bafcc..76f3382 100644 --- a/go.sum +++ b/go.sum @@ -256,6 +256,8 @@ github.com/huandu/xstrings v1.3.3/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq github.com/huandu/xstrings v1.4.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= +github.com/ikawaha/kagome v1.11.2 h1:eCWpLqv5Euqa5JcwkaobUSy6uGM8rwwMw5Su3eRepBI= +github.com/ikawaha/kagome v1.11.2/go.mod h1:lHwhkGuuWqKWTxeQMppD0EmQAfKbc39QKx9qoWqgo+A= github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA= github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg= github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=