From 8f6610ff860ad18d56545b903d9edb6a1254ddec Mon Sep 17 00:00:00 2001 From: sentriz Date: Fri, 8 Sep 2023 19:58:47 +0100 Subject: [PATCH] feat: allow multi valued tag modes to be configurable --- README.md | 59 ++++++++++++++++++++------------- cmd/gonic/gonic.go | 57 ++++++++++++++++++++++++++++---- contrib/config | 3 -- mockfs/mockfs.go | 8 ++++- scanner/scanner.go | 78 ++++++++++++++++++++++++++++++++------------ scanner/tags/tags.go | 24 ++++++++++---- server/server.go | 24 +++++++------- 7 files changed, 180 insertions(+), 73 deletions(-) diff --git a/README.md b/README.md index 828cce4..9e2f20e 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@

irc #gonic on libera.chat -  |  +  |  matrix #gonic:libera.chat

@@ -25,7 +25,7 @@ - [last.fm](https://www.last.fm/) scrobbling - [listenbrainz](https://listenbrainz.org/) scrobbling (thank you [spezifisch](https://github.com/spezifisch), [lxea](https://github.com/lxea)) - artist similarities and biographies from the last.fm api -- multiple genre support (see `GONIC_GENRE_SPLIT` to split tag strings on a character, eg. `;`, and browse them individually) +- support for multi valued tags like albumartists and genres ([see more](#multi-valued-tags) - a web interface for configuration (set up last.fm, manage users, start scans, etc.) - support for the [album-artist](https://mkoby.com/2007/02/18/artist-versus-album-artist/) tag, to not clutter your artist list with compilation album appearances - written in [go](https://golang.org/), so lightweight and suitable for a raspberry pi, etc. (see ARM images below) @@ -57,26 +57,41 @@ password can then be changed from the web interface ## configuration options -| env var | command line arg | description | -| ------------------------------ | ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `GONIC_MUSIC_PATH` | `-music-path` | path to your music collection (see also multi-folder support below) | -| `GONIC_PODCAST_PATH` | `-podcast-path` | path to a podcasts directory | -| `GONIC_PLAYLISTS_PATH` | `-playlists-path` | path to new or existing directory with m3u files for subsonic playlists. items in the directory should be in the format `/.m3u`. for example the admin user could have `1/my-playlist.m3u`. gonic create and make changes to these playlists over the subsonic api. | -| `GONIC_CACHE_PATH` | `-cache-path` | path to store audio transcodes, covers, etc | -| `GONIC_DB_PATH` | `-db-path` | **optional** path to database file | -| `GONIC_HTTP_LOG` | `-http-log` | **optional** http request logging, enabled by default | -| `GONIC_LISTEN_ADDR` | `-listen-addr` | **optional** host and port to listen on (eg. `0.0.0.0:4747`, `127.0.0.1:4747`) (_default_ `0.0.0.0:4747`) | -| `GONIC_TLS_CERT` | `-tls-cert` | **optional** path to a TLS cert (enables HTTPS listening) | -| `GONIC_TLS_KEY` | `-tls-key` | **optional** path to a TLS key (enables HTTPS listening) | -| `GONIC_PROXY_PREFIX` | `-proxy-prefix` | **optional** url path prefix to use if behind reverse proxy. eg `/gonic` (see example configs below) | -| `GONIC_SCAN_INTERVAL` | `-scan-interval` | **optional** interval (in minutes) to check for new music (automatic scanning disabled if omitted) | -| `GONIC_SCAN_AT_START_ENABLED` | `-scan-at-start-enabled` | **optional** whether to perform an initial scan at startup | -| `GONIC_SCAN_WATCHER_ENABLED` | `-scan-watcher-enabled` | **optional** whether to watch file system for new music and rescan | -| `GONIC_JUKEBOX_ENABLED` | `-jukebox-enabled` | **optional** whether the subsonic [jukebox api](https://airsonic.github.io/docs/jukebox/) should be enabled | -| `GONIC_JUKEBOX_MPV_EXTRA_ARGS` | `-jukebox-mpv-extra-args` | **optional** extra command line arguments to pass to the jukebox mpv daemon | -| `GONIC_PODCAST_PURGE_AGE` | `-podcast-purge-age` | **optional** age (in days) to purge podcast episodes if not accessed | -| `GONIC_GENRE_SPLIT` | `-genre-split` | **optional** a string or character to split genre tags on for multi-genre support (eg. `;`) | -| `GONIC_EXCLUDE_PATTERN` | `-exclude-pattern` | **optional** files matching this regex pattern will not be imported | +| env var | command line arg | description | +| -------------------------------- | --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `GONIC_MUSIC_PATH` | `-music-path` | path to your music collection (see also multi-folder support below) | +| `GONIC_PODCAST_PATH` | `-podcast-path` | path to a podcasts directory | +| `GONIC_PLAYLISTS_PATH` | `-playlists-path` | path to new or existing directory with m3u files for subsonic playlists. items in the directory should be in the format `/.m3u`. for example the admin user could have `1/my-playlist.m3u`. gonic create and make changes to these playlists over the subsonic api. | +| `GONIC_CACHE_PATH` | `-cache-path` | path to store audio transcodes, covers, etc | +| `GONIC_DB_PATH` | `-db-path` | **optional** path to database file | +| `GONIC_HTTP_LOG` | `-http-log` | **optional** http request logging, enabled by default | +| `GONIC_LISTEN_ADDR` | `-listen-addr` | **optional** host and port to listen on (eg. `0.0.0.0:4747`, `127.0.0.1:4747`) (_default_ `0.0.0.0:4747`) | +| `GONIC_TLS_CERT` | `-tls-cert` | **optional** path to a TLS cert (enables HTTPS listening) | +| `GONIC_TLS_KEY` | `-tls-key` | **optional** path to a TLS key (enables HTTPS listening) | +| `GONIC_PROXY_PREFIX` | `-proxy-prefix` | **optional** url path prefix to use if behind reverse proxy. eg `/gonic` (see example configs below) | +| `GONIC_SCAN_INTERVAL` | `-scan-interval` | **optional** interval (in minutes) to check for new music (automatic scanning disabled if omitted) | +| `GONIC_SCAN_AT_START_ENABLED` | `-scan-at-start-enabled` | **optional** whether to perform an initial scan at startup | +| `GONIC_SCAN_WATCHER_ENABLED` | `-scan-watcher-enabled` | **optional** whether to watch file system for new music and rescan | +| `GONIC_JUKEBOX_ENABLED` | `-jukebox-enabled` | **optional** whether the subsonic [jukebox api](https://airsonic.github.io/docs/jukebox/) should be enabled | +| `GONIC_JUKEBOX_MPV_EXTRA_ARGS` | `-jukebox-mpv-extra-args` | **optional** extra command line arguments to pass to the jukebox mpv daemon | +| `GONIC_PODCAST_PURGE_AGE` | `-podcast-purge-age` | **optional** age (in days) to purge podcast episodes if not accessed | +| `GONIC_EXCLUDE_PATTERN` | `-exclude-pattern` | **optional** files matching this regex pattern will not be imported | +| `GONIC_MULTI_VALUE_GENRE` | `-multi-value-genre` | **optional** setting for multi-valued genre tags when scanning ([see more](#multi-valued-tags)) | +| `GONIC_MULTI_VALUE_ALBUM_ARTIST` | `-multi-value-album-artist` | **optional** setting for multi-valued album artist tags when scanning ([see more](#multi-valued-tags)) | + +## multi valued tags + +gonic can support potentially multi valued tags like `genres` and `albumartists`. in both cases gonic will individual entries in its database for each. + +this means being able to click find album "X" under both "techno" and "house" for example. or finding the album "My Life in the Bush of Ghosts" under either "David Byrne" or "Brian Eno". it also means not cluttering up your artists list with "A & X", "A and Y", "A ft. Z", etc. you will only have A, X, Y, and Z. + +the available modes are: + +| value | desc | +| ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `multi` | gonic will explictly look for multi value fields in your audio metadata. software like musicbrainz picard or beets can set set these ([soon](https://github.com/beetbox/beets/pull/4743)) | +| `delim ` | gonic will look at your normal audio metadata fields like "genre" or "album_artist", but split them on a delimiter. for example you could set `-multi-value-genre "delim ;"` to split the single genre field on ";" | +| `none` (default) | gonic will not attempt to do any multi value processing | ## screenshots diff --git a/cmd/gonic/gonic.go b/cmd/gonic/gonic.go index 18dcb47..d156a8d 100644 --- a/cmd/gonic/gonic.go +++ b/cmd/gonic/gonic.go @@ -22,6 +22,7 @@ import ( "go.senan.xyz/gonic" "go.senan.xyz/gonic/db" + "go.senan.xyz/gonic/scanner" "go.senan.xyz/gonic/server" "go.senan.xyz/gonic/server/ctrlsubsonic" ) @@ -59,13 +60,17 @@ func main() { confProxyPrefix := set.String("proxy-prefix", "", "url path prefix to use if behind proxy. eg '/gonic' (optional)") confHTTPLog := set.Bool("http-log", true, "http request logging (optional)") - confGenreSplit := set.String("genre-split", "\n", "character or string to split genre tag data on (optional)") - confShowVersion := set.Bool("version", false, "show gonic version") _ = set.String("config-path", "", "path to config (optional)") confExcludePatterns := set.String("exclude-pattern", "", "regex pattern to exclude files from scan (optional)") + var confMultiValueGenre, confMultiValueAlbumArtist multiValueSetting + set.Var(&confMultiValueGenre, "multi-value-genre", "setting for mutli-valued genre scanning (optional)") + set.Var(&confMultiValueAlbumArtist, "multi-value-album-artist", "setting for mutli-valued album artist scanning (optional)") + + deprecatedConfGenreSplit := set.String("genre-split", "", "(deprecated, see multi-value settings)") + if _, err := regexp.Compile(*confExcludePatterns); err != nil { log.Fatalf("invalid exclude pattern: %v\n", err) } @@ -135,6 +140,12 @@ func main() { proxyPrefixExpr := regexp.MustCompile(`^\/*(.*?)\/*$`) *confProxyPrefix = proxyPrefixExpr.ReplaceAllString(*confProxyPrefix, `/$1`) + + if *deprecatedConfGenreSplit != "" && *deprecatedConfGenreSplit != "\n" { + confMultiValueGenre = multiValueSetting{Mode: scanner.Delim, Delim: *deprecatedConfGenreSplit} + *deprecatedConfGenreSplit = "" + } + server, err := server.New(server.Options{ DB: dbc, MusicPaths: musicPaths, @@ -144,7 +155,10 @@ func main() { PodcastPath: *confPodcastPath, PlaylistsPath: *confPlaylistsPath, ProxyPrefix: *confProxyPrefix, - GenreSplit: *confGenreSplit, + MultiValueSettings: map[scanner.Tag]scanner.MultiValueSetting{ + scanner.Genre: scanner.MultiValueSetting(confMultiValueGenre), + scanner.AlbumArtist: scanner.MultiValueSetting(confMultiValueAlbumArtist), + }, HTTPLog: *confHTTPLog, JukeboxEnabled: *confJukeboxEnabled, }) @@ -212,14 +226,12 @@ func (pa *pathAliases) Set(value string) error { return nil } -var errNotExists = errors.New("path does not exist, please provide one") - func validatePath(p string) (string, error) { if p == "" { - return "", errNotExists + return "", errors.New("path can't be empty") } if _, err := os.Stat(p); os.IsNotExist(err) { - return "", errNotExists + return "", errors.New("path does not exist, please provide one") } p, err := filepath.Abs(p) if err != nil { @@ -227,3 +239,34 @@ func validatePath(p string) (string, error) { } return p, nil } + +type multiValueSetting scanner.MultiValueSetting + +func (mvs multiValueSetting) String() string { + switch mvs.Mode { + case scanner.Delim: + return fmt.Sprintf("delim(%s)", mvs.Delim) + case scanner.Multi: + return fmt.Sprint("multi", mvs.Delim) + default: + return "none" + } +} + +func (mvs *multiValueSetting) Set(value string) error { + mode, delim, _ := strings.Cut(value, " ") + switch mode { + case "delim": + if delim == "" { + return fmt.Errorf("no delimiter provided for delimiter mode") + } + mvs.Mode = scanner.Delim + mvs.Delim = delim + case "multi": + mvs.Mode = scanner.Multi + case "none": + default: + return fmt.Errorf(`unknown multi value mode %q. should be "none" | "multi" | "delim "`, mode) + } + return nil +} diff --git a/contrib/config b/contrib/config index f5f448b..29528af 100644 --- a/contrib/config +++ b/contrib/config @@ -47,6 +47,3 @@ cache-path /var/cache/gonic #scan-watcher-enabled false #jukebox-enabled false #jukebox-mpv-extra-args - -# A string or character to split genre tags on for multi-genre support (e.g. ;) -#genre-split ; diff --git a/mockfs/mockfs.go b/mockfs/mockfs.go index 9cb0e86..8221604 100644 --- a/mockfs/mockfs.go +++ b/mockfs/mockfs.go @@ -61,8 +61,13 @@ func newMockFS(t testing.TB, dirs []string, excludePattern string) *MockFS { } } + multiValueSettings := map[scanner.Tag]scanner.MultiValueSetting{ + scanner.Genre: {Mode: scanner.Delim, Delim: ";"}, + scanner.AlbumArtist: {Mode: scanner.Multi}, + } + tagReader := &tagReader{paths: map[string]*tagReaderResult{}} - scanner := scanner.New(absDirs, dbc, ";", tagReader, excludePattern) + scanner := scanner.New(absDirs, dbc, multiValueSettings, tagReader, excludePattern) return &MockFS{ t: t, @@ -383,6 +388,7 @@ func (m *Tags) AlbumArtist() string { return m.RawAlbumArtist } func (m *Tags) AlbumArtists() []string { return m.RawAlbumArtists } func (m *Tags) AlbumBrainzID() string { return "" } func (m *Tags) Genre() string { return m.RawGenre } +func (m *Tags) Genres() []string { return []string{m.RawGenre} } func (m *Tags) TrackNumber() int { return 1 } func (m *Tags) DiscNumber() int { return 1 } func (m *Tags) Year() int { return 2021 } diff --git a/scanner/scanner.go b/scanner/scanner.go index ede0d64..ff379f4 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -30,32 +30,32 @@ var ( ) type Scanner struct { - db *db.DB - musicDirs []string - genreSplit string - tagger tags.Reader - excludePattern *regexp.Regexp - scanning *int32 - watcher *fsnotify.Watcher - watchMap map[string]string // maps watched dirs back to root music dir - watchDone chan bool + db *db.DB + musicDirs []string + multiValueSettings map[Tag]MultiValueSetting + tagger tags.Reader + excludePattern *regexp.Regexp + scanning *int32 + watcher *fsnotify.Watcher + watchMap map[string]string // maps watched dirs back to root music dir + watchDone chan bool } -func New(musicDirs []string, db *db.DB, genreSplit string, tagger tags.Reader, excludePattern string) *Scanner { +func New(musicDirs []string, db *db.DB, multiValueSettings map[Tag]MultiValueSetting, tagger tags.Reader, excludePattern string) *Scanner { var excludePatternRegExp *regexp.Regexp if excludePattern != "" { excludePatternRegExp = regexp.MustCompile(excludePattern) } return &Scanner{ - db: db, - musicDirs: musicDirs, - genreSplit: genreSplit, - tagger: tagger, - excludePattern: excludePatternRegExp, - scanning: new(int32), - watchMap: make(map[string]string), - watchDone: make(chan bool), + db: db, + musicDirs: musicDirs, + multiValueSettings: multiValueSettings, + tagger: tagger, + excludePattern: excludePatternRegExp, + scanning: new(int32), + watchMap: make(map[string]string), + watchDone: make(chan bool), } } @@ -353,7 +353,7 @@ func (s *Scanner) populateTrackAndAlbumArtists(tx *db.DB, c *Context, i int, alb return fmt.Errorf("%v: %w", err, ErrReadingTags) } - genreNames := strings.Split(tags.MustGenre(trags), s.genreSplit) + genreNames := parseMulti(trags, s.multiValueSettings[Genre], tags.MustGenres, tags.MustGenre) genreIDs, err := populateGenres(tx, genreNames) if err != nil { return fmt.Errorf("populate genres: %w", err) @@ -361,9 +361,9 @@ func (s *Scanner) populateTrackAndAlbumArtists(tx *db.DB, c *Context, i int, alb // metadata for the album table comes only from the the first track's tags if i == 0 { - albumArtists := tags.MustAlbumArtists(trags) + albumArtistNames := parseMulti(trags, s.multiValueSettings[AlbumArtist], tags.MustAlbumArtists, tags.MustAlbumArtist) var albumArtistIDs []int - for _, albumArtistName := range albumArtists { + for _, albumArtistName := range albumArtistNames { albumArtist, err := populateArtist(tx, albumArtistName) if err != nil { return fmt.Errorf("populate album artist: %w", err) @@ -669,3 +669,39 @@ func (c *Context) TracksMissing() int { return len(c.tracksMissing) } func (c *Context) AlbumsMissing() int { return len(c.albumsMissing) } func (c *Context) ArtistsMissing() int { return c.artistsMissing } func (c *Context) GenresMissing() int { return c.genresMissing } + +type MultiValueMode uint8 + +const ( + None MultiValueMode = iota + Delim + Multi +) + +type Tag uint8 + +const ( + Genre Tag = iota + AlbumArtist +) + +type MultiValueSetting struct { + Mode MultiValueMode + Delim string +} + +func parseMulti(parser tags.Parser, setting MultiValueSetting, getMulti func(tags.Parser) []string, get func(tags.Parser) string) []string { + var parts []string + switch setting.Mode { + case Multi: + parts = getMulti(parser) + case Delim: + parts = strings.Split(get(parser), setting.Delim) + default: + parts = []string{get(parser)} + } + for i := range parts { + parts[i] = strings.TrimSpace(parts[i]) + } + return parts +} diff --git a/scanner/tags/tags.go b/scanner/tags/tags.go index 4315531..235242c 100644 --- a/scanner/tags/tags.go +++ b/scanner/tags/tags.go @@ -29,6 +29,7 @@ func (t *Tagger) AlbumArtist() string { return first(find(t.raw, "albumartist func (t *Tagger) AlbumArtists() []string { return find(t.raw, "albumartists", "album_artists") } func (t *Tagger) AlbumBrainzID() string { return first(find(t.raw, "musicbrainz_albumid")) } // musicbrainz release ID func (t *Tagger) Genre() string { return first(find(t.raw, "genre")) } +func (t *Tagger) Genres() []string { return find(t.raw, "genres") } func (t *Tagger) TrackNumber() int { return intSep("/" /* eg. 5/12 */, first(find(t.raw, "tracknumber"))) @@ -56,6 +57,7 @@ type Parser interface { AlbumArtists() []string AlbumBrainzID() string Genre() string + Genres() []string TrackNumber() int DiscNumber() int Length() int @@ -127,17 +129,18 @@ func MustArtist(p Parser) string { return "Unknown Artist" } +func MustAlbumArtist(p Parser) string { + if r := p.AlbumArtist(); r != "" { + return r + } + return MustArtist(p) +} + func MustAlbumArtists(p Parser) []string { if r := p.AlbumArtists(); len(r) > 0 { return r } - if r := p.AlbumArtist(); r != "" { - return []string{r} - } - if r := p.Artist(); r != "" { - return []string{r} - } - return []string{"Unknown Artist"} + return []string{MustAlbumArtist(p)} } func MustGenre(p Parser) string { @@ -146,3 +149,10 @@ func MustGenre(p Parser) string { } return "Unknown Genre" } + +func MustGenres(p Parser) []string { + if r := p.Genres(); len(r) > 0 { + return r + } + return []string{MustGenre(p)} +} diff --git a/server/server.go b/server/server.go index 56d8b39..0e3790c 100644 --- a/server/server.go +++ b/server/server.go @@ -30,17 +30,17 @@ import ( ) type Options struct { - DB *db.DB - MusicPaths []ctrlsubsonic.MusicPath - ExcludePattern string - PodcastPath string - CacheAudioPath string - CoverCachePath string - PlaylistsPath string - ProxyPrefix string - GenreSplit string - HTTPLog bool - JukeboxEnabled bool + DB *db.DB + MusicPaths []ctrlsubsonic.MusicPath + ExcludePattern string + PodcastPath string + CacheAudioPath string + CoverCachePath string + PlaylistsPath string + ProxyPrefix string + MultiValueSettings map[scanner.Tag]scanner.MultiValueSetting + HTTPLog bool + JukeboxEnabled bool } type Server struct { @@ -54,7 +54,7 @@ type Server struct { func New(opts Options) (*Server, error) { tagger := &tags.TagReader{} - scanner := scanner.New(ctrlsubsonic.PathsOf(opts.MusicPaths), opts.DB, opts.GenreSplit, tagger, opts.ExcludePattern) + scanner := scanner.New(ctrlsubsonic.PathsOf(opts.MusicPaths), opts.DB, opts.MultiValueSettings, tagger, opts.ExcludePattern) playlistStore, err := playlist.NewStore(opts.PlaylistsPath) if err != nil {