Created
February 29, 2016 13:58
-
-
Save desflynn/6d5a1aaace8fc983e3f3 to your computer and use it in GitHub Desktop.
Saves a stream of tweets from Ireland to a text file
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
var Twit = require('twit') | |
var moment = require('moment'); | |
var fs = require('fs'); | |
var T = new Twit({ | |
consumer_key: '...', | |
consumer_secret: '...', | |
access_token: '...', | |
access_token_secret: '...', | |
timeout_ms: 60*1000, // optional HTTP request timeout to apply to all requests. | |
}) | |
var results = []; | |
//Filter using a box within two geolocations | |
var ireland = ['-10.7048275','51.33', '-5.4753353','55.2573070']; | |
var stream = T.stream('statuses/filter', { locations: ireland }) | |
//Record start date and run an "update" message on an interval | |
var startDate = Date.now(); | |
setInterval(function() | |
{ | |
var elapsed = (Date.now() - startDate)/1000 | |
if(elapsed < 60) | |
{ | |
var elapsedText = "Time elapsed: " + elapsed.toFixed(0) + " seconds." | |
} | |
else if(elapsed >= 60) | |
{ | |
var elapsedText = "Time elapsed: " + (elapsed/60).toFixed(0) + " minutes and " + (elapsed%60).toFixed(0) + " seconds." | |
} | |
console.log("############## " + elapsedText + " Total Tweets: ", results.length, ". Tweets/hr: ", (results.length/elapsed*3600).toFixed(2) + " ##############"); | |
}, 10000); | |
stream.on('tweet', function (status) { | |
var result = | |
{ | |
statusCreatedDate: status.created_at, | |
statusText: status.text, | |
userId: status.user.id, | |
userScreenName: '@' + status.user.screen_name, | |
userName: status.user.name, | |
userLocation: status.user.location, | |
userDescription: status.user.description, | |
userFollowers: status.user.followers_count, | |
userFollowing: status.user.friends_count | |
} | |
results.push(result); | |
console.log('Tweet # ' + results.length + '#:' + JSON.stringify(result)); | |
//Write Results to file | |
fs.writeFile('results.txt', JSON.stringify(results,null,4), function (err) { | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment