🎧 Your Personal Streaming Service
Go airsonicmadsonicmedia-servermusicmusic-servernavidromeopensubsonicraspberry-piself-hostedstreamerstreaming-apistreaming-audiosubsonicsubsonic-server
Use MBID with most occurrences
Deluan173dd52fe19f
diff --git a/persistence/album_repository.go b/persistence/album_repository.goindex bc1bd232c..8c758ef07 100644--- a/persistence/album_repository.go+++ b/persistence/album_repository.go@@ -161,8 +161,8 @@ func (r *albumRepository) refresh(ids ...string) error {sel := Select(`f.album_id as id, f.album as name, f.artist, f.album_artist, f.artist_id, f.album_artist_id,f.sort_album_name, f.sort_artist_name, f.sort_album_artist_name,f.order_album_name, f.order_album_artist_name, f.path,- f.mbz_album_id, f.mbz_album_artist_id, f.mbz_album_type, f.mbz_album_comment, f.catalog_num,- f.compilation, f.genre, max(f.year) as max_year, sum(f.duration) as duration,+ group_concat(f.mbz_album_id, ' ') as mbz_album_id, f.mbz_album_artist_id, f.mbz_album_type, f.mbz_album_comment,+ f.catalog_num, f.compilation, f.genre, max(f.year) as max_year, sum(f.duration) as duration,count(f.id) as song_count, a.id as current_id,group_concat(f.disc_subtitle, ' ') as disc_subtitles,group_concat(f.artist, ' ') as song_artists, group_concat(f.year, ' ') as years,@@ -211,6 +211,7 @@ func (r *albumRepository) refresh(ids ...string) error {al.AlbumArtistID = al.ArtistID}al.MinYear = getMinYear(al.Years)+ al.MbzAlbumID = getMbzId(r.ctx, al.MbzAlbumID, r.tableName, al.Name)al.UpdatedAt = time.Now()if al.CurrentId != "" {toUpdate++diff --git a/persistence/artist_repository.go b/persistence/artist_repository.goindex fe4033a5e..b39e50925 100644--- a/persistence/artist_repository.go+++ b/persistence/artist_repository.go@@ -144,7 +144,7 @@ func (r *artistRepository) refresh(ids ...string) error {}var artists []refreshArtistsel := Select("f.album_artist_id as id", "f.album_artist as name", "count(*) as album_count", "a.id as current_id",- "f.mbz_album_artist_id as mbz_artist_id",+ "group_concat(f.mbz_album_artist_id , ' ') as mbz_artist_id","f.sort_album_artist_name as sort_artist_name", "f.order_album_artist_name as order_artist_name","sum(f.song_count) as song_count", "sum(f.size) as size").From("album f").@@ -164,6 +164,7 @@ func (r *artistRepository) refresh(ids ...string) error {} else {toInsert++}+ ar.MbzArtistID = getMbzId(r.ctx, ar.MbzArtistID, r.tableName, ar.Name)err := r.Put(&ar.Artist)if err != nil {return errdiff --git a/persistence/helpers.go b/persistence/helpers.goindex 6bec5e989..f281a5367 100644--- a/persistence/helpers.go+++ b/persistence/helpers.go@@ -1,12 +1,15 @@package persistenceimport (+ "context""encoding/json""fmt""regexp""strings""github.com/Masterminds/squirrel"+ "github.com/deluan/navidrome/consts"+ "github.com/deluan/navidrome/log""github.com/deluan/navidrome/model""github.com/deluan/navidrome/utils")@@ -55,3 +58,32 @@ func (e existsCond) ToSql() (string, []interface{}, error) {sql = fmt.Sprintf("exists (select 1 from %s where %s)", e.subTable, sql)return sql, args, err}++func getMbzId(ctx context.Context, mbzIDS, entityName, name string) string {+ ids := strings.Fields(mbzIDS)+ if len(ids) == 0 {+ return ""+ }+ idCounts := map[string]int{}+ for _, id := range ids {+ if c, ok := idCounts[id]; ok {+ idCounts[id] = c + 1+ } else {+ idCounts[id] = 1+ }+ }++ var topKey string+ var topCount int+ for k, v := range idCounts {+ if v > topCount {+ topKey = k+ topCount = v+ }+ }++ if len(idCounts) > 1 && name != consts.VariousArtists {+ log.Warn(ctx, "Multiple MBIDs found for "+entityName, "name", name, "mbids", idCounts)+ }+ return topKey+}diff --git a/persistence/helpers_test.go b/persistence/helpers_test.goindex 4c4a10f0c..4a9f05a46 100644--- a/persistence/helpers_test.go+++ b/persistence/helpers_test.go@@ -1,6 +1,7 @@package persistenceimport (+ "context""time""github.com/Masterminds/squirrel"@@ -61,4 +62,16 @@ var _ = Describe("Helpers", func() {Expect(err).To(BeNil())})})++ Describe("getMbzId", func() {+ It(`returns "" when no ids are passed`, func() {+ Expect(getMbzId(context.TODO(), " ", "", "")).To(Equal(""))+ })+ It(`returns the only id passed`, func() {+ Expect(getMbzId(context.TODO(), "1234 ", "", "")).To(Equal("1234"))+ })+ It(`returns the id with higher frequency`, func() {+ Expect(getMbzId(context.TODO(), "1 2 3 4 1", "", "")).To(Equal("1"))+ })+ })})