How to Export Your Twitter Following List to a CSV File
Are you looking for a way to export your Twitter following list to a CSV file? In this article, we’ll show you how to do it using Node.js and the Twitter API.
First, you’ll need to install the twitter
and csv-writer
packages. You can do this by running npm install twitter csv-writer
in your terminal.
const Twitter = require('twitter');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const fs = require('fs');
Next, set up a Twitter API client by creating a new instance of the Twitter
class from the twitter
package. You’ll need to provide your consumer key, consumer secret, access token key, and access token secret as options when creating the client.
/ set up Twitter API client
const client = new Twitter({
// consumer_key: 'your_consumer_key',
// consumer_secret: 'your_consumer_secret',
// access_token_key: 'your_access_token_key',
// access_token_secret: 'your_access_token_secret'
});
After setting up the Twitter API client, set up a CSV writer using the createObjectCsvWriter
method from the csv-writer
package. Specify the path where you want to save the CSV file and define the headers for each column in the CSV file.
// set up CSV writer
const csvWriter = createCsvWriter({
path: 'following.csv',
header: [
{id: 'name', title: 'Name'},
{id: 'screen_name', title: 'Screen Name'},
{id: 'description', title: 'Description'},
{id: 'location', title: 'Location'},
{id: 'url', title: 'URL'},
{id: 'followers_count', title: 'Followers'},
{id: 'friends_count', title: 'Following'},
{id: 'created_at', title: 'Joined'},
{id: 'verified', title: 'Verified'}
]
});
Now that everything is set up, it’s time to retrieve your following list from Twitter. The code snippet uses a recursive function called getFollowing
that retrieves 200 users at a time (the maximum allowed by the Twitter API) until all pages of results have been retrieved.
The function uses the client.get
method from the Twitter API client to send a GET request to the 'friends/list'
endpoint with parameters specifying that 200 users should be returned per page and providing a cursor value for pagination. The cursor is initially set to -1 (the first page) and is updated with each subsequent call to retrieve more pages of results.
// get following list and write to CSV
let cursor = -1; // initialize cursor to first page
let followingData = [];
The data returned by each call is added to an array called followingData
, which stores objects representing each user in your following list. Each object contains properties such as name, screen name, description, location, URL, followers count, friends count (i.e., following count), created at date/time (i.e., when they joined Twitter), and whether or not they are verified.
function getFollowing() {
client.get('friends/list', { count: 200, cursor: cursor }, function(error, following, response) {
if (error) throw error;
followingData = followingData.concat(following.users.map(user => ({
name: user.name,
screen_name: user.screen_name,
description: user.description,
location: user.location,
url: user.url,
followers_count: user.followers_count,
friends_count: user.friends_count,
created_at: user.created_at,
verified: user.verified
})));
// check if there are more pages to retrieve
if (following.next_cursor_str != '0') {
cursor = following.next_cursor_str;
getFollowing();
} else {
// write following data to CSV
csvWriter.writeRecords(followingData)
.then(() => console.log('Following list downloaded as following.csv'))
.catch(error => console.error(error));
}
});
}
Once all pages of results have been retrieved (indicated by when following.next_cursor_str == '0'
, meaning there are no more pages), then write all data stored in followingData array into csv file using csvWriter.writeRecords(followingData).
getFollowing();
And that’s it! Run this script using node command in terminal and you should see message “Following list downloaded as following.csv” indicating successful download of your twitter following list into csv file.
Here the link for complete script:
Follow for more tricks and tips: