The first public release of Fabled Minstrel #158
@@ -139,6 +139,34 @@ func TestGetLyrics_ReadsTheSongsLrcByIdOrByName(t *testing.T) {
|
||||
if byName.Lyrics != (Lyrics{}) {
|
||||
t.Errorf("another artist's Dusk = %+v, want no lyrics", byName.Lyrics)
|
||||
}
|
||||
|
||||
// Without an artist, a title only one artist has is that artist's song;
|
||||
// a title two artists have is neither's, even when only one has lyrics.
|
||||
if code := call(t, f.browse().getLyrics, f.user, "title=Dusk", &byName); code != 0 {
|
||||
t.Fatalf("getLyrics: error %d", code)
|
||||
}
|
||||
if byName.Lyrics.Value != "First light\nGone by noon" {
|
||||
t.Errorf("Dusk by title alone = %+v, want Tin Orchard's text", byName.Lyrics)
|
||||
}
|
||||
ctx := context.Background()
|
||||
q := dbq.New(f.pool)
|
||||
other, err := q.UpsertArtist(ctx, dbq.UpsertArtistParams{Name: "Quiet Harbour", SortName: "Quiet Harbour"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := q.UpsertTrack(ctx, dbq.UpsertTrackParams{
|
||||
Title: "Dusk", AlbumID: f.albums["Paper Boats"].ID, ArtistID: other.ID,
|
||||
FilePath: filepath.Join(dir, "Quiet Harbour - Dusk.flac"), DurationMs: 200_000,
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
byName.Lyrics = Lyrics{}
|
||||
if code := call(t, f.browse().getLyrics, f.user, "title=Dusk", &byName); code != 0 {
|
||||
t.Fatalf("getLyrics: error %d", code)
|
||||
}
|
||||
if byName.Lyrics != (Lyrics{}) {
|
||||
t.Errorf("Dusk by title alone, two artists' = %+v, want no lyrics", byName.Lyrics)
|
||||
}
|
||||
}
|
||||
|
||||
// Artist info is the MusicBrainz id and the similar artists in the library,
|
||||
|
||||
@@ -189,10 +189,13 @@ func lrcMillis(min, sec, frac string) int64 {
|
||||
|
||||
// getLyrics finds a song by artist and title, regardless of case, and
|
||||
// returns its lyrics as text. A song not found, or found without lyrics, is
|
||||
// an empty lyrics element, as the spec has it.
|
||||
// an empty lyrics element, as the spec has it. Without an artist, a title
|
||||
// that more than one artist has a song by is also empty: either answer could
|
||||
// be another song's words.
|
||||
func (b *browseHandlers) getLyrics(w http.ResponseWriter, r *http.Request) {
|
||||
params := r.URL.Query()
|
||||
title := strings.TrimSpace(params.Get("title"))
|
||||
artistName := strings.TrimSpace(params.Get("artist"))
|
||||
empty := LyricsResponse{Envelope: NewEnvelope("ok")}
|
||||
if title == "" {
|
||||
Write(w, r, empty)
|
||||
@@ -200,12 +203,16 @@ func (b *browseHandlers) getLyrics(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
q := dbq.New(b.pool)
|
||||
rows, err := q.ListTracksByArtistAndTitle(r.Context(), dbq.ListTracksByArtistAndTitleParams{
|
||||
Title: title, ArtistName: strings.TrimSpace(params.Get("artist")),
|
||||
Title: title, ArtistName: artistName,
|
||||
})
|
||||
if err != nil {
|
||||
WriteFail(w, r, ErrGeneric, "Failed to look up the song")
|
||||
return
|
||||
}
|
||||
if artistName == "" && byMoreThanOneArtist(rows) {
|
||||
Write(w, r, empty)
|
||||
return
|
||||
}
|
||||
for _, row := range rows {
|
||||
l, ok := readLyrics(row.Track.FilePath)
|
||||
if !ok {
|
||||
@@ -224,6 +231,16 @@ func (b *browseHandlers) getLyrics(w http.ResponseWriter, r *http.Request) {
|
||||
Write(w, r, empty)
|
||||
}
|
||||
|
||||
// byMoreThanOneArtist reports whether rows are songs by different artists.
|
||||
func byMoreThanOneArtist(rows []dbq.ListTracksByArtistAndTitleRow) bool {
|
||||
for _, row := range rows {
|
||||
if row.Track.ArtistID != rows[0].Track.ArtistID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// getLyricsBySongID serves getLyricsBySongId, OpenSubsonic's songLyrics
|
||||
// extension: a song's lyrics by id, timed when its .lrc file or tag is
|
||||
// timed. A song without lyrics has an empty list.
|
||||
|
||||
Reference in New Issue
Block a user