Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return total with saved_tracks object #209

Open
Shpigford opened this issue Jan 31, 2020 · 1 comment
Open

Return total with saved_tracks object #209

Shpigford opened this issue Jan 31, 2020 · 1 comment

Comments

@Shpigford
Copy link

Shpigford commented Jan 31, 2020

According to the Spotify docs, the Saved Tracks endpoint has a total key that will tell you how many items there are to page through.

https://developer.spotify.com/documentation/web-api/reference/library/get-users-saved-tracks/

This would be immensely helpful to have returned to know when to stop looping through saved_tracks calls (which frequently return "Too Many Requests" errors).

Here's an example request that shows the total key.

{
  "href": "https://api.spotify.com/v1/me/tracks?offset=1&limit=1",
  "items": [
    {
      "added_at": "2020-01-28T19:51:03Z",
      "track": {
        "album": {
          "album_type": "album",
          "artists": [
            {
              "external_urls": {
                "spotify": "https://open.spotify.com/artist/3vAaWhdBR38Q02ohXqaNHT"
              },
              "href": "https://api.spotify.com/v1/artists/3vAaWhdBR38Q02ohXqaNHT",
              "id": "3vAaWhdBR38Q02ohXqaNHT",
              "name": "The All-American Rejects",
              "type": "artist",
              "uri": "spotify:artist:3vAaWhdBR38Q02ohXqaNHT"
            }
          ],
          "external_urls": {
            "spotify": "https://open.spotify.com/album/0TvOeelcHQXYgPcyQiLhyR"
          },
          "href": "https://api.spotify.com/v1/albums/0TvOeelcHQXYgPcyQiLhyR",
          "id": "0TvOeelcHQXYgPcyQiLhyR",
          "images": [
            {
              "height": 640,
              "url": "https://i.scdn.co/image/ab67616d0000b273eba2acd1d78fc80ed234af0b",
              "width": 640
            },
            {
              "height": 300,
              "url": "https://i.scdn.co/image/ab67616d00001e02eba2acd1d78fc80ed234af0b",
              "width": 300
            },
            {
              "height": 64,
              "url": "https://i.scdn.co/image/ab67616d00004851eba2acd1d78fc80ed234af0b",
              "width": 64
            }
          ],
          "name": "The All-American Rejects",
          "release_date": "2002-10-15",
          "release_date_precision": "day",
          "total_tracks": 11,
          "type": "album",
          "uri": "spotify:album:0TvOeelcHQXYgPcyQiLhyR"
        },
        "artists": [
          {
            "external_urls": {
              "spotify": "https://open.spotify.com/artist/3vAaWhdBR38Q02ohXqaNHT"
            },
            "href": "https://api.spotify.com/v1/artists/3vAaWhdBR38Q02ohXqaNHT",
            "id": "3vAaWhdBR38Q02ohXqaNHT",
            "name": "The All-American Rejects",
            "type": "artist",
            "uri": "spotify:artist:3vAaWhdBR38Q02ohXqaNHT"
          }
        ],
        "disc_number": 1,
        "duration_ms": 300386,
        "explicit": false,
        "external_ids": {
          "isrc": "USDW10200537"
        },
        "external_urls": {
          "spotify": "https://open.spotify.com/track/2vLgTJ8YADmKDvrfkrUCaJ"
        },
        "href": "https://api.spotify.com/v1/tracks/2vLgTJ8YADmKDvrfkrUCaJ",
        "id": "2vLgTJ8YADmKDvrfkrUCaJ",
        "is_local": false,
        "name": "The Last Song",
        "popularity": 46,
        "preview_url": "https://p.scdn.co/mp3-preview/b7ac42620ef1d6e09f7d5537d592f2582a913afd?cid=6048582d0618445a8517216ef827bb0f",
        "track_number": 11,
        "type": "track",
        "uri": "spotify:track:2vLgTJ8YADmKDvrfkrUCaJ"
      }
    }
  ],
  "limit": 1,
  "next": "https://api.spotify.com/v1/me/tracks?offset=2&limit=1",
  "offset": 1,
  "previous": "https://api.spotify.com/v1/me/tracks?offset=0&limit=1",
  "total": 9105
}
@justinperkins
Copy link

Good feature request. Until this gem is forked, updated and merged, you can get around the limitation by doing the following:

# turning this on will return the raw response from Spotify
RSpotify.raw_response=true
raw_tracks_json = rspotify_user_instance.saved_tracks
tracks_hash = JSON.parse(raw_tracks_json)
# now you can access pagination info
puts tracks_hash["next"] # API url to next page, will be nil when no more pages
puts tracks_hash["total"] # total liked tracks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants