feat(tags): filter genres to avoid unusual tags

This commit is contained in:
Mathis Maquenne
2025-09-12 14:41:29 +02:00
parent 2ef1ba42a8
commit 0a8da3d0e7
+65 -3
View File
@@ -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,7 +137,9 @@ 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, " "))
}