Scan only newly ripped albums after download

This commit is contained in:
Joren committed 2026-11-26 22:23:41 +01:00
1 parent 2e69574943
commit e593b78185
3 files changed
+121 -3

No files matched your search

+28
View File
@@ -360,6 +360,19 @@ func runSync(ctx context.Context, args []string) error {
switch {
case *skipDownload && preIdx != nil:
idx = preIdx
case preIdx != nil && len(qobuzIDs) > 0:
ripped := albumsForQobuzIDs(allAlbums, allMatches, qobuzIDs)
before := preIdx.FileCount()
if addErr := preIdx.AddAlbums(ctx, cfg.Global.DownloadsDir, ripped, allMatches, debugf); addErr != nil {
logf("library: scan of ripped albums failed: %v; rebuilding", addErr)
idx, err = buildLibraryIndex(ctx, cfg, allAlbums, allMatches, beetsFiles, debugf)
if err != nil {
return errors.Join(runErr, err)
}
} else {
idx = preIdx
logf("library: %d new files from %d ripped album(s)", idx.FileCount()-before, len(ripped))
}
case len(qobuzIDs) == 0 && preIdx != nil:
idx = preIdx
default:
@@ -576,6 +589,21 @@ func albumsToDownload(runs []*playlistRun, idx *library.Index) ([]string, int) {
return ids, present
}
func albumsForQobuzIDs(albums []*model.Album, matches map[string]*model.AlbumMatch, ids []string) []*model.Album {
wanted := make(map[string]bool, len(ids))
for _, id := range ids {
wanted[id] = true
}
out := make([]*model.Album, 0, len(ids))
for _, album := range albums {
match := matches[album.AppleID]
if match != nil && wanted[match.QobuzAlbumID] {
out = append(out, album)
}
}
return out
}
func albumSatisfied(idx *library.Index, run *playlistRun, album *model.Album) bool {
if len(album.Tracks) == 0 {
return false
+49 -3
View File
@@ -290,18 +290,31 @@ func findAlbumDirs(album *model.Album, match *model.AlbumMatch, dirs []dirInfo)
return nil
}
// A distinctive title can stand on its own if the artist tag differs.
// Short, generic titles ("Home", "Goodfellas") must match the artist too;
// otherwise thousands of unrelated folders get scanned.
titleDistinctive := len([]rune(normTitle)) >= 12 && strings.Contains(normTitle, " ")
find := func(titleNeedle string) []string {
var candidates []dirCandidate
for _, dir := range dirs {
if !strings.Contains(dir.norm, titleNeedle) {
continue
}
artistOK := normArtist != "" && strings.Contains(dir.norm, normArtist)
combined := strings.TrimSpace(normArtist + " " + normTitle)
combinedOK := combined != "" && strings.Contains(dir.norm, combined)
if !combinedOK && artistOK {
combinedOK = strings.Contains(dir.norm, strings.TrimSpace(normArtist+" "+titleNeedle))
}
if !combinedOK && !artistOK && !titleDistinctive {
continue
}
s := 10
if normArtist != "" && strings.Contains(dir.norm, normArtist) {
if artistOK {
s += 5
}
combined := strings.TrimSpace(normArtist + " " + normTitle)
if combined != "" && strings.Contains(dir.norm, combined) {
if combinedOK {
s += 3
}
// Prefer the shortest folder name when scores tie.
@@ -474,6 +487,39 @@ func rawISRC(raw map[string]any) string {
return ""
}
// AddAlbums scans the download folder for the folders of the given albums and
// adds any new files to the index. It is used after rip so the entire staging
// tree does not have to be walked and tagged a second time.
func (i *Index) AddAlbums(ctx context.Context, downloadsRoot string, albums []*model.Album, matches map[string]*model.AlbumMatch, logf func(string, ...any)) error {
downloadsRoot = cleanRoot(downloadsRoot)
if downloadsRoot == "" || len(albums) == 0 {
return nil
}
info, err := os.Stat(downloadsRoot)
if err != nil {
return fmt.Errorf("downloads_dir %s: %w", downloadsRoot, err)
}
if !info.IsDir() {
return fmt.Errorf("downloads_dir %s is not a directory", downloadsRoot)
}
if logf == nil {
logf = func(string, ...any) {}
}
dirs, err := listDirs(downloadsRoot, 4)
if err != nil {
return err
}
for _, album := range albums {
if err := ctx.Err(); err != nil {
return err
}
for _, candidate := range findAlbumDirs(album, matches[album.AppleID], dirs) {
i.scanFiles(ctx, candidate, 2, logf)
}
}
return nil
}
// rootScore prefers files that are in the final library but not in rip's
// download folder.
func (i *Index) rootScore(path string) int {
+44
View File
@@ -37,6 +37,50 @@ func TestFindAlbumDirs(t *testing.T) {
}
}
func TestFindAlbumDirsRejectsWrongArtistShortTitle(t *testing.T) {
album := &model.Album{AppleID: "a1", Title: "Goodfellas", Artist: "Some Band"}
dirs := []dirInfo{
{path: "/music/Tai Haf Heb Drigolyn - _Goodfellas_ (2026)", norm: "tai haf heb drigolyn goodfellas 2026 flac"},
}
if got := findAlbumDirs(album, nil, dirs); len(got) != 0 {
t.Fatalf("expected no candidates, got %#v", got)
}
}
func TestAddAlbumsScansNewAlbum(t *testing.T) {
root := t.TempDir()
downloads := filepath.Join(root, "downloads")
libraryRoot := filepath.Join(root, "library")
if err := os.MkdirAll(downloads, 0o755); err != nil {
t.Fatal(err)
}
if err := os.MkdirAll(libraryRoot, 0o755); err != nil {
t.Fatal(err)
}
idx := newIndex()
idx.downloadsRoot = downloads
idx.libraryRoot = libraryRoot
albumDir := filepath.Join(downloads, "Michael Nau - Popfly (2026) [FLAC]")
if err := os.MkdirAll(albumDir, 0o755); err != nil {
t.Fatal(err)
}
file := filepath.Join(albumDir, "01. Michael Nau - Lost Station Hideaway.flac")
if err := os.WriteFile(file, nil, 0o644); err != nil {
t.Fatal(err)
}
albums := []*model.Album{{AppleID: "a1", Title: "Popfly", Artist: "Michael Nau", TrackCount: 10}}
matches := map[string]*model.AlbumMatch{"a1": {QobuzTitle: "Popfly", QobuzArtist: "Michael Nau"}}
if err := idx.AddAlbums(context.Background(), downloads, albums, matches, nil); err != nil {
t.Fatal(err)
}
track := &model.Track{Title: "Lost Station Hideaway", ArtistName: "Michael Nau", AlbumName: "Popfly"}
got, ok := idx.Resolve(track, albums[0], matches["a1"])
if !ok || filepath.Base(got.Path) != filepath.Base(file) {
t.Fatalf("got %+v ok=%v", got, ok)
}
}
func TestFindAlbumDirsLongTitlePrefix(t *testing.T) {
album := &model.Album{AppleID: "a1", Title: "A very long album title that will certainly exceed the truncation limit used by the ripper for folder names", Artist: "Artist"}
dirs := []dirInfo{