feat(deezer): add language support for Deezer API client

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan 2025-11-20 16:48:11 -05:00
parent b58527e906
commit cfd72c3c78
4 changed files with 23 additions and 10 deletions

View File

@ -176,7 +176,8 @@ type spotifyOptions struct {
}
type deezerOptions struct {
Enabled bool
Enabled bool
Language string
}
type listenBrainzOptions struct {
@ -566,6 +567,7 @@ func setViperDefaults() {
viper.SetDefault("spotify.id", "")
viper.SetDefault("spotify.secret", "")
viper.SetDefault("deezer.enabled", true)
viper.SetDefault("deezer.language", "en")
viper.SetDefault("listenbrainz.enabled", true)
viper.SetDefault("listenbrainz.baseurl", "https://api.listenbrainz.org/1/")
viper.SetDefault("httpsecurityheaders.customframeoptionsvalue", "DENY")

View File

@ -28,10 +28,11 @@ type httpDoer interface {
type client struct {
httpDoer httpDoer
language string
}
func newClient(hc httpDoer) *client {
return &client{hc}
func newClient(hc httpDoer, language string) *client {
return &client{hc, language}
}
func (c *client) searchArtists(ctx context.Context, name string, limit int) ([]Artist, error) {
@ -122,7 +123,7 @@ var dzrAppStateRegex = regexp.MustCompile(`window\.__DZR_APP_STATE__\s*=\s*({.+?
var strictPolicy = bluemonday.StrictPolicy()
func (c *client) getArtistBio(ctx context.Context, artistID int) (string, error) {
u := fmt.Sprintf("https://www.deezer.com/en/artist/%d/biography", artistID)
u := fmt.Sprintf("https://www.deezer.com/%s/artist/%d/biography", c.language, artistID)
req, err := http.NewRequestWithContext(ctx, "GET", u, nil)
if err != nil {
return "", err

View File

@ -2,7 +2,6 @@ package deezer
import (
"bytes"
"context"
"io"
"net/http"
"os"
@ -17,7 +16,7 @@ var _ = Describe("client", func() {
BeforeEach(func() {
httpClient = &fakeHttpClient{}
client = newClient(httpClient)
client = newClient(httpClient, "en")
})
Describe("ArtistImages", func() {
@ -26,7 +25,7 @@ var _ = Describe("client", func() {
Expect(err).To(BeNil())
httpClient.mock("https://api.deezer.com/search/artist", http.Response{Body: f, StatusCode: 200})
artists, err := client.searchArtists(context.TODO(), "Michael Jackson", 20)
artists, err := client.searchArtists(GinkgoT().Context(), "Michael Jackson", 20)
Expect(err).To(BeNil())
Expect(artists).To(HaveLen(17))
Expect(artists[0].Name).To(Equal("Michael Jackson"))
@ -39,7 +38,7 @@ var _ = Describe("client", func() {
Body: io.NopCloser(bytes.NewBufferString(`{"data":[],"total":0}`)),
})
_, err := client.searchArtists(context.TODO(), "Michael Jackson", 20)
_, err := client.searchArtists(GinkgoT().Context(), "Michael Jackson", 20)
Expect(err).To(MatchError(ErrNotFound))
})
})
@ -50,12 +49,23 @@ var _ = Describe("client", func() {
Expect(err).To(BeNil())
httpClient.mock("https://www.deezer.com/en/artist/27/biography", http.Response{Body: f, StatusCode: 200})
bio, err := client.getArtistBio(context.TODO(), 27)
bio, err := client.getArtistBio(GinkgoT().Context(), 27)
Expect(err).To(BeNil())
Expect(bio).To(ContainSubstring("Schoolmates Thomas and Guy-Manuel"))
Expect(bio).ToNot(ContainSubstring("<p>"))
Expect(bio).ToNot(ContainSubstring("</p>"))
})
It("uses the configured language", func() {
client = newClient(httpClient, "fr")
f, err := os.Open("tests/fixtures/deezer.artist.bio.html")
Expect(err).To(BeNil())
httpClient.mock("https://www.deezer.com/fr/artist/27/biography", http.Response{Body: f, StatusCode: 200})
_, err = client.getArtistBio(GinkgoT().Context(), 27)
Expect(err).To(BeNil())
Expect(httpClient.lastRequest.URL.String()).To(Equal("https://www.deezer.com/fr/artist/27/biography"))
})
})
})

View File

@ -33,7 +33,7 @@ func deezerConstructor(dataStore model.DataStore) agents.Interface {
Timeout: consts.DefaultHttpClientTimeOut,
}
cachedHttpClient := cache.NewHTTPClient(httpClient, consts.DefaultHttpClientTimeOut)
agent.client = newClient(cachedHttpClient)
agent.client = newClient(cachedHttpClient, conf.Server.Deezer.Language)
return agent
}