Skip to content

Instantly share code, notes, and snippets.

@MattSandy
Last active November 4, 2018 05:02
Show Gist options
  • Select an option

  • Save MattSandy/8cad0323ccea0a9e4034ac09d5185580 to your computer and use it in GitHub Desktop.

Select an option

Save MattSandy/8cad0323ccea0a9e4034ac09d5185580 to your computer and use it in GitHub Desktop.
Scrape Twitter
var Twitter = require('twitter');
var fs = require('fs');
//https://apps.twitter.com/
var client = new Twitter({
consumer_key: '',
consumer_secret: '',
access_token_key: '',
access_token_secret: ''
});
dl(0);
function dl(iteration,max_id = null) {
var params = {
q: '#queen OR #freddiemercury OR #BohemianRhapsody',
count: 100,
result_type: 'recent',
max_id: max_id
};
client.get('/search/tweets.json', params, function(error, tweets, response) {
if (!error) {
//Show progress
console.log("=====================");
console.log("== " + iteration + " ==");
console.log("=====================");
tweets.statuses.forEach(function(tweet) {
//Write JSON
var line = JSON.stringify(tweet) + "\n";
fs.appendFile('tweets.json', line, function (err) {
//console.log(err);
});
});
if(iteration<10000) {
if(tweets.statuses.length > 0) {
var id_str = tweets.statuses[tweets.statuses.length - 1].id_str;
setTimeout(function(iteration,id_str) {
dl(iteration + 1, id_str);
}, 5000, iteration, id_str);
}
}
} else {
console.log(error);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment