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?
我正试图弄清楚如何通过分页检索一个包含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++;
}
}
- 元素在我的代码中不会.fadeTo.Don'I don’我不知道;s错误的JavaScript、JQuery、H
- Don'不允许将焦点集中在自动完成的选择上
- javascript如果图像不存在don't加载它
- append元素don'不要在提交时出现
- AngularJS指令部分应用的函数don'不起作用
- Don'我不明白为什么我的setInterval+jQuery;不起作用
- 我没有'I don’我不理解for在循环中的用途
- jQuery 1.7.2:.on()在一个页面上工作;Don’别再干别的了
- AngularJs|Don'在成功加载所有指令htmls之前,不显示页面
- Codeacademy Javascript..好的!/输出正确,但codeacademy没有;I don’我不喜欢
- Don't工作警报代码jQuery
- 如何实施“;Don'不要再给我看了”;在GWT中
- AngularJS routeprovider赢得'Don’我什么都不做
- Mediawiki皮肤创建-Javascript don'Don’t get load
- Dropit下拉列表没有'Don’不要躲在老鼠屋
- 链接don'由于Javascript悬停效果,无法在移动设备上工作
- Jquery live()绑定没有'I don’我对某些元素不感兴趣
- Ng点击don´不起作用
- Office加载项don't与“;asp:”;
- Soundcloud API寻呼-8000的偏移限制是否意味着您可以'I don’我无法得到一份超过8000长的