Skip to content

Instantly share code, notes, and snippets.

@irealva
Created December 16, 2017 21:12
Show Gist options
  • Save irealva/35c16c196920d516b95bbe29c907655f to your computer and use it in GitHub Desktop.
Save irealva/35c16c196920d516b95bbe29c907655f to your computer and use it in GitHub Desktop.
Add all the Twitter users you are following to a Twitter list.
/*
Author: Irene Alvarado
Description: Add all the Twitter users you are following to a Twitter list.
Useful if using services such as TweetDeck to search only through people you are following
To run: first install twit through node `npm install twit`, then run `node addTwitterFollowingToList.js`
Change your API keys and list configurations where the comments say TODO.
Note about API limits: Twitter sets limits for how many users you can add to a list within 12 hours.
The limit number is not clear to me from what I could read online, but it seems
to be around 1,000 users per day or 12 hour period. If you are getting a 'You
aren't allowed to add members to this list' error it's probably because you've
hit a limit. Wait 12-24 hours and try again.
*/
var Twit = require('twit')
// TODO: Set your API key and access token
var T = new Twit({
consumer_key: '...',
consumer_secret: '...',
access_token: '...',
access_token_secret: '...',
})
// TODO: Set these two variables depending on your own account
var my_id = 'ire_alva' // This is your twitter ID
var list_name = 'All Following'; // This is the name of the Twitter list you want to add all your followers to. Create it beforehand on Twitter.
var following_ids; // Will store an array with all the users you are following
var list_slug; // Will store the id of the list we want to add all our followed users to
// This GET request gets a list of all the users you are following on twitter. Twitter calls them "friends"
// Important to stringify the IDs because some programming environments, like Javascript, will not consume Twitter IDs due to their size.
T.get('friends/ids', { user_id: my_id, stringify_ids: true }, function(err, data, response) {
following_ids = data.ids;
console.log("Fetched users");
console.log("You are following " + following_ids.length + " users.")
})
// This GET function finds the "slug" (the unique name of the list) for the list specified in the "list_name" variable
// It also adds all of the users we are following to that list
T.get('lists/list', { user_id: my_id }, function(err, data, response) {
for (var list of data) {
// Find the list that matches the one we specified in the "list_name" variable
if (list.name == list_name) {
list_slug = list.slug;
console.log("Your list slug is: " + list_slug);
// For each user that we are following, add them to our list
for (var following_id of following_ids) {
addToList(following_id);
}
}
}
})
// This function makes a POST request that adds a user with an ID to a list identified by a "slug"
function addToList(id) {
T.post('lists/members/create', { owner_screen_name: my_id, slug: list_slug, user_id: id }, (err, data, response) => {
if (err != undefined) {
console.log("Error adding " + id + ": " + err.message);
if (err.code == 108) {
console.log("Could not add user: " + id);
}
}
else {
console.log("Added: " + id);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment