Fix performance and precision of TopSongs
This commit is contained in:
parent
a45b5a037f
commit
075c28d2e5
|
@ -137,19 +137,25 @@ func (e *externalInfo) similarArtists(ctx context.Context, artist *model.Artist,
|
||||||
return result, nil
|
return result, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *externalInfo) TopSongs(ctx context.Context, artist string, count int) (model.MediaFiles, error) {
|
func (e *externalInfo) TopSongs(ctx context.Context, artistName string, count int) (model.MediaFiles, error) {
|
||||||
if e.lfm == nil {
|
if e.lfm == nil {
|
||||||
log.Warn(ctx, "Last.FM client not configured")
|
log.Warn(ctx, "Last.FM client not configured")
|
||||||
return nil, model.ErrNotAvailable
|
return nil, model.ErrNotAvailable
|
||||||
}
|
}
|
||||||
log.Debug(ctx, "Calling Last.FM ArtistGetTopTracks", "artist", artist)
|
artist, err := e.ds.Artist(ctx).FindByName(artistName)
|
||||||
tracks, err := e.lfm.ArtistGetTopTracks(ctx, artist, count)
|
if err != nil {
|
||||||
|
log.Error(ctx, "Artist not found", "name", artistName, err)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Debug(ctx, "Calling Last.FM ArtistGetTopTracks", "artist", artistName, "id", artist.ID)
|
||||||
|
tracks, err := e.lfm.ArtistGetTopTracks(ctx, artistName, count)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
var songs model.MediaFiles
|
var songs model.MediaFiles
|
||||||
for _, t := range tracks {
|
for _, t := range tracks {
|
||||||
mf, err := e.findMatchingTrack(ctx, artist, t.Name, t.MBID)
|
mf, err := e.findMatchingTrack(ctx, t.MBID, artist.ID, t.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -158,19 +164,24 @@ func (e *externalInfo) TopSongs(ctx context.Context, artist string, count int) (
|
||||||
return songs, nil
|
return songs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *externalInfo) findMatchingTrack(ctx context.Context, artist, title, mbid string) (*model.MediaFile, error) {
|
func (e *externalInfo) findMatchingTrack(ctx context.Context, mbid string, artistID, title string) (*model.MediaFile, error) {
|
||||||
|
if mbid != "" {
|
||||||
|
mfs, err := e.ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
||||||
|
Filters: squirrel.Eq{"mbz_track_id": mbid},
|
||||||
|
})
|
||||||
|
if err == nil && len(mfs) > 0 {
|
||||||
|
return &mfs[0], nil
|
||||||
|
}
|
||||||
|
}
|
||||||
mfs, err := e.ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
mfs, err := e.ds.MediaFile(ctx).GetAll(model.QueryOptions{
|
||||||
Filters: squirrel.Or{
|
Filters: squirrel.And{
|
||||||
squirrel.And{
|
squirrel.Or{
|
||||||
squirrel.Or{
|
squirrel.Eq{"artist_id": artistID},
|
||||||
squirrel.Like{"artist": artist},
|
squirrel.Eq{"album_artist_id": artistID},
|
||||||
squirrel.Like{"album_artist": artist},
|
|
||||||
},
|
|
||||||
squirrel.Like{"title": title},
|
|
||||||
},
|
},
|
||||||
squirrel.Like{"mbz_track_id": mbid},
|
squirrel.Like{"title": title},
|
||||||
},
|
},
|
||||||
Sort: "mbz_track_id desc, year asc",
|
Sort: "year",
|
||||||
})
|
})
|
||||||
if err != nil || len(mfs) == 0 {
|
if err != nil || len(mfs) == 0 {
|
||||||
return nil, model.ErrNotFound
|
return nil, model.ErrNotFound
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
package migration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
|
||||||
|
"github.com/pressly/goose"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
goose.AddMigration(Up20201021135455, Down20201021135455)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Up20201021135455(tx *sql.Tx) error {
|
||||||
|
_, err := tx.Exec(`
|
||||||
|
create index if not exists media_file_artist_id
|
||||||
|
on media_file (artist_id);
|
||||||
|
`)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
func Down20201021135455(tx *sql.Tx) error {
|
||||||
|
return nil
|
||||||
|
}
|
Loading…
Reference in New Issue