Esempio n. 1
0
	'get' : function(query,callback){
	  var url = "https://api.twitter.com/1.1/search/tweets.json";
	  t.get(url, query, function(err, data) {
		  console.log(query);
	    if (typeof(data) != 'undefined' && data.statuses.length) {
	      res = data.statuses;
	      counter = 0;
	      res.forEach(function (item) {
	        Twit.save(item, function(err, type) {
	          counter++;
	          if (err) {
	            console.log('Fatal error on saving tweet.');
	            console.log(err);
	            process.exit(0);
	          }
	          if (counter == res.length) {
	            console.log('Imported: ' + counter + ' tweets.');
	            if(callback)
	            	callback(res);
	            else{
	            	console.log('no callback');
	            	process.exit(1);
	            }
	          }
	        });
	      });
	    }
	    else {
	      console.log('No fresh data available.');
	      process.exit(1);
	    }
	  });
	},
Esempio n. 2
0
 function fetch_tweets(max_id) {
     // The ntwitter library helper method is not yet up to date
     // with API version 1.1, so we resort to just calling .get().
     twit
         .get(
             "/statuses/mentions_timeline.json",
             {
                 "count": 100, 
                 "include_rts": false, 
                 "include_entities": true,
                 "contributor_details": false,
                 "since_id": max_id
             },
             function (err, data) {
                 if (err) {
                     throw err;
                 } else {
                     tcnt = data.length
                     if (tcnt == 0)
                         process.exit();
                     console.log("found " + tcnt + " new tweets");
                     data.map(save_tweet);
                 }
             });
 }
Esempio n. 3
0
    models.Post.where('foreign_id', id).count(function (err, count) {
        if (err) {
            console.error(err);
            nextId(true);
            return;
        }

        if (count > 0) {
            console.warn("Tweet existing", id);
            alreadyHaveCount++;
            nextId(true);
            return;
        }

        var params = {'include_entities': true, 'include_my_retweet': false, 'trim_user': false, 'id': id};
        console.log("Making request, id = %s", id);
        twit.get('/statuses/show.json', params, function(err, data) {
            if (err) {
                if ((err.statusCode === 500 || err.statusCode === 503) && errorCount < 3) {
                    console.error("Twitter fetch error, retrying", err);
                    errorCount++;
                    setTimeout(processIds, settings.TWITTER_REQUEST_INTERVAL);
                }
                else if (err.statusCode === 404) {
                    console.warn("Tweet not found", err);
                    missingCount++;
                    nextId();
                }
                else if (err.statusCode === 403) {
                    console.warn("Tweet inaccessible", err);
                    missingCount++;
                    nextId();
                }
                else {
                    console.error("Twitter fetch error", err);
                    process.exit(1);
                }
                return;
            }

            if (!regexMatch(data)) {
                console.warn("Tweet skipped", util.inspect(data, false, null));
                withoutQueryCount++;
                nextId();
                return;
            }

            models.Post.storeTweet(data, 'import', function (err, tweet) {
                if (err) {
                    console.error(err);
                    nextId();
                    return;
                }

                newCount++;
                nextId();
            });
        });
    });
Esempio n. 4
0
function loadtweets() {
    var params = {'include_entities': true, 'count': 100, 'max_id': maxId, 'q': settings.TWITTER_QUERY.join(' OR ') + (date ? ' until:' + date.format('YYYY-MM-DD') : '')};
    console.log("Making request, max_id = %s, date = %s", maxId, date);
    twit.get('/search/tweets.json', params, function(err, data) {
        if (err) {
            if ((err.statusCode === 500 || err.statusCode === 503) && errorCount < 3) {
                console.error("Twitter fetch error, retrying", err);
                errorCount++;
                setTimeout(loadtweets, LOAD_INTERVAL);
            }
            else {
                console.error("Twitter fetch error", err);
                process.exit(1);
            }
            return;
        }

        errorCount = 0;

        if (data.statuses.length === 0) {
            console.log("%s new tweets fetched overall", count);
            next();
            return;
        }

        async.forEach(data.statuses, function (tweet, cb) {
            models.Post.storeTweet(tweet, 'search', function (err, tweet) {
                if (err) {
                    console.error(err);
                    return;
                }

                if (tweet) {
                    count++;
                }
            });

            // We handle error independently
            cb(null);
        }, function (err) {
            console.log("%s new tweets fetched overall", count);

            var max_id_match = MAX_ID_REGEXP.exec(data.search_metadata.next_results);
            if (!max_id_match) {
                next();
                return;
            }

            maxId = max_id_match[1];

            setTimeout(loadtweets, LOAD_INTERVAL);
        });
    });
}
Esempio n. 5
0
app.get('/twitter/callback', function (req, res) {
	if (req.session.oauth.token &&
		req.session.oauth.token_secret &&
		req.session.oauth.access_token &&
		req.session.oauth.access_token_secret) {

		console.log("=2======================================================")
		console.log('oauth.token: ' + req.session.oauth.token);
		console.log('oauth.token_secret: ' + req.session.oauth.token_secret);
		console.log('oauth_access_token: ' + req.session.oauth.access_token);
		console.log('oauth_access_token_secret: ' + req.session.oauth.access_token_secret);
		console.log("=2======================================================")

		twit = new twitter({
			consumer_key: 'JGIhyIMHSCyr90oZjvxRvg',
			consumer_secret: 'ReDJ3Wn2xIBS6nvNIW1tzT5rc861jCWkjtsnWMsDCk',
			access_token_key: req.session.oauth.access_token,
			access_token_secret: req.session.oauth.access_token_secret
		});

		twit.get('/statuses/home_timeline.json', {'count': 20, 'include_entities': true}, function(err, result) {
			console.log(result);
		});
	} else {
		if (req.session.oauth) {
			req.session.oauth.verifier = req.query.oauth_verifier;
			var oauth = req.session.oauth;
			oa.getOAuthAccessToken(oauth.token,oauth.token_secret,oauth.verifier, 
			function(error, oauth_access_token, oauth_access_token_secret, results){
				if (error){
					console.log(error);
					res.send("yeah something broke.");
				} else {
					req.session.oauth.access_token = oauth_access_token;
					req.session.oauth.access_token_secret = oauth_access_token_secret;

					console.log("=======================================================")
					console.log('oauth.token: ' + req.session.oauth.token);
					console.log('oauth.token_secret: ' + req.session.oauth.token_secret);
					console.log('oauth_access_token: ' + oauth_access_token);
					console.log('oauth_access_token_secret: ' + oauth_access_token_secret);
					console.log("=======================================================")
					console.log(results);
					console.log("=======================================================")
					//res.send(results.screen_name + "<br/>worked. nice one.");
					//res.sendfile(__dirname + '/index.html');
					res.redirect('http://127.0.0.1:8888');
				}
			});
		} else {
			next(new Error("you're not supposed to be here."));
		}
	}
});
Esempio n. 6
0
app.get('/get', function(req, res) {
    var twit = new twitter({
    	consumer_key: 'JGIhyIMHSCyr90oZjvxRvg',
    	consumer_secret: 'ReDJ3Wn2xIBS6nvNIW1tzT5rc861jCWkjtsnWMsDCk',
    	access_token_key: req.session.oauth.access_token,
    	access_token_secret: req.session.oauth.access_token_secret
    });
    
    twit.get('/statuses/home_timeline.json', {'count': 200, 'include_entities': true}, function(err, result) {
    	console.log(result);
        res.json(result);
    });
});
Esempio n. 7
0
// twitter_status: pulls in the statuses of the user_name passed into the function
// default tweet limit is 5, set in the config.js file
function twitter_status(user_name, config, socket) {

	// I'm fairly certain the count option isn't working
  var twit_options = {
  	count: config.seed_tweet_limit,
  	screen_name: user_name,
  	trim_user: true
  }
  twit.get('/statuses/user_timeline.json', twit_options, function(stuff, data){
	if (typeof(socket) != undefined){
	  socket.emit('artist_tweet', {artist_data: data});
	}
	else console.log('Socket error!');
  });
}
Esempio n. 8
0
function _getTweets(params) {
	console.log("_getTweets");
	if(!_.isNull(_since_id)) params.since_id = _since_id;
	
	T.get('/lists/statuses.json', params, function(err, reply) {
		
		// if there's an error, return false
		if(err) {
			console.error("Error getting tweets from Twitter API", err);
			return false;
		}
		
		// twitter response with since_id seems to return the since_id, so remove it to avoid duplication in db
		// if(reply.length > 0 && reply[0].id === _since_id) {
		// 			reply = reply.slice(1);
		// 		}
		
		// add extra info
		_.each(reply, function(tweet) {
			tweet.type = "tweet";
			tweet.party = params.slug;
			tweet.twitter_list_screen_name = params.owner_screen_name;
			tweet.twitter_list_slug = params.slug;
			tweet._id = "" + tweet.id;
			// console.log("Fetched tweet from API. ID:", tweet.id);
		});
		
		// console.log(reply);
		
		// save to db
		// db.save(reply, function(err, resp) {
		db.bulk({ "docs": reply }, {}, function(err, resp) {
			if(err) {
				console.error("Error saving tweets to db", err);
			} else {
				console.log('saved tweets');	
			}
		});
		
		// update _since_id based on response
		/*console.log(params.slug, reply);
		var max = _.max(reply, function(tweet){ console.log(tweet.id); return tweet.id; });
		console.log("max: ", max.id);
		if(max.id) _since_id = max.id;
		console.log("_since_id: ", _since_id);
		*/
	});
}
Esempio n. 9
0
exports.restGet = function(req, res) {

  var twit = new Twitter({
    consumer_key: configs.TWITTER_CONSUMER_KEY,
    consumer_secret: configs.TWITTER_CONSUMER_SECRET,
    access_token_key: req.user.token,
    access_token_secret: req.user.tokenSecret
  });

  var url = req.path.replace(configs.API_PATH, '');
  console.log('Get request, url:', url, ', params:', req.query);
  twit.get(url, req.query, function(err, data) {
    if (err) {
      res.status(err.statusCode).send(err);
      return;
    }
    res.send(data || {});
  });
};
io.sockets.on('connection', function (socket) {
    util.log('A user connected. current sockets: ' + ++clientCount);

    twit.get('/statuses/user_timeline.json', {'screen_name': 'groovesalad', 'count': '1'}, function(err, data) {
        if (err) {
            util.log('Error received from Twitter REST API: ' + err);
        }
        if (util.isArray(data)) {
            //Twitter REST API returns array of tweets, get first one.
            data = data[0];
            if ('text' in data) {
                //push tweeted text to a specific socket.
                socket.emit('new_song', data.text);
            }
        }
    });

    socket.on('disconnect', function () {
        util.log('A user disconnected. current sockets:' + --clientCount);
    });
});
Esempio n. 11
0
function lookupUser(userId, callback) {
    twit.get('/users/lookup.json', {
        user_id: userId
    }, callback);
}