【问题标题】:Soundcloud API python issues with linked partitioningSoundcloud API python 问题与链接分区
【发布时间】:2016-11-15 20:37:19
【问题描述】:

关于 Soundcloud API 指南 (https://developers.soundcloud.com/docs/api/guide#pagination) 读取100多条数据的例子如下:

# 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”需要引用而不仅仅是“tracks”。根据 GitHub python soundcloud API wiki,它应该看起来更像这样:

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

我从最后一行删除了缩进(我认为 wiki 上有一个错误,它位于 for 循环中,这对我来说毫无意义)。这适用于第一个循环。但是,这不适用于连续页面,因为找不到“GetNextPartition()”函数。我试过最后一行:

  tracks = tracks.collection.GetNextPartition()

...但没有成功。

也许我把版本搞混了?但我在从这里下载版本后尝试使用 Python 3.4 运行它:https://github.com/soundcloud/soundcloud-python

非常感谢任何帮助!

【问题讨论】:

    标签: python pagination soundcloud


    【解决方案1】:

    对于任何关心的人,我在 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多