Skip to content

Instantly share code, notes, and snippets.

@jloescher
Created August 29, 2020 22:15
Show Gist options
  • Save jloescher/9b8d6c5cc0e685dc0b9df111102a7706 to your computer and use it in GitHub Desktop.
Save jloescher/9b8d6c5cc0e685dc0b9df111102a7706 to your computer and use it in GitHub Desktop.
Twitter API Start
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