David A. Ridley
David A. Ridley

Reputation: 41

Soundcloud API python issues with linked partitioning

On the Soundcloud API guide (https://developers.soundcloud.com/docs/api/guide#pagination) the example given for reading more than 100 piece of data is as follows:

# get first 100 tracks
tracks = client.get('/tracks', order='created_at', limit=page_size)
for track in tracks:
    print track.title

# start paging through results, 100 at a time
tracks = client.get('/tracks', order='created_at', limit=page_size,
                    linked_partitioning=1)
for track in tracks:
    print track.title

I'm pretty certain this is wrong as I found that 'tracks.collection' needs referencing rather than just 'tracks'. Based on the GitHub python soundcloud API wiki it should look more like this:

tracks = client.get('/tracks', order='created_at',limit=10,linked_partitioning=1)
while tracks.collection != None:
 for track in tracks.collection:
  print(track.playback_count)
 tracks = tracks.GetNextPartition()

Where I have removed the indent from the last line (I think there is an error on the wiki it is within the for loop which makes no sense to me). This works for the first loop. However, this doesn't work for successive pages because the "GetNextPartition()" function is not found. I've tried the last line as:

  tracks = tracks.collection.GetNextPartition()

...but no success.

Maybe I'm getting versions mixed up? But I'm trying to run this with Python 3.4 after downloading the version from here: https://github.com/soundcloud/soundcloud-python

Any help much appreciated!

Upvotes: 1

Views: 392

Answers (1)

David A. Ridley
David A. Ridley

Reputation: 41

For anyone that cares, I found this solution on the SoundCloud developer forum. It is slightly modified from the original case (searching for tracks) to list my own followers. The trick is to call the client.get function repeatedly, passing the previously returned "users.next_href" as the request that points to the next page of results. Hooray!

pgsize=200
c=1
me = client.get('/me')
#first call to get a page of followers
users = client.get('/users/%d/followers' % me.id, limit=pgsize, order='id',
                   linked_partitioning=1)
for user in users.collection:
    print(c,user.username)
    c=c+1
#linked_partitioning means .next_href exists
while users.next_href != None:
 #pass the contents of users.next_href that contains 'cursor=' to
 #locate next page of results
 users = client.get(users.next_href, limit=pgsize, order='id',
                    linked_partitioning=1)
 for user in users.collection:
     print(c,user.username)
     c=c+1

Upvotes: 3

Related Questions