Skip to content

Instantly share code, notes, and snippets.

@bmorelli25
Created May 28, 2017 15:13
Show Gist options
  • Save bmorelli25/d61c937b162281bd395e854d9d707b7c to your computer and use it in GitHub Desktop.
Save bmorelli25/d61c937b162281bd395e854d9d707b7c to your computer and use it in GitHub Desktop.
Twitter Favorite Bot
var Twitter = require('twitter');
var config = require('./config.js');
var T = new Twitter(config);
// Set up your search parameters
var params = {
q: '#nodejs',
count: 10,
result_type: 'recent',
lang: 'en'
}
// Initiate your search using the above paramaters
T.get('search/tweets', params, function(err, data, response) {
// If there is no error, proceed
if(!err){
// Loop through the returned tweets
for(let i = 0; i < data.statuses.length; i++){
// Get the tweet Id from the returned data
let id = { id: data.statuses[i].id_str }
// Try to Favorite the selected Tweet
T.post('favorites/create', id, function(err, response){
// If the favorite fails, log the error message
if(err){
console.log(err[0].message);
}
// If the favorite is successful, log the url of the tweet
else{
let username = response.user.screen_name;
let tweetId = response.id_str;
console.log('Favorited: ', `https://twitter.com/${username}/status/${tweetId}`)
}
});
}
} else {
console.log(err);
}
})
@ozgurrgul
Copy link

Instead of for loop you may use async.eachSeries function from async library because you are making asynchronous calls in synchronized block.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment