paginate artist album fetching for deezer and qobuz

This commit is contained in:
2026-04-21 11:39:04 +02:00
parent 9ebddc8316
commit 4f86751ff4
4 changed files with 203 additions and 3 deletions

View File

@@ -122,6 +122,9 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
if mediaType == "label" {
return c.getLabel(ctx, item)
}
if mediaType == "artist" {
return c.getArtist(ctx, item)
}
params := url.Values{}
params.Set("app_id", c.cfg.Session.Qobuz.AppID)
@@ -130,8 +133,6 @@ func (c *Client) GetMetadata(ctx context.Context, item, mediaType string) (map[s
params.Set("offset", "0")
switch mediaType {
case "artist":
params.Set("extra", "albums")
case "playlist":
params.Set("extra", "tracks")
case "label":
@@ -358,6 +359,77 @@ func (c *Client) getLabel(ctx context.Context, labelID string) (map[string]any,
return resp, nil
}
func (c *Client) getArtist(ctx context.Context, artistID string) (map[string]any, error) {
pageLimit := 500
params := url.Values{}
params.Set("app_id", c.cfg.Session.Qobuz.AppID)
params.Set("artist_id", artistID)
params.Set("limit", strconv.Itoa(pageLimit))
params.Set("offset", "0")
params.Set("extra", "albums")
resp, status, err := c.apiRequest(ctx, "artist/get", params, c.authHeaders())
if err != nil {
return nil, err
}
if status != http.StatusOK {
return nil, fmt.Errorf("artist/get failed: status=%d", status)
}
albumsObj, ok := mapValue(resp["albums"])
if !ok {
return resp, nil
}
items, ok := albumsObj["items"].([]any)
if !ok {
return resp, nil
}
total, _ := intValue(resp["albums_count"])
if total <= 0 {
total, _ = intValue(albumsObj["total"])
}
if total <= pageLimit && len(items) < pageLimit {
return resp, nil
}
for offset := pageLimit; ; offset += pageLimit {
if total > 0 && offset >= total {
break
}
pageParams := url.Values{}
pageParams.Set("app_id", c.cfg.Session.Qobuz.AppID)
pageParams.Set("artist_id", artistID)
pageParams.Set("limit", strconv.Itoa(pageLimit))
pageParams.Set("offset", strconv.Itoa(offset))
pageParams.Set("extra", "albums")
pageResp, pageStatus, pageErr := c.apiRequest(ctx, "artist/get", pageParams, c.authHeaders())
if pageErr != nil {
return nil, pageErr
}
if pageStatus != http.StatusOK {
return nil, fmt.Errorf("artist/get pagination failed: status=%d offset=%d", pageStatus, offset)
}
pageAlbums, ok := mapValue(pageResp["albums"])
if !ok {
break
}
pageItems, ok := pageAlbums["items"].([]any)
if !ok || len(pageItems) == 0 {
break
}
items = append(items, pageItems...)
if len(pageItems) < pageLimit {
break
}
}
albumsObj["items"] = items
resp["albums"] = albumsObj
return resp, nil
}
func (c *Client) authHeaders() map[string]string {
headers := map[string]string{"X-App-Id": c.cfg.Session.Qobuz.AppID}
if c.uat != "" {