[英]Page Twitter's REST API with Node
I am attempting to get a list of tweets from Twitter with a specific hashtag using Node js. 我正在尝试使用Node js从Twitter获取带有特定主题标签的推文列表。 Twitter has is it so that you can only get a maximum of 15 tweets for every request, so I need to make multiple requests if I want a sizable list of tweets.
Twitter就是这样,每个请求最多只能获得15条推文,因此,如果我需要大量推文,则需要发出多个请求。 To let Twitter know that you want the next list of tweets, you need to provide a "max_id" variable, which should hold the minumum id of the list of tweets that you got back from the previous request.
为了让Twitter知道您想要下一个推文列表,您需要提供一个“ max_id”变量,该变量应包含从上一个请求中获得的推文列表的最小ID。 This process is documented here .
此过程在此处记录 。
Here is my attempt at doing this: 这是我这样做的尝试:
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');
I am using the node-twitter-api module to make the requests. 我正在使用node-twitter-api模块发出请求。 This won't work because the outer loop will keep firing off without waiting for the query.
这是行不通的,因为外循环会一直触发而不会等待查询。 Is there a better way to do this?
有一个更好的方法吗?
Twitter lets you request up to 100 tweets at a time. Twitter允许您一次请求多达100条推文。 I added a
count
parameter for this. 我为此添加了一个
count
参数。 You should initiate subsequent requests from your callback. 您应该从回调中发起后续请求。 That way you will serialize your requests.
这样,您将序列化您的请求。
function twitterSearch() {
twitter.search({
q: "#" + hashtag,
result_type: "recent",
max_id: max_id,
count: 100 // MAX RETURNED ITEMS IS 100
},
session.accessToken,
session.accessTokenSecret,
function(error, data, response) {
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;
}
}
// GET MORE TWEETS
if (max_id != '0')
twitterSearch();
}
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.