Skip to content

Instantly share code, notes, and snippets.

@veltman
Created April 6, 2018 18:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save veltman/ef8eebe826c3519e3477580a66cb6d2c to your computer and use it in GitHub Desktop.
Save veltman/ef8eebe826c3519e3477580a66cb6d2c to your computer and use it in GitHub Desktop.
// npm install twit
const Twit = require("twit");
// Fill this in
const T = new Twit({
consumer_key: "???",
consumer_secret: "???",
access_token: "???",
access_token_secret: "???"
});
const user = "jbenton";
const otherUsers = [
"laurahazardowen",
"shansquared",
"newsbyschmidt",
"rbilton",
"ceodonovan",
"zseward"
];
// Get who each user is following
Promise.all([user, ...otherUsers].map(getAllFollowing)).then(results => {
const followers = {};
// Get shared ids that user isn't following, but other people are
results.slice(1).forEach((ids, i) =>
ids.forEach(id => {
if (!results[0].includes(id)) {
if (!followers[id]) {
followers[id] = [];
}
followers[id].push(otherUsers[i]);
}
})
);
// Convert IDs back to user details and print the top 100
getUsernames(followers).then(printResults);
});
// Log the top 100 with name + bio
function printResults(results) {
results.forEach(({ name, username, description, followers }) =>
console.log(
[
name + "(@" + username + ")",
"Followed by " + followers.join(", "),
description
]
.filter(d => d)
.join("\n") + "\n"
)
);
}
function getUsernames(followers) {
// Pick top 100 by number of shared followers from the list
const top100 = Object.entries(followers)
.sort((a, b) => b[1].length - a[1].length)
.slice(0, 100)
.map(p => p[0]);
// Convert ids to user details
return T.post("users/lookup", {
user_id: top100.join(","),
include_entities: false
}).then(({ data }) =>
data
.map(d => ({
name: d.name,
username: d.screen_name,
description: d.description,
followers: followers[d.id_str]
}))
.sort((a, b) => b.followers.length - a.followers.length)
);
}
// Assumes following 5000 or fewer, no pagination
function getAllFollowing(username) {
return T.get("friends/ids", {
screen_name: username,
stringify_ids: true,
count: 5000
}).then(({ data }) => data.ids);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment