Last active
August 22, 2018 21:21
-
-
Save jacobrosenthal/044a9b911251bc31d4805fd733e6837c to your computer and use it in GitHub Desktop.
twitter noisy users
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
// https://developer.twitter.com/en/docs/tweets/timelines/api-reference/get-statuses-home_timeline.html | |
// note, twitter is super stingy and this code requires like 5 api requests by default, ie 1/3 of your 15 min max | |
// ie you can run this script as many as 3 times before youll get api limited for another 15 minutes | |
var Twitter = require('twitter-lite'); | |
var client = new Twitter({ | |
consumer_key: process.env.TWITTER_CONSUMER_KEY, | |
consumer_secret: process.env.TWITTER_CONSUMER_SECRET, | |
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY, | |
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET | |
}); | |
var printTweetsPerDay = function(tweets){ | |
var byday={}; | |
for (var i = 0; i < tweets.length; i++) { | |
var d = new Date(tweets[i]['created_at'].replace("+0000 ", "") + " UTC"); | |
var dateString = d.toString().substring(0,15); | |
byday[dateString] = byday[dateString] || []; | |
byday[dateString].push(tweets[i]); | |
} | |
Object.keys(byday).forEach(function(key,index) { | |
console.log(key, ": ", byday[key].length); | |
}); | |
return tweets; | |
} | |
var sortTweetsByNoisyUser = function(tweets){ | |
// shame its associative, but cant think of a better solution | |
// https://stackoverflow.com/questions/15052702/count-unique-elements-in-array-without-sorting | |
var counts = {}; | |
for (var i = 0; i < tweets.length; i++) { | |
counts[tweets[i].user.screen_name] = 1 + (counts[tweets[i].user.screen_name] || 0); | |
} | |
//lets get back to an array we can sort | |
var countsArray = []; | |
for(var key in counts){ | |
countsArray.push({screen_name:key, count: counts[key]}) | |
} | |
countsArray.sort((a,b) => b.count - a.count) | |
return countsArray; | |
} | |
function getHomeTimeline( results ) { | |
if(!results) { | |
results = {}; | |
} | |
if(!results.data){ | |
results.data = [] | |
} | |
//"Up to 800 Tweets are obtainable on the home timeline" | |
if(!results.max){ | |
results.max = 800; | |
} | |
if ( results.data.length >= results.max ) { | |
return Promise.resolve(results.data); | |
} | |
// this is ugly but fuck it, our params are splashed into the top level object | |
// but we need to get data array out of there because the get request will serialize all that | |
var params = Object.assign({}, results); | |
delete params['data']; | |
var thisManyMore = results.max - results.data.length | |
params.count = thisManyMore >= 200 ? 200 : thisManyMore; | |
if(results.data.length){ | |
params.max_id = results.data[results.data.length-1].id; | |
} | |
return client | |
.get("statuses/home_timeline", params) | |
.then(function(data) { | |
//sadly errors come back as an object, while results come back as an array | |
if(!Array.isArray(data)){ | |
return Promise.reject(data); | |
} | |
//seems like when theyre done giving you results for any reason, it comes back an empty array | |
if(data.length == 0){ | |
return Promise.resolve(results.data); | |
} | |
//add new results to old results and recursive call | |
results.data = results.data.concat(data); | |
return getHomeTimeline( results ); | |
} | |
); | |
} | |
getHomeTimeline() | |
.then(tweets => printTweetsPerDay(tweets)) | |
.then(tweets => sortTweetsByNoisyUser(tweets)) | |
.then(sortedUser => { | |
console.log(sortedUser.slice(0, 20)); //just show top 10 tweeters | |
}) | |
.catch(console.error); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Moved this over to https://github.com/jacobrosenthal/node-twitter-stuff