Created
August 29, 2020 22:15
-
-
Save jloescher/9b8d6c5cc0e685dc0b9df111102a7706 to your computer and use it in GitHub Desktop.
Twitter API Start
This file contains 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
require('dotenv').config(); | |
const Twitter = require('twitter'); | |
let client = new Twitter({ | |
consumer_key: process.env.TWITTER_CONSUMER_KEY, | |
consumer_secret: process.env.TWITTER_CONSUMER_SECRET, | |
bearer_token: process.env.TWITTER_BEARER_TOKEN, | |
}); | |
let params = { | |
q: '#Airbnb', | |
count: 10, | |
result_type: 'recent', | |
lang: 'en', | |
}; | |
client.get('search/tweets', params, function (err, data, response) { | |
if (!err) { | |
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 | |
client.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