Created
March 14, 2023 13:35
-
-
Save xoraus/d6f2e8b08cc1f2fb041366ea70bee917 to your computer and use it in GitHub Desktop.
This is a Node.js script that uses the Twitter API to retrieve a list of users that the authenticated user is following, and then writes the data to a CSV file.
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
const Twitter = require('twitter'); | |
const createCsvWriter = require('csv-writer').createObjectCsvWriter; | |
const fs = require('fs'); | |
// 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' | |
}); | |
// 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'} | |
] | |
}); | |
// get following list and write to CSV | |
let cursor = -1; // initialize cursor to first page | |
let followingData = []; | |
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)); | |
} | |
}); | |
} | |
getFollowing(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment