【问题标题】:using Twitter API Stream for multiple twitter accounts?将 Twitter API 流用于多个 Twitter 帐户?
【发布时间】:2020-03-06 09:47:07
【问题描述】:

我正在做一个项目,其中树莓派上的音频文件由来自特定帐户的推文触发。

我正在使用 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  

 });

但它又一次冻结了。

任何建议都会很棒。

【问题讨论】:

    标签: javascript node.js api twitter raspberry-pi


    【解决方案1】:

    您声明follow 列表的方式存在问题。它应该是一个以逗号分隔的用户 ID 列表,而在您的代码中您只需使用 comma-operator。试着把它改成这样:

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

    在您的第二个代码示例中(我认为这更好),因此生成的代码如下所示:

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

    【讨论】:

    • 这就是塞巴斯蒂安,非常感谢!
    • 我能问你为什么更喜欢第二个例子吗?
    • 因为你只创建了一个流。代码更干净
    猜你喜欢
    • 2018-07-11
    • 2023-02-15
    • 2011-11-10
    • 2021-06-13
    • 2014-11-27
    • 2011-09-08
    • 2012-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多