mirror of
https://git.sr.ht/~joren/streamrip-go
synced 2026-06-17 15:05:39 +02:00
paginate tidal artist album fetches across all pages
This commit is contained in:
@@ -310,31 +310,49 @@ func (c *Client) fetchArtistAlbums(ctx context.Context, artistID string) ([]map[
|
||||
out := make([]map[string]any, 0)
|
||||
seen := map[string]struct{}{}
|
||||
for _, p := range paths {
|
||||
resp, status, err := c.apiRequest(ctx, p.path, p.params, c.baseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if status != http.StatusOK {
|
||||
return nil, fmt.Errorf("tidal artist albums failed: status=%d", status)
|
||||
}
|
||||
items, _ := resp["items"].([]any)
|
||||
for _, raw := range items {
|
||||
itm, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
offset := 0
|
||||
for {
|
||||
params := url.Values{}
|
||||
for k, values := range p.params {
|
||||
for _, v := range values {
|
||||
params.Add(k, v)
|
||||
}
|
||||
}
|
||||
if wrapped, ok := itm["item"].(map[string]any); ok {
|
||||
itm = wrapped
|
||||
params.Set("offset", strconv.Itoa(offset))
|
||||
|
||||
resp, status, err := c.apiRequest(ctx, p.path, params, c.baseURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id := stringify(itm["id"])
|
||||
if id == "" {
|
||||
continue
|
||||
if status != http.StatusOK {
|
||||
return nil, fmt.Errorf("tidal artist albums failed: status=%d offset=%d", status, offset)
|
||||
}
|
||||
if _, dup := seen[id]; dup {
|
||||
continue
|
||||
items, _ := resp["items"].([]any)
|
||||
if len(items) == 0 {
|
||||
break
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
out = append(out, itm)
|
||||
for _, raw := range items {
|
||||
itm, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if wrapped, ok := itm["item"].(map[string]any); ok {
|
||||
itm = wrapped
|
||||
}
|
||||
id := stringify(itm["id"])
|
||||
if id == "" {
|
||||
continue
|
||||
}
|
||||
if _, dup := seen[id]; dup {
|
||||
continue
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
out = append(out, itm)
|
||||
}
|
||||
if len(items) < 100 {
|
||||
break
|
||||
}
|
||||
offset += 100
|
||||
}
|
||||
}
|
||||
return out, nil
|
||||
|
||||
Reference in New Issue
Block a user