Last active
November 4, 2018 05:02
-
-
Save MattSandy/8cad0323ccea0a9e4034ac09d5185580 to your computer and use it in GitHub Desktop.
Scrape Twitter
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 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