Skip to content

Commit

Permalink
Merge pull request #59 from kounoike:feature/kana-match
Browse files Browse the repository at this point in the history
読み仮名マッチの追加
  • Loading branch information
kounoike authored Apr 1, 2023
2 parents a90db48 + 0861097 commit 4454fa1
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
}
2 changes: 2 additions & 0 deletions docker-compose/config.yml.example
Original file line number Diff line number Diff line change
Expand Up @@ -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
43 changes: 30 additions & 13 deletions dtv/auto_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 {
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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)

Expand All @@ -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)
}
}
Expand Down
4 changes: 3 additions & 1 deletion dtv/dtv_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -53,5 +54,6 @@ func NewDTVUsecase(cfg config.Config, asynqClient *asynq.Client, inspector *asyn
logger: logger,
contentPathTmpl: contentTmpl,
outputPathTmpl: outputTmpl,
kanaMatch: kanaMatch,
}, nil
}
4 changes: 2 additions & 2 deletions dtv/on_ok_emoji_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion dtv/on_program_updated.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down

0 comments on commit 4454fa1

Please sign in to comment.