0
我試圖從Twitter獲取推文列表,使用Node js的特定主題標籤。 Twitter是這樣的,所以每次請求最多隻能獲得15條推文,所以如果我想要一個可觀的推文列表,我需要提出多個請求。爲了讓Twitter知道你想要下一個推文列表,你需要提供一個「max_id」變量,它應該包含你從前一個請求中獲得的推文列表的最小編號。這個過程記錄在here。頁面Twitter的REST API和節點
這是我在做這樣的嘗試:
var hashtag = "thisisahashtag"; // hashtag that we are looking for
var max_id = '';
do {
twitter.search({
q: "#" + hashtag,
result_type: "recent",
max_id: max_id
},
session.accessToken,
session.accessTokenSecret,
function(error, data, response) { // callback
// get the ids of all the tweets from one response and do comparison for smallest one
for(var i = 0; i < data.statuses.length; i++) {
var id = data.statuses[i].id;
if(max_id == '' || id < parseInt(max_id)) {
max_id = id;
}
}
// do something with the data...
}
)
} while(max_id != '0');
我使用的節點Twitter的API模塊做出請求。這是行不通的,因爲外層循環將不會等待查詢而關閉。有一個更好的方法嗎?