Soundcloud API寻呼-8000的偏移限制是否意味着您可以'I don’我无法得到一份超过8000长的

Soundcloud API Pagination - does the offset limit of 8000 mean you can't get the entirety of a list over 8000 in length?

本文关键字:don 8000 长的 一份 -8000 API 寻呼 是否 意味着 Soundcloud      更新时间:2023-09-26

我正试图弄清楚如何通过分页检索一个包含60k多名追随者的列表。现在,我就是这样做的(我想这在语法上可能会更好):

var array = [];
var page_length = 200;
var num_pages = 40;
var offset = 0;   
for (i=0; i< num_pages; i++) {
     SC.get("/users/" + id + "/followers", {limit: page_length, offset: offset},
          function(followers) {
               for (j=0; j < page_length; j++) {
                    array.push(followers[j]);
               }
               if(array[array.length-1] != undefined && array.length == page_length * num_pages) {
                    //success    
               }
          }
          );
    offset+=page_length;
}

根据API文档,我将page_length设置为最大值200。如果我将num_pages增加到例如45,则该迭代的结果followers[]数组只包含未定义的记录,因为num_pages*page_length将超过8000偏移量限制。

那么,有什么方法可以将8000多条记录放入这个阵列中吗?

您必须注意,SoundCloud刚刚"更新"了他们的API,3月3日之后,您将无法使用带有"偏移量"参数的API。您必须更改代码才能在将来正常工作。

因此,一开始,SoundCloud API默认情况下将限制设置为50,您可以将该限制设置为200(最大)。

第二件事是,在你发送请求后,你的响应通常包含一个"nexthref"光标,你必须调用它才能获得下一个,例如200个关注者。SoundCloud现在改变了这一点。如果你不在你的URL中添加"linked_divisioning"参数,你只会得到前200个关注者。如果添加参数,则会得到next_href

我不明白你为什么把num_pages设置为40?在你的情况下,我会召集200名追随者,把他们放在我的阵列中。然后使用next_href-光标获取接下来的200个关注者(依此类推…)。如果响应中没有next_href,则没有任何关注者。

在JavaScript中,我认为它会是这样的:

var page_size = 200;
SC.get("/users/" + id + "/followers", { limit: page_size, linked_partitioning: 1 }, function(followers) {
  // do your stuff here with every 200 followers (as long as there are followers)
});

我知道我做这件事有点晚了,但我已经设法通过点击"下一步"按钮来让它工作,就像这样:

var nextCursor;
function next() {
  SC.get('/users/' + userId + '/followers', {
    limit: 200,
    linked_partitioning: 1,
    cursor: nextCursor
  }).then(function(followers) {
    var nextHref = followers.next_href;
    nextCursor = nextHref.substr(nextHref.length - 13);
    $(followers.collection).each(function(i) {
      console.log(followers.collection[i])
    });
  });
}
$("#next-btn").click(function() {
  next();
});

我现在正在努力改进这一点,这样点击"搜索"按钮就会返回所有关注者。

我得到的最接近的方法是将页面限制设置为100,将关注者总数除以100,并将其存储在一个变量中(这样我就可以在循环中增加1——我知道这并不理想,但暂时有效),然后将GET请求封装在while循环中,其中"x"小于关注者总数。但是,这只返回结果的第一页,因为"nextCursor"变量永远不会更新。

有人知道这是怎么可能的吗?

var userId;
var totalFollowers;
function getUser() {
  SC.get('/users/' + userName, {}).then(function(user) {
    userId = user.id;
    totalFollowers = user.followers_count / 100;
  })
}
function getFollowers() {
  var x = 1;
  while (x < totalFollowers) {
    var nextCursor;
    SC.get('/users/' + userId + '/followers', {
      limit: 100,
      linked_partitioning: 1,
      cursor: nextCursor
    }).then(function(followers) {
      $(followers.collection).each(function(i) {
        var nextHref = followers.next_href;
        nextCursor = nextHref.substr(nextHref.length - 13);
        console.log(nextCursor)
      });
    });
    x++;
  }
}