簡體   English   中英

將 Twitter API Stream 用於多個 ZB73C2D22763D1CE2143A375AZC1D0AD3 帳戶?

[英]using Twitter API Stream for multiple twitter accounts?

我正在做一個項目,其中樹莓派上的音頻文件由來自特定帳戶的推文觸發。

我正在使用 node.js 和 npm 推特 package。 當使用兩個不同的 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  

 });

但它又一次凍結了。

任何建議都會很棒。

您聲明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  

 });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM