Reputation: 1195
I'm using MeteorJS w/the Twitter REST API. What I'm attempting to do is retrieve a bunch of tweets w/a specific hash tag. I'm able to retrieve the tweets but my question is why do I keep retrieving the same tweets?
The hashtags I'm searching are are relatively popular, so I would expect there to be a bunch of random tweets, every time I restart MeteorJS. Not sure exactly why this is happening but my guess is that since I'm requesting the same hashtag w/the same authentication information, Twitter is just retrieving a certain set of tweets rather than retrieving different tweets w/the specific hashtag every single time I restart Meteor.
Not sure if this is exactly what's happening but that's my educated guess. If this is what is happening, is there any way around it such that I retrieve random tweets every time w/a specific hash tag?
Here's my code. Forgot that would be helpful.
if (Meteor.isServer) {
Meteor.startup(function () {
if (Tweets.find().count()==0) {
var Twit = Meteor.npmRequire('twit');
var T = new Twit({
consumer_key: '', // API key
consumer_secret: '', // API secret
access_token: '',
access_token_secret: ''
});
var Tget = Meteor.wrapAsync(T.get,T);
var result = Tget('search/tweets', {q:'#UCLA',count:10});
for (var i = 0; i < 10; i++) {
var temp = [
{'title':result.statuses[i].user.name,'handle':result.statuses[i].user.screen_name,'picture':result.statuses[i].user.profile_image_url,'tweet':result.statuses[i].text}];
Tweets.insert(temp[0]);
}
}
});
}
And Tweets is a collection I made outside of the client and server code :
Tweets = new Mongo.Collection("tweets");
Upvotes: 0
Views: 178
Reputation: 41
Just throwing this out there. Maybe you are getting the same results because you are making the same search.
You save the results to the DB each time right? Are you making sure to exclude the previous results?
You should be able to add a parameter to tell the search to return tweets that only happened after a certain time.
Upvotes: 0
Reputation: 6020
Your MongoDB should persist, so you have to do Tweets.remove({})
outside of the
if (Tweets.find().count()==0) {
block or remove the condition.
Also, consider using upsert
, so that you don't have to clear the entire database every time. (ref)
Upvotes: 0
Reputation: 66
on first run your Tweets Collection is empty you get inside of
if (Tweets.find().count()==0) {
then you insert your posts/tweets and won´t get into it again…as @FullStack says: you MongoDB persists. ;-)
Upvotes: 0