feat(smart): smart playlists by the year a song came out
I can now ask for songs from a year to a year, for example the eighties. - The year is read from the tags of each file (the original date first, then the date) and kept on the track: migration 0028 adds tracks.year (SQLite and PostgreSQL). - Two new rules, min_year and max_year, both included and checked (1000 to 2999, the first not after the second). The index answers them. A song with no year never matches. - Files indexed before have no year until a full rescan reads their tags again; the editor and the API description say so. - The smart playlist editor gets From year and Up to year, and the rules are described in words. - English and Italian.
This commit is contained in:
parent
6028d8e686
commit
d3695d00d6
12 changed files with 273 additions and 16 deletions
|
|
@ -91,12 +91,14 @@ func TestASmartPlaylistIsMadeReadChangedAndDeleted(t *testing.T) {
|
|||
func TestWhatIsNotAcceptedIsSaidSo(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
for name, body := range map[string]map[string]any{
|
||||
"no name": {"rules": map[string]any{}},
|
||||
"an unknown order": {"name": "a", "sort": "loudest"},
|
||||
"a limit too big": {"name": "a", "limit": 501},
|
||||
"days below zero": {"name": "a", "rules": map[string]any{"played_within_days": -1}},
|
||||
"a genre that isn't": {"name": "a", "rules": map[string]any{"genres": []string{"unknown"}}},
|
||||
"a bad format": {"name": "a", "rules": map[string]any{"formats": []string{"fl/ac"}}},
|
||||
"no name": {"rules": map[string]any{}},
|
||||
"an unknown order": {"name": "a", "sort": "loudest"},
|
||||
"a limit too big": {"name": "a", "limit": 501},
|
||||
"days below zero": {"name": "a", "rules": map[string]any{"played_within_days": -1}},
|
||||
"a genre that isn't": {"name": "a", "rules": map[string]any{"genres": []string{"unknown"}}},
|
||||
"a bad format": {"name": "a", "rules": map[string]any{"formats": []string{"fl/ac"}}},
|
||||
"a year too early": {"name": "a", "rules": map[string]any{"min_year": 99}},
|
||||
"years the wrong way": {"name": "a", "rules": map[string]any{"min_year": 1990, "max_year": 1980}},
|
||||
} {
|
||||
if r := do(t, m.h, "POST", sp, m.alice, body); r.Code != http.StatusBadRequest || errCode(r) != "invalid_input" {
|
||||
t.Errorf("%s: %d %v", name, r.Code, r.Body)
|
||||
|
|
@ -112,6 +114,21 @@ func TestWhatIsNotAcceptedIsSaidSo(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestTheYearRulesAreKeptAndShown(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
id := smartMade(t, m, map[string]any{"name": "Eighties", "rules": map[string]any{"min_year": 1980, "max_year": 1989}})
|
||||
r := do(t, m.h, "GET", fmt.Sprintf("%s/%d", sp, id), m.alice, nil)
|
||||
rules, _ := r.Body["rules"].(map[string]any)
|
||||
if r.Code != http.StatusOK || rules["min_year"] != float64(1980) || rules["max_year"] != float64(1989) {
|
||||
t.Errorf("get: %d %v", r.Code, r.Body)
|
||||
}
|
||||
// None of the songs of this library has a year, so the rule leaves none.
|
||||
tr := do(t, m.h, "GET", fmt.Sprintf("%s/%d/tracks", sp, id), m.alice, nil)
|
||||
if tr.Code != http.StatusOK || len(trackIDs(t, tr)) != 0 {
|
||||
t.Errorf("tracks: %d %v", tr.Code, tr.Body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheSongsAreWorkedOutFromTheRules(t *testing.T) {
|
||||
m := newMixAPI(t)
|
||||
id := smartMade(t, m, map[string]any{"name": "Hard", "sort": "title", "limit": 5, "rules": map[string]any{"genres": []string{"hard rock"}}})
|
||||
|
|
|
|||
|
|
@ -4939,6 +4939,8 @@ components:
|
|||
without_avoided_artists: {type: boolean, description: Leaves out songs by artists you avoid.}
|
||||
min_duration_sec: {type: integer, minimum: 0, maximum: 86400}
|
||||
max_duration_sec: {type: integer, minimum: 0, maximum: 86400}
|
||||
min_year: {type: integer, minimum: 0, maximum: 2999, description: "The song came out in this year or later (0 or a year from 1000). The year comes from the tags of the file, the original date first. A song whose file has no year never matches a year rule, and the files indexed before this rule existed have none until a full rescan (`POST /library/scan?full=true`) reads their tags again."}
|
||||
max_year: {type: integer, minimum: 0, maximum: 2999, description: "The song came out in this year or earlier. With `min_year` set it cannot be lower than it. See `min_year`."}
|
||||
|
||||
SuggestedSmartPlaylist:
|
||||
type: object
|
||||
|
|
|
|||
|
|
@ -58,6 +58,7 @@ func (TaglibReader) Read(path string) (Metadata, error) {
|
|||
AlbumMBID: albumID,
|
||||
TrackNo: leadingInt(first(tags, "TRACKNUMBER")),
|
||||
DiscNo: leadingInt(first(tags, "DISCNUMBER")),
|
||||
Year: yearOf(tags),
|
||||
},
|
||||
Properties: Properties{
|
||||
Format: strings.ToLower(props.Format),
|
||||
|
|
@ -81,6 +82,22 @@ func (TaglibReader) Read(path string) (Metadata, error) {
|
|||
return m, nil
|
||||
}
|
||||
|
||||
// yearOf reads the year a song came out: the original date when the file has one (a reissue says when it was
|
||||
// reissued in DATE), then the date. Both are written as 1997, 1997-05-21 or 1997-05-21T10:00:00; anything that does
|
||||
// not start with four digits that make a plausible year is no year.
|
||||
func yearOf(tags map[string][]string) int {
|
||||
for _, key := range []string{"ORIGINALDATE", "ORIGINALYEAR", "DATE", "YEAR"} {
|
||||
v := strings.TrimSpace(first(tags, key))
|
||||
if len(v) < 4 {
|
||||
continue
|
||||
}
|
||||
if y, err := strconv.Atoi(v[:4]); err == nil && y >= MinYear && y <= MaxYear && (len(v) == 4 || v[4] < '0' || v[4] > '9') {
|
||||
return y
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// maxGenreLen bounds one genre; a longer one is a sentence, not a genre.
|
||||
const maxGenreLen = 100
|
||||
|
||||
|
|
|
|||
|
|
@ -302,3 +302,29 @@ func TestCleanGenres(t *testing.T) {
|
|||
t.Errorf("a 101-character genre is noise, kept as %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaglibReaderYear(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
for name, tc := range map[string]struct {
|
||||
meta map[string]string
|
||||
want int
|
||||
}{
|
||||
"a full date": {map[string]string{"title": "a", "date": "1997-05-21"}, 1997},
|
||||
"only a year": {map[string]string{"title": "b", "date": "1973"}, 1973},
|
||||
"the original year wins": {map[string]string{"title": "c", "date": "2011", "originaldate": "1979-11-30"}, 1979},
|
||||
"no date": {map[string]string{"title": "d"}, 0},
|
||||
"something that is no year": {map[string]string{"title": "e", "date": "unknown"}, 0},
|
||||
"a year out of range": {map[string]string{"title": "f", "date": "0000"}, 0},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
p := makeAudio(t, dir, strings.ReplaceAll(name, " ", "_")+".flac", tc.meta)
|
||||
m, err := TaglibReader{}.Read(p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m.Year != tc.want {
|
||||
t.Errorf("year = %d, want %d", m.Year, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,8 +31,15 @@ type Tags struct {
|
|||
AlbumMBID string // the MusicBrainz release group id of the album, when the file has exactly one
|
||||
TrackNo int
|
||||
DiscNo int
|
||||
Year int // the year the song came out (the original date first), 0 when the file has none or it is not a year
|
||||
}
|
||||
|
||||
// MinYear and MaxYear bound what is taken for the year of a song.
|
||||
const (
|
||||
MinYear = 1000
|
||||
MaxYear = 2999
|
||||
)
|
||||
|
||||
// MaxGenresPerTrack bounds the genres kept for one file.
|
||||
const MaxGenresPerTrack = 5
|
||||
|
||||
|
|
|
|||
|
|
@ -37,6 +37,8 @@ const (
|
|||
MaxPlays = 1_000_000
|
||||
MaxBitrateKbps = 100_000
|
||||
MaxDurationSec = 86_400
|
||||
MinYear = 1000 // the first and the last year a rule can name
|
||||
MaxYear = 2999
|
||||
DefaultLimit = 50
|
||||
MaxLimit = 500
|
||||
MaxPlaylists = 200 // smart playlists of one user
|
||||
|
|
@ -108,6 +110,10 @@ type Rules struct {
|
|||
WithoutAvoidedArtists bool `json:"without_avoided_artists,omitempty"`
|
||||
MinDurationSec int `json:"min_duration_sec,omitempty"`
|
||||
MaxDurationSec int `json:"max_duration_sec,omitempty"`
|
||||
// MinYear and MaxYear are about the year the song came out, from the tags of its file: both limits count
|
||||
// (1980 to 1989 is the eighties). A song whose file has no year never matches a year rule.
|
||||
MinYear int `json:"min_year,omitempty"`
|
||||
MaxYear int `json:"max_year,omitempty"`
|
||||
}
|
||||
|
||||
// SmartPlaylist is a smart playlist.
|
||||
|
|
@ -161,6 +167,7 @@ type Filter struct {
|
|||
AlbumIDs, NotAlbumIDs []int64
|
||||
Formats []string
|
||||
MinBitrateKbps int
|
||||
MinYear, MaxYear int // 0 is no limit
|
||||
}
|
||||
|
||||
// Store keeps the smart playlists and reads what they are made of. Every call that is about one playlist is
|
||||
|
|
@ -329,6 +336,17 @@ func cleanRules(r Rules) (Rules, error) {
|
|||
if r.MaxDurationSec > 0 && r.MinDurationSec > r.MaxDurationSec {
|
||||
return Rules{}, invalid("min_duration_sec is more than max_duration_sec")
|
||||
}
|
||||
for _, y := range []struct {
|
||||
name string
|
||||
got int
|
||||
}{{"min_year", r.MinYear}, {"max_year", r.MaxYear}} {
|
||||
if y.got != 0 && (y.got < MinYear || y.got > MaxYear) {
|
||||
return Rules{}, invalid("%s must be 0 or a year from %d to %d", y.name, MinYear, MaxYear)
|
||||
}
|
||||
}
|
||||
if r.MinYear > 0 && r.MaxYear > 0 && r.MinYear > r.MaxYear {
|
||||
return Rules{}, invalid("min_year is more than max_year")
|
||||
}
|
||||
return r, nil
|
||||
}
|
||||
|
||||
|
|
@ -538,7 +556,7 @@ func (s *Service) run(ctx context.Context, userID, id int64, r Rules, order Sort
|
|||
func (s *Service) pick(ctx context.Context, userID, id int64, r Rules, order Sort, limit, variant int) ([]int64, error) {
|
||||
f := Filter{
|
||||
ArtistIDs: r.ArtistIDs, NotArtistIDs: r.NotArtistIDs, AlbumIDs: r.AlbumIDs, NotAlbumIDs: r.NotAlbumIDs,
|
||||
Formats: r.Formats, MinBitrateKbps: r.MinBitrateKbps,
|
||||
Formats: r.Formats, MinBitrateKbps: r.MinBitrateKbps, MinYear: r.MinYear, MaxYear: r.MaxYear,
|
||||
}
|
||||
if len(r.Genres) > 0 || len(r.NotGenres) > 0 {
|
||||
all, err := s.store.Genres(ctx)
|
||||
|
|
@ -734,5 +752,7 @@ func (r Rules) Summary() string {
|
|||
add(r.WithoutAvoidedArtists, "without avoided artists")
|
||||
add(r.MinDurationSec > 0, "at least %d minutes long", r.MinDurationSec/60)
|
||||
add(r.MaxDurationSec > 0, "at most %d minutes long", r.MaxDurationSec/60)
|
||||
add(r.MinYear > 0, "from %d", r.MinYear)
|
||||
add(r.MaxYear > 0, "up to %d", r.MaxYear)
|
||||
return strings.Join(parts, "; ")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
|
|
@ -794,3 +795,35 @@ func TestTheCoversOfASuggestedOneAreThoseOfItsSongs(t *testing.T) {
|
|||
t.Errorf("unknown key: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTheYearRulesGoToTheIndexAndAreChecked(t *testing.T) {
|
||||
st := newStore()
|
||||
st.cands = songs(3)
|
||||
svc := newService(st, taste.Learned{})
|
||||
preview(t, svc, Rules{MinYear: 1980, MaxYear: 1989}, SortTitle)
|
||||
if len(st.asked) != 1 || st.asked[0].MinYear != 1980 || st.asked[0].MaxYear != 1989 {
|
||||
t.Errorf("the index was asked %+v", st.asked)
|
||||
}
|
||||
for name, r := range map[string]Rules{
|
||||
"a year before 1000": {MinYear: 999},
|
||||
"a year after 2999": {MaxYear: 3000},
|
||||
"a negative year": {MinYear: -1},
|
||||
"from after up to": {MinYear: 1990, MaxYear: 1980},
|
||||
} {
|
||||
if _, err := svc.Preview(context.Background(), 1, Input{Rules: r}, 0); !errors.Is(err, ErrInvalidInput) {
|
||||
t.Errorf("%s: err = %v, want ErrInvalidInput", name, err)
|
||||
}
|
||||
}
|
||||
for name, r := range map[string]Rules{
|
||||
"one year": {MinYear: 1985, MaxYear: 1985},
|
||||
"only a start": {MinYear: 1950},
|
||||
"only an end": {MaxYear: 2024},
|
||||
} {
|
||||
if _, err := svc.Preview(context.Background(), 1, Input{Rules: r}, 0); err != nil {
|
||||
t.Errorf("%s: %v", name, err)
|
||||
}
|
||||
}
|
||||
if got := (Rules{MinYear: 1980, MaxYear: 1989}).Summary(); !strings.Contains(got, "from 1980") || !strings.Contains(got, "up to 1989") {
|
||||
t.Errorf("summary = %q", got)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -47,6 +47,7 @@ type trackModel struct {
|
|||
TrackNo int `bun:"track_no,nullzero"`
|
||||
DiscNo int `bun:"disc_no,nullzero"`
|
||||
AlbumID int64 `bun:"album_id,nullzero"`
|
||||
Year int `bun:"year,nullzero"`
|
||||
}
|
||||
|
||||
type trackArtistModel struct {
|
||||
|
|
@ -152,7 +153,7 @@ func (r *LibraryRepo) SaveFile(ctx context.Context, libraryID int64, f library.P
|
|||
}
|
||||
|
||||
track := &trackModel{
|
||||
Title: f.Title, TitleKey: matchKey(f.Title), SearchKey: searchKey(f.Title), DurationMs: f.DurationMs, TrackNo: f.TrackNo, DiscNo: f.DiscNo, AlbumID: albumID,
|
||||
Title: f.Title, TitleKey: matchKey(f.Title), SearchKey: searchKey(f.Title), DurationMs: f.DurationMs, TrackNo: f.TrackNo, DiscNo: f.DiscNo, AlbumID: albumID, Year: f.Year,
|
||||
}
|
||||
|
||||
if existingID == 0 {
|
||||
|
|
@ -187,7 +188,7 @@ func (r *LibraryRepo) SaveFile(ctx context.Context, libraryID int64, f library.P
|
|||
return err
|
||||
}
|
||||
if _, err := tx.NewUpdate().Model(track).
|
||||
Column("title", "title_key", "search_key", "duration_ms", "track_no", "disc_no", "album_id").
|
||||
Column("title", "title_key", "search_key", "duration_ms", "track_no", "disc_no", "album_id", "year").
|
||||
WherePK().Exec(ctx); err != nil {
|
||||
return fmt.Errorf("update track: %w", err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -621,3 +621,44 @@ func TestTheIDOfAnAlbumWithNoTracksLeftPassesToTheRetaggedOne(t *testing.T) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestLibraryRepoKeepsTheYearOfATrackAndUpdatesItOnARescan(t *testing.T) {
|
||||
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
||||
ctx := context.Background()
|
||||
repo := NewLibraryRepo(db)
|
||||
lib := mkLib(t, repo, "music")
|
||||
f := parsed("a/1.flac", "Song", "Album", "Radiohead")
|
||||
f.Year = 1997
|
||||
if _, err := repo.SaveFile(ctx, lib, f, 0); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
year := func() sql.NullInt64 {
|
||||
var y sql.NullInt64
|
||||
if err := db.QueryRow("SELECT year FROM tracks WHERE title = 'Song'").Scan(&y); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return y
|
||||
}
|
||||
if y := year(); !y.Valid || y.Int64 != 1997 {
|
||||
t.Fatalf("year = %v, want 1997", y)
|
||||
}
|
||||
var fileID int64
|
||||
if err := db.QueryRow("SELECT id FROM audio_files").Scan(&fileID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
f.Year = 1998
|
||||
if _, err := repo.SaveFile(ctx, lib, f, fileID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if y := year(); y.Int64 != 1998 {
|
||||
t.Errorf("after a rescan year = %v, want 1998", y)
|
||||
}
|
||||
f.Year = 0
|
||||
if _, err := repo.SaveFile(ctx, lib, f, fileID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if y := year(); y.Valid {
|
||||
t.Errorf("a file with no year leaves the track with none, got %v", y)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -261,6 +261,12 @@ func (r *SmartRepo) Candidates(ctx context.Context, f smart.Filter) ([]smart.Can
|
|||
if len(f.NotAlbumIDs) > 0 {
|
||||
add("(t.album_id IS NULL OR t.album_id NOT IN (?))", bun.In(f.NotAlbumIDs))
|
||||
}
|
||||
if f.MinYear > 0 {
|
||||
add("t.year >= ?", f.MinYear)
|
||||
}
|
||||
if f.MaxYear > 0 {
|
||||
add("t.year <= ?", f.MaxYear)
|
||||
}
|
||||
if len(f.Formats) > 0 || f.MinBitrateKbps > 0 {
|
||||
file := "EXISTS (SELECT 1 FROM audio_files f WHERE f.track_id = t.id AND f.missing_at IS NULL"
|
||||
var fargs []any
|
||||
|
|
|
|||
|
|
@ -412,3 +412,36 @@ func TestSmartRepoAlbumCoversFollowTheOrderOfTheSongsAndSkipWhatHasNoCover(t *te
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
func TestSmartRepoCandidatesNarrowByYear(t *testing.T) {
|
||||
smartBackends(t, smartSongs, func(t *testing.T, e *smartEnv) {
|
||||
ctx := context.Background()
|
||||
// a 1975, b 1985, c 1985, d 1995, e has no year.
|
||||
for title, year := range map[string]int{"a": 1975, "b": 1985, "c": 1985, "d": 1995} {
|
||||
if _, err := e.db.Exec("UPDATE tracks SET year = ? WHERE id = ?", year, e.tracks[title]); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
for name, tc := range map[string]struct {
|
||||
f smart.Filter
|
||||
want []string
|
||||
}{
|
||||
"from a year": {smart.Filter{MinYear: 1985}, []string{"b", "c", "d"}},
|
||||
"up to a year": {smart.Filter{MaxYear: 1985}, []string{"a", "b", "c"}},
|
||||
"between two years": {smart.Filter{MinYear: 1980, MaxYear: 1989}, []string{"b", "c"}},
|
||||
"one year": {smart.Filter{MinYear: 1995, MaxYear: 1995}, []string{"d"}},
|
||||
"a year and a genre": {smart.Filter{MinYear: 1980, Genres: []string{"rock"}}, []string{"b", "c"}},
|
||||
"no year rule: a song with no year is still there": {smart.Filter{}, []string{"a", "b", "c", "d", "e"}},
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
got, err := e.repo.Candidates(ctx, tc.f)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if g := names(e, got); !slices.Equal(g, tc.want) {
|
||||
t.Errorf("got %v, want %v", g, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
|
|
@ -156,8 +156,8 @@ func TestMigrateCreatesSchemaAndIsIdempotent(t *testing.T) {
|
|||
if err := db.QueryRow("SELECT COUNT(*) FROM schema_migrations").Scan(&applied); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if applied != 27 {
|
||||
t.Errorf("applied migrations = %d, want 27", applied)
|
||||
if applied != 28 {
|
||||
t.Errorf("applied migrations = %d, want 28", applied)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue