Created
May 28, 2017 15:13
-
-
Save bmorelli25/d61c937b162281bd395e854d9d707b7c to your computer and use it in GitHub Desktop.
Twitter Favorite Bot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Instead of
for
loop you may useasync.eachSeries
function fromasync
library because you are making asynchronous calls in synchronized block.