How to update Twit stream? (Npm module and Twitter API)

I want to update a Twit stream.

I have a Twitter stream made with the twpm Twpm module (you can find it here: https://github.com/ttezel/twit ).

Here is my code:

Researches.find().observeChanges({
    added: function(){
        hashArray = Researches.find().fetch();
        hashCount = Researches.find().count();
        for(i=0; i<hashCount; i++){
            hashArray[i]= hashArray[i].hashtag;
        }
    }
});

stream = T.stream('statuses/filter', {track: hashArray});

//Launch stream
stream.on('tweet', Meteor.bindEnvironment(function(tweet) {
    //Get the hashtag of the tweet
    tweetText = tweet.text;
    tweetText = tweetText.toLowerCase();

    //Get the hashtag of the current tweet
    for(i=0; i<hashCount; i++){
        var hashCompare = hashArray[i];
        hashCompare = hashCompare.toLowerCase();
        var isInString = tweetText.search(hashCompare);
        if(isInString>=0)
            goodHash = hashArray[i];
    }

    // Get the tweet informations
    tweetToInsert = {
        user: tweet.user.screen_name, 
        tweet: tweet.text, 
        picture: tweet.user.profile_image_url, 
        date: new Date().getTime(),
        hashtag: goodHash
    };

    matchTweet = Tweets.findOne({tweet:tweetToInsert.tweet});
    //Store tweets
    if(matchTweet || (lastTweet.user == tweetToInsert.user) || (lastTweet.tweet == tweetToInsert.tweet)){

    } else {
        console.log(tweetToInsert.tweet);
        Tweets.insert(tweetToInsert, function(error) {
            if(error)
                console.log(error);
        });
    }
    //Store last tweet
    lastTweet = {
        user: tweetToInsert.user,
        tweet: tweetToInsert.tweet
    }

    //Delete tweet overflow
    nbTweet = Tweets.find({hashtag: goodHash}).count();
    tweetToDelete = nbTweet-25;
    if(nbTweet>25){
        for(i=0; i<tweetToDelete;i++){
            idDelete = Tweets.findOne({hashtag: goodHash});
            Tweets.remove(idDelete._id);
        }
    }
}));

As you can see, I have an observation in my research collection with which I created an array with all the hashtag. Then I made my stream using this array to track all these hashtags.

Now here is my problem. When I had a new hashtag of my collection, my array updated itself with a new hashtag and that’s good. The problem is that the thread is not updating itself.

What i tried

I tried using the .stop () stream provided by the Twit documentation (this works great), but when I tried to restart it using .start (), it did not work.

, :

Researches.find().observeChanges({
    added: function(){
        hashArray = Researches.find().fetch();
        hashCount = Researches.find().count();
        for(i=0; i<hashCount; i++){
            hashArray[i]= hashArray[i].hashtag;
        }
        if(stream){
            stream.stop();
            stream.start();
        }
    }
});

, , / Twit , , .

+4
1

github : https://github.com/ttezel/twit/issues/90#issuecomment-41247402

, .

var Twit = require('twit');
var twit = new Twit(config);
var stream1 = twit.stream('statuses/filter', { track: [ '#yolo' ] });

// ... some time passes ...

 // initiate a new streaming connection with our updated track list
var stream2 = twit.stream('statuses/filter', { track: [ '#yolo', '#fun' ] });

stream2.once('connected', function (res) {
    console.log('second stream connected')
    // stop the first stream ASAP so twitter doesn't block us
    stream1.stop();

    stream2.on('tweet', function (tweet) {
        // handle tweets
    });
});
0

Source: https://habr.com/ru/post/1533012/


All Articles