I'm working with Twit to get a nice wrapper around the Twitter API. I have a cron to get all the tweet on a particular hashtag. It increment the counter everytime there's a new tweet, and at the end of the period, save it in to the database (MongoDB). Only problem is, it always return me 0.
Here is the code
new cronJob('00 */5 * * * *', function(){ // start parsing 5mn after call, and every 5mn then
var stream = T.stream('statuses/filter', { track: 'hashtag' })
var counter = 0;
var date = new Date();
var collection = client.collection("TweetsNumber");
stream.on('tweet', function (tweet) {
console.log(tweet);
counter += 1;
})
collection.insert({Date: date, CrawledTweets: counter, Channel: "someChannel"});
console.log(counter + " tweets saved in DB");
}, null, true, "Europe/Paris");
According to the doc, the "stream.on" method is called everytime there's a new tweets. I use some trending topic to be sure to have data, but it's like it is never called, and I really don't know why.
Hope you can help. Have a great day !
EDIT: T is already create in another part of the program, and with other functionnalities, it is working. Same for client, which is my db.
EDIT: Thanks to Shodan, it works now, see the github issue. Thanks a lot !
Are the tweets logged to your console?
If yes, then it is not a twit problem, since it exactly does what you told it to.
As I read your code correctly, you create a cronjob, which fires once every 5 minutes.
client.collection("TweetNumbers"), with the local variable counter having a value of 0console.log(counter + " tweets saved in DB");, with the local variable counter having a value of 0The function then exits, and is fresh started in 5 minutes.
stream.on should continue to fire when ever a tweet comes along for the next 5 minutes and increase the counter, BUT the counter is never used again by collection.insert and the second console.log.
This is because you restart the function creating new local variables for all the stuff and logging the initial values again.
You set var counter = 0 and then immediately console.log() it, which means the 'tweet' event never gets a chance to fire and increase counter. You might want to do this:
new cronJob('00 */5 * * * *', function(){ // start parsing 5mn after call, and every 5mn then var stream = T.stream('statuses/filter', { track: 'hashtag' }) var counter = 0; var date = new Date(); var collection = client.collection("TweetsNumber");
stream.on('tweet', function (tweet) {
console.log(tweet);
collection.insert({Date: date, CrawledTweets: counter, Channel: "someChannel"});
counter += 1;
console.log(counter + " tweets saved in DB");
})
}, null, true, "Europe/Paris");