简体   繁体   中英

using Twitter API Stream for multiple twitter accounts?

I am working on a project where audio files on a raspberry pi are triggered by tweets from specific accounts.

I am using node.js and the npm twit package. I have been able to get it work when using two different twitter accounts. However, when I try to do it with multiple accounts (15+), it is not working.

My working code is this:

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();

    }

 });

As I mentioend when I add multiple accounts it does not work. It does not throw up an error message instead it just freezes (no reaction to tweets). I thought the problem might be that I only needed either one var stream or one instance of stream.on so I tried this:

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  

 });

But again it just freezes.

Any suggestion would be great.

There's a problem with the way you declare follow list. It should be a comma-separated list of user ids, while in your code you just use comma-operator . Try changing it to something like this:

{ follow: [ userID1, userID2, ... , userID17 ].join(',') }

in your second example of code (which is better in my opinion), so the resulting code would look like this:

var stream = T.stream('statuses/filter', { follow: [ userID1, userID2, ... , userID17 ].join(',') });  

//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  

 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM