我正在一个项目中,树莓派上的音频文件由来自特定帐户的推文触发。
我正在使用node.js和npm twit软件包。使用两个不同的twitter帐户时,我已经能够使其工作。但是,当我尝试使用多个帐户(15个以上)进行操作时,它不起作用。
我的工作代码是这样
var userID1 = 'XXXXXXX'; //userID of first account
var userID2 = 'XXXXXXX'; //UserdIp of Second account
//Following code set up stream just for USERID1
var stream = T.stream('statuses/filter',{ follow: ( userID1 ) });
stream.on('tweet',function (tweet) {
// two variables to store tweet time and actual tweet
var tweetTime = new Date().toLocaleString();
var printTweet = tweet.text;
if (tweet.user.id == userID1 ) {
printTweet.toString()//converts printTweet to String
console.log(printTweet) //prints tweet as variable
console.log("Tweet has been received from UserID1 + tweetTime");
audio();
}
});
//Following code set up stream just for USERID2
var stream = T.stream('statuses/filter',{ follow: ( userID2 ) });
stream.on('tweet',function (tweet) {
// two variables to store tweet time and actual tweet
var tweetTime2 = new Date().toLocaleString();
var printTweet2 = tweet.text;
if(tweet.user.id == userID2 ) {
printTweet2.toString()//converts printTweet to String
console.log(printTweet2) //prints tweet as variable
console.log("Tweet has been received from UserID2" + tweetTime2);
audio();
}
});
正如我提到的,当我添加多个帐户时,它不起作用。
它不会抛出错误消息,而是会冻结(对鸣叫无反应)。我以为问题可能是我只需要一个var stream
或一个stream.on
的实例,所以我尝试了这个:
var userID1 = 'XXXXXXX'; //userID of first account
var userID2 = 'XXXXXXX'; //UserdID of Second account
var userID3 = 'xxxxxxx'; //user ID pf third account
//I then have userID of another 15 accounts.
//Following code set up stream just for all user IDs
var stream = T.stream('statuses/filter',{ follow: ( userID1,userID2,****15 more userID***** ) });
//just one stream.on for all followed twitter accounts
stream.on('tweet',function (tweet) {
//conditional for userID1
if (tweet.user.id == userID1 ) {
// two variables to store tweet time and actual tweet
var tweetTime = new Date().toLocaleString();
var printTweet = tweet.text;
printTweet.toString()//converts printTweet to String
console.log(printTweet) //prints tweet as variable
console.log("Tweet has been received from UserID1 + tweetTime");
audio();
}
//conditional for userID2
if(tweet.user.id == userID2 ) {
// two variables to store tweet time and actual tweet
var tweetTime = new Date().toLocaleString();
var printTweet = tweet.text;
printTweet2.toString()//converts printTweet to String
console.log(printTweet2) //prints tweet as variable
console.log("Tweet has been received from UserID2" + tweetTime2);
audio();
}
//same conditional above done 15 more times for other accounts
});
但是它再次冻结了。
任何建议都会很棒。