From 0a8da3d0e7fdf8c364ed82fdbef1b4adc919594e Mon Sep 17 00:00:00 2001 From: Mathis Maquenne <124215603+mathismqn@users.noreply.github.com> Date: Fri, 12 Sep 2025 12:09:37 +0200 Subject: [PATCH] feat(tags): filter genres to avoid unusual tags --- internal/provider/genre.go | 70 +++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) diff --git a/internal/provider/genre.go b/internal/provider/genre.go index dd5e26d..0ef48ee 100644 --- a/internal/provider/genre.go +++ b/internal/provider/genre.go @@ -9,6 +9,19 @@ import ( "github.com/PuerkitoBio/goquery" ) +var electronicKeywords = []string{ + "Ambient", "Bass", "Big Room", "Breakbeat", "Dance", "Disco", "Downtempo", + "Drum And Bass", "Dub", "Dubstep", "EDM", "Electro", "Electronic", "Electronica", + "Eurodance", "Gabber", "Garage", "Hardcore", "Hardstyle", "House", "Industrial", + "Jungle", "Moombahton", "Synthpop", "Synthwave", "Techno", "Trance", "Trap", + "Trip Hop", "Vaporwave", +} + +var nonElectronicKeywords = []string{ + "Blues", "Chillout", "Classical", "Country", "Folk", "Funk", "Hip Hop", "Jazz", + "Latin", "Metal", "Pop", "R&B", "Rap", "Reggae", "Rock", "Soul", +} + type GenreProvider struct{} func (p GenreProvider) Fetch(ctx context.Context, httpClient *http.Client, artist, title string) (string, error) { @@ -23,11 +36,16 @@ func (p GenreProvider) Fetch(ctx context.Context, httpClient *http.Client, artis return "", fmt.Errorf("no data found") } - if len(tags) > 1 { + if len(tags) > 2 { tags = tags[:2] } - genre := p.formatTags(tags) + filteredTags := p.filterTags(tags) + if len(filteredTags) == 0 { + return "", fmt.Errorf("no data found") + } + + genre := p.formatTags(filteredTags) return genre, nil } @@ -67,6 +85,48 @@ func (p GenreProvider) parse(doc *goquery.Document) []string { return tags } +func (p GenreProvider) filterTags(tags []string) []string { + var electronicTags []string + var nonElectronicTags []string + + for _, tag := range tags { + if p.isElectronicGenre(tag) { + electronicTags = append(electronicTags, tag) + } else if p.isNonElectronicGenre(tag) { + nonElectronicTags = append(nonElectronicTags, tag) + } + } + + var filteredTags []string + filteredTags = append(filteredTags, electronicTags...) + + if len(electronicTags) > 0 { + filteredTags = append(filteredTags, nonElectronicTags...) + } + + return filteredTags +} + +func (p GenreProvider) isElectronicGenre(tag string) bool { + tagLower := strings.ToLower(tag) + for _, allowed := range electronicKeywords { + if strings.Contains(tagLower, strings.ToLower(allowed)) { + return true + } + } + return false +} + +func (p GenreProvider) isNonElectronicGenre(tag string) bool { + tagLower := strings.ToLower(tag) + for _, allowed := range nonElectronicKeywords { + if strings.Contains(tagLower, strings.ToLower(allowed)) { + return true + } + } + return false +} + func (p GenreProvider) formatTags(tags []string) string { var formatted []string for _, tag := range tags { @@ -77,10 +137,12 @@ func (p GenreProvider) formatTags(tags []string) string { words := strings.Fields(tag) for i, w := range words { - words[i] = strings.Title(w) + if len(w) > 0 { + words[i] = strings.ToUpper(string(w[0])) + strings.ToLower(w[1:]) + } } formatted = append(formatted, strings.Join(words, " ")) } - return strings.Join(formatted, "/") + return strings.Join(formatted, " / ") }