Use MBID with most occurrences

This commit is contained in:
Deluan 2020-10-20 17:16:24 -04:00
parent 6663c079e0
commit 173dd52fe1
4 changed files with 50 additions and 3 deletions

View File

@ -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++

View File

@ -144,7 +144,7 @@ func (r *artistRepository) refresh(ids ...string) error {
}
var artists []refreshArtist
sel := 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 err

View File

@ -1,12 +1,15 @@
package persistence
import (
"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
}

View File

@ -1,6 +1,7 @@
package persistence
import (
"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"))
})
})
})