2016-11-15 52 views
1

在的SoundCloud API指南(https://developers.soundcloud.com/docs/api/guide#pagination) 閱讀超過100件的數據給出的示例的SoundCloud API的Python的問題如下:帶有鏈接的分區

# 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 

我敢肯定這是錯誤的我發現'tracks.collection'需要引用而不僅僅是'track'。基於GitHub的蟒蛇的SoundCloud API維基應該更像這樣:

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() 

這裏,我從最後一行除去縮進(我認爲這是一個維基的錯誤是在for循環,這使得對我沒有意義)。這適用於第一個循環。但是,這不適用於連續頁面,因爲找不到「GetNextPartition()」函數。我試過最後一行:

tracks = tracks.collection.GetNextPartition() 

...但沒有成功。

也許我正在混合版本?但我試圖在從這裏下載版本後使用Python 3.4運行:https://github.com/soundcloud/soundcloud-python

任何幫助非常感謝!

回答

2

對於任何關心的人,我在SoundCloud開發者論壇上找到了這個解決方案。它從原始情況(搜索曲目)稍微修改以列出我自己的追隨者。訣竅是反覆調用client.get函數,並將先前返回的「users.next_href」作爲指向下一頁結果的請求。萬歲!

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