Merge main into feat/duplicates-api

This commit is contained in:
tgiachi 2026-11-12 16:53:50 +01:00
commit fff24dfe25
4 changed files with 149 additions and 19 deletions

View file

@ -1668,7 +1668,8 @@ paths:
that tag again for music indexed before); only that id leaves the server. An album whose
files have no id is looked up in MusicBrainz by its artist and title when the
`musicbrainz` provider is on too: the id is taken only when MusicBrainz is sure (an exact
match of both names), is used for that request and is not stored.
match of both names; a title that ends with a note like `(disc 2)` or `(Deluxe Edition)` is
tried again without it), is used for that request and is not stored.
The files of the user always come first. That request can take several seconds and may
answer `provider_unavailable`. At most a couple of covers are built at the same time, so a
client that asks for many at once may wait.

View file

@ -3,6 +3,8 @@ package images
import (
"context"
"errors"
"regexp"
"strings"
"codeberg.org/squidd/sonda/internal/providers"
"codeberg.org/squidd/sonda/internal/providers/coverart"
@ -98,12 +100,43 @@ func (c *CoverArtSource) find(ctx context.Context, albumID int64) (string, error
if artist == "" || title == "" {
return "", ErrNoID
}
id, err := c.Finder.FindReleaseGroup(ctx, artist, title)
switch {
case err != nil:
return "", failed(ctx, err)
case id == "":
return "", ErrNoImage
// The title as the files have it comes first: a release group is named after the album
// and not after a disc, so a title with a disc or an edition note is tried again without it.
tries := []string{title}
if base := baseTitle(title); base != title {
tries = append(tries, base)
}
return id, nil
for _, t := range tries {
id, err := c.Finder.FindReleaseGroup(ctx, artist, t)
if err != nil {
return "", failed(ctx, err)
}
if id != "" {
return id, nil
}
}
return "", ErrNoImage
}
// notesRe is a note at the end of a title that says which disc or which edition it is: a group
// in brackets (closed, or cut off by a tag that is too short) that has one of the words of a
// note, like "(disc 2)", "(bonus disc: Rarities)" or "[2011 Deluxe Remaster]". A group without
// them ("(The Mastermind Redux)") is part of the title.
var notesRe = regexp.MustCompile(`(?i)\s*[(\[][^()\[\]]*\b(?:disc|disk|cd|bonus|deluxe|remaster(?:ed)?|edition|expanded|anniversary|reissue|special|limited)\b[^()\[\]]*[)\]]?\s*$`)
// baseTitle is a title without the notes at its end, or the title itself when taking them off
// would leave nothing.
func baseTitle(title string) string {
base := strings.TrimSpace(title)
for {
next := strings.TrimSpace(notesRe.ReplaceAllString(base, ""))
if next == base || next == "" {
break
}
base = next
}
if base == "" {
return title
}
return base
}

View file

@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"slices"
"testing"
"codeberg.org/squidd/sonda/internal/providers"
@ -145,17 +146,26 @@ func TestACallerWhoLeavesGetsTheCancellation(t *testing.T) {
// ---- albums without an id: looked up in MusicBrainz by the artist and the title ----
type finderFake struct {
id string
err error
calls int
artist string
title string
id string
answers map[string]string // by title: when set, a title it does not list has no answer
err error
calls int
artist string
title string
titles []string
}
func (f *finderFake) FindReleaseGroup(_ context.Context, artist, title string) (string, error) {
f.calls++
f.artist, f.title = artist, title
return f.id, f.err
f.titles = append(f.titles, title)
if f.err != nil {
return "", f.err
}
if f.answers != nil {
return f.answers[title], nil
}
return f.id, nil
}
// searching is an archive whose source may look an album up: album 2 has no id and a name,
@ -165,8 +175,11 @@ func searching() (*archive, *finderFake) {
a.sw.set(providers.MusicBrainz, true)
f := &finderFake{id: releaseGroup}
a.src.Albums = albumsFake{
ids: map[int64]string{1: releaseGroup, 2: "", 3: ""},
names: map[int64][2]string{1: {"Radiohead", "The Bends"}, 2: {"Radiohead", "OK Computer"}, 3: {"", "Untitled"}},
ids: map[int64]string{1: releaseGroup, 2: "", 3: "", 4: "", 5: ""},
names: map[int64][2]string{
1: {"Radiohead", "The Bends"}, 2: {"Radiohead", "OK Computer"}, 3: {"", "Untitled"},
4: {"Aerosmith", "Alive II (disc 2)"}, 5: {"Pearl Jam", "Ten"},
},
}
a.src.Finder = f
return a, f
@ -260,3 +273,84 @@ func TestSwitchedOffDuringTheLookupNothingIsReturned(t *testing.T) {
t.Errorf("err = %v, want ErrProviderDisabled", err)
}
}
func TestADiscOrEditionSuffixIsTriedWithoutItWhenTheFullTitleHasNoSureResult(t *testing.T) {
a, f := searching()
f.answers = map[string]string{"Alive II": releaseGroup}
got, err := a.src.AlbumImage(context.Background(), 4)
if err != nil || string(got.Data) != "cover bytes" {
t.Fatalf("got %+v err %v", got, err)
}
if want := []string{"Alive II (disc 2)", "Alive II"}; !slices.Equal(f.titles, want) {
t.Errorf("titles asked = %q, want %q", f.titles, want)
}
if a.cv.got != releaseGroup {
t.Errorf("the archive was asked for %q", a.cv.got)
}
}
func TestTheFullTitleComesFirstAndIsEnoughWhenItIsSure(t *testing.T) {
a, f := searching()
f.answers = map[string]string{"Alive II (disc 2)": releaseGroup, "Alive II": "cccccccc-1111-4222-8333-444444444444"}
if _, err := a.src.AlbumImage(context.Background(), 4); err != nil {
t.Fatal(err)
}
if want := []string{"Alive II (disc 2)"}; !slices.Equal(f.titles, want) {
t.Errorf("titles asked = %q, want only the full one", f.titles)
}
if a.cv.got != releaseGroup {
t.Errorf("the archive was asked for %q", a.cv.got)
}
}
func TestATitleWithoutASuffixIsAskedOnce(t *testing.T) {
a, f := searching()
f.id = ""
if _, err := a.src.AlbumImage(context.Background(), 5); !errors.Is(err, ErrNoImage) {
t.Errorf("err = %v, want ErrNoImage", err)
}
if f.calls != 1 {
t.Errorf("MusicBrainz was asked %d times, want once", f.calls)
}
}
func TestWhenNeitherTitleHasASureResultThereIsNoCoverAndAFailureStops(t *testing.T) {
a, f := searching()
f.id = ""
if _, err := a.src.AlbumImage(context.Background(), 4); !errors.Is(err, ErrNoImage) || f.calls != 2 {
t.Errorf("err = %v after %d calls, want ErrNoImage after 2", err, f.calls)
}
a, f = searching()
f.err = musicbrainz.ErrUnavailable
if _, err := a.src.AlbumImage(context.Background(), 4); !errors.Is(err, ErrSourceUnavailable) || f.calls != 1 {
t.Errorf("err = %v after %d calls, want an outage after 1", err, f.calls)
}
}
func TestBaseTitleTakesOffTheDiscAndEditionNotes(t *testing.T) {
for in, want := range map[string]string{
"Alive II (disc 2)": "Alive II",
"Crossroads (Disc 2)": "Crossroads",
"The Boogie House Tapes (Disc 1": "The Boogie House Tapes",
"Ummagumma (disc 1: Live Album)": "Ummagumma",
"Seventeen Seconds (bonus disc: Rarities 1979-1980)": "Seventeen Seconds",
"Pornography (bonus disc: Rarit": "Pornography",
"Exciter (bonus disc)": "Exciter",
"A-Ha Shake Heartbreak (Bonus CD)": "A-Ha Shake Heartbreak",
"Live At Wembley Stadium (2011 Deluxe Remaster)": "Live At Wembley Stadium",
"Extreme Behavior (Special Edit": "Extreme Behavior",
"Album [Remastered]": "Album",
"Album (Deluxe Edition) (disc 2)": "Album",
"Soundtrack to the Apocalypse (disc 5: Live at The Grove in Anaheim, CA 5/2/02)": "Soundtrack to the Apocalypse",
// What is part of the title stays.
"Cobras And Fire (The Mastermind Redux)": "Cobras And Fire (The Mastermind Redux)",
"This Type Of Thinking (Could Do Us In)": "This Type Of Thinking (Could Do Us In)",
"Ten": "Ten",
"(disc 1)": "(disc 1)", // nothing would be left
"Disc One": "Disc One", // not in brackets
} {
if got := baseTitle(in); got != want {
t.Errorf("baseTitle(%q) = %q, want %q", in, got, want)
}
}
}

View file

@ -188,7 +188,8 @@ const maxNameLen = 200
// A result is taken only if MusicBrainz scores it at least 95, its title is the title asked for
// and one of the names of its artist credit is the artist asked for, all compared after folding
// case, accents and punctuation. The first such result in MusicBrainz's order wins. Nothing
// else is guessed: an album called "OK Computer (Remastered)" finds nothing for "OK Computer".
// else is guessed: an album called "OK Computer (Remastered)" finds nothing for "OK Computer"
// (the caller may ask again with the title without the note).
// Only the artist and the title are sent.
func (c *Client) FindReleaseGroup(ctx context.Context, artist, title string) (string, error) {
artist, title = strings.TrimSpace(artist), strings.TrimSpace(title)