fix: store and scrobble with real album artist info string

This commit is contained in:
sentriz
2023-10-25 22:06:36 +01:00
parent 46575278ed
commit fe0567a995
4 changed files with 50 additions and 38 deletions

View File

@@ -9,7 +9,6 @@ import (
"net/url" "net/url"
"os" "os"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"time" "time"
@@ -293,29 +292,30 @@ type Play struct {
} }
type Album struct { type Album struct {
ID int `gorm:"primary_key"` ID int `gorm:"primary_key"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
ModifiedAt time.Time ModifiedAt time.Time
LeftPath string `gorm:"unique_index:idx_album_abs_path"` LeftPath string `gorm:"unique_index:idx_album_abs_path"`
RightPath string `gorm:"not null; unique_index:idx_album_abs_path" sql:"default: null"` RightPath string `gorm:"not null; unique_index:idx_album_abs_path" sql:"default: null"`
RightPathUDec string `sql:"default: null"` RightPathUDec string `sql:"default: null"`
Parent *Album Parent *Album
ParentID int `sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"` ParentID int `sql:"default: null; type:int REFERENCES albums(id) ON DELETE CASCADE"`
RootDir string `gorm:"unique_index:idx_album_abs_path" sql:"default: null"` RootDir string `gorm:"unique_index:idx_album_abs_path" sql:"default: null"`
Genres []*Genre `gorm:"many2many:album_genres"` Genres []*Genre `gorm:"many2many:album_genres"`
Cover string `sql:"default: null"` Cover string `sql:"default: null"`
Artists []*Artist `gorm:"many2many:album_artists"` Artists []*Artist `gorm:"many2many:album_artists"`
TagTitle string `sql:"default: null"` TagTitle string `sql:"default: null"`
TagTitleUDec string `sql:"default: null"` TagAlbumArtist string // display purposes only
TagBrainzID string `sql:"default: null"` TagTitleUDec string `sql:"default: null"`
TagYear int `sql:"default: null"` TagBrainzID string `sql:"default: null"`
Tracks []*Track TagYear int `sql:"default: null"`
ChildCount int `sql:"-"` Tracks []*Track
Duration int `sql:"-"` ChildCount int `sql:"-"`
AlbumStar *AlbumStar Duration int `sql:"-"`
AlbumRating *AlbumRating AlbumStar *AlbumStar
AverageRating float64 `sql:"default: null"` AlbumRating *AlbumRating
AverageRating float64 `sql:"default: null"`
} }
func (a *Album) SID() *specid.ID { func (a *Album) SID() *specid.ID {
@@ -333,18 +333,6 @@ func (a *Album) IndexRightPath() string {
return a.RightPath return a.RightPath
} }
func (a *Album) ArtistsStrings() []string {
artists := append([]*Artist(nil), a.Artists...)
sort.Slice(artists, func(i, j int) bool {
return artists[i].ID < artists[j].ID
})
strs := make([]string, 0, len(artists))
for _, artist := range artists {
strs = append(strs, artist.Name)
}
return strs
}
type PlayQueue struct { type PlayQueue struct {
ID int `gorm:"primary_key"` ID int `gorm:"primary_key"`
CreatedAt time.Time CreatedAt time.Time

View File

@@ -66,6 +66,7 @@ func (db *DB) Migrate(ctx MigrationContext) error {
construct(ctx, "202309070009", migrateDeleteArtistCoverField), construct(ctx, "202309070009", migrateDeleteArtistCoverField),
construct(ctx, "202309131743", migrateArtistInfo), construct(ctx, "202309131743", migrateArtistInfo),
construct(ctx, "202309161411", migratePlaylistsPaths), construct(ctx, "202309161411", migratePlaylistsPaths),
construct(ctx, "202310252205", migrateAlbumTagArtistString),
} }
return gormigrate. return gormigrate.
@@ -729,3 +730,7 @@ func backupDBPre016(tx *gorm.DB, ctx MigrationContext) error {
} }
return Dump(context.Background(), tx, fmt.Sprintf("%s.%d.bak", ctx.DBPath, time.Now().Unix())) return Dump(context.Background(), tx, fmt.Sprintf("%s.%d.bak", ctx.DBPath, time.Now().Unix()))
} }
func migrateAlbumTagArtistString(tx *gorm.DB, _ MigrationContext) error {
return tx.AutoMigrate(Album{}).Error
}

View File

@@ -372,6 +372,7 @@ func populateAlbum(tx *db.DB, album *db.Album, trags tagcommon.Info, modTime tim
albumName := tagcommon.MustAlbum(trags) albumName := tagcommon.MustAlbum(trags)
album.TagTitle = albumName album.TagTitle = albumName
album.TagTitleUDec = decoded(albumName) album.TagTitleUDec = decoded(albumName)
album.TagAlbumArtist = tagcommon.MustAlbumArtist(trags)
album.TagBrainzID = trags.AlbumBrainzID() album.TagBrainzID = trags.AlbumBrainzID()
album.TagYear = trags.Year() album.TagYear = trags.Year()
@@ -381,7 +382,6 @@ func populateAlbum(tx *db.DB, album *db.Album, trags tagcommon.Info, modTime tim
if err := tx.Save(&album).Error; err != nil { if err := tx.Save(&album).Error; err != nil {
return fmt.Errorf("saving album: %w", err) return fmt.Errorf("saving album: %w", err)
} }
return nil return nil
} }

View File

@@ -7,6 +7,7 @@ import (
"math" "math"
"net/http" "net/http"
"path/filepath" "path/filepath"
"sort"
"strings" "strings"
"sync" "sync"
"time" "time"
@@ -71,7 +72,7 @@ func (c *Controller) ServeScrobble(r *http.Request) *spec.Response {
scrobbleTrack.Track = track.TagTitle scrobbleTrack.Track = track.TagTitle
scrobbleTrack.Artist = track.TagTrackArtist scrobbleTrack.Artist = track.TagTrackArtist
scrobbleTrack.Album = track.Album.TagTitle scrobbleTrack.Album = track.Album.TagTitle
scrobbleTrack.AlbumArtist = strings.Join(track.Album.ArtistsStrings(), ", ") scrobbleTrack.AlbumArtist = albumArtistString(track.Album)
scrobbleTrack.TrackNumber = uint(track.TagTrackNumber) scrobbleTrack.TrackNumber = uint(track.TagTrackNumber)
scrobbleTrack.Duration = time.Second * time.Duration(track.Length) scrobbleTrack.Duration = time.Second * time.Duration(track.Length)
if _, err := uuid.Parse(track.TagBrainzID); err == nil { if _, err := uuid.Parse(track.TagBrainzID); err == nil {
@@ -513,3 +514,21 @@ func lowerUDecOrHash(in string) string {
} }
return string(lower) return string(lower)
} }
// not everyone's records may have the album.TagAlbumArtist column set, since we didn't always store that.
// try it first, but fallback to a list from the artists table
func albumArtistString(album *db.Album) string {
if album.TagAlbumArtist != "" {
return album.TagAlbumArtist
}
artists := append([]*db.Artist(nil), album.Artists...)
sort.Slice(artists, func(i, j int) bool {
return artists[i].ID < artists[j].ID
})
names := make([]string, 0, len(artists))
for _, artist := range artists {
names = append(names, artist.Name)
}
return strings.Join(names, ", ")
}