Skip to content

Instantly share code, notes, and snippets.

@roshanmirajkar
Created December 31, 2023 02:21
Show Gist options
  • Save roshanmirajkar/c7924d7a2ad115b8b9c3462cfb90643b to your computer and use it in GitHub Desktop.
Save roshanmirajkar/c7924d7a2ad115b8b9c3462cfb90643b to your computer and use it in GitHub Desktop.
Twitter Following List Exporter
/*
Collect and export a list of Twitter usernames you're following, along with follow-back status, to a CSV file every 80 seconds. Done client-side using Javascript running in the Console window of the web browser.
Instructions:
1) Modify the 'LANGUAGE' variable to match your Twitter interface language.
2) Customize 'SKIP_USERS' with usernames you do not wish to include in the CSV.
3) 'PRINT_FOLLOW_INFORMATION' determines whether follow-back status is included.
4) 'SKIP_FOLLOWERS' and 'SKIP_NON_FOLLOWERS' settings filter out respective user groups.
5) 'MS_PER_CYCLE' controls the frequency of data collection cycles.
6) Load the Twitter page showing users you're following, open the console (F12), paste this code, and run it.
7) The script will automatically scroll through your following list, collect data, and download it as 'twitter_following.csv' every 80 seconds.
*/
// Language settings for the script. Modify the 'LANGUAGE' variable to match the language used by your Twitter interface.
// This script runs in your https://username/following
var LANGUAGE = "EN";
// WORDS dictionary holds the text for different languages that the script uses to identify if someone follows you.
// The 'followsYouText' should exactly match the text displayed on Twitter for that language.
// Add additional language support by adding new language keys and the appropriate text.
var WORDS = {
EN: { // English settings
followsYouText: "Follows you" // Text indicating if someone follows you back
},
// Example: Add a Spanish language setting below
// ES: {
// followsYouText: "Te sigue"
// },
};
// Timing configuration: 'MS_PER_CYCLE' controls the frequency (in milliseconds) of data collection cycles.
// This affects how often the script scrolls and collects data from the page.
var MS_PER_CYCLE = 10;
// Filtering settings: Configure which users to include or exclude in the CSV export.
var PRINT_FOLLOW_INFORMATION = true; // Set to true to include follow-back information in the output.
var SKIP_FOLLOWERS = false; // Set to true to exclude users who follow you from the output.
var SKIP_NON_FOLLOWERS = false; // Set to true to exclude users who don't follow you from the output.
var SKIP_USERS = [ // List of specific usernames to exclude from the output.
'user_name_to_skip_example_1',
'user_name_to_skip_example_2',
'user_name_to_skip_example_3'
];
// Normalize the usernames to lowercase for case-insensitive comparison.
SKIP_USERS = SKIP_USERS.map(function(value) { return value.toLowerCase(); });
// An array to store the collected user data for CSV export.
var userData = [];
// Function to collect usernames and follow-back status.
// This function will be called repeatedly according to 'MS_PER_CYCLE' timing.
function printUserNames() {
var followsYouText = WORDS[LANGUAGE].followsYouText;
var userContainers = document.querySelectorAll('[data-testid=UserCell]');
// Iterate over each user element to extract the necessary information.
userContainers.forEach(function(userContainer) {
var followsYou = userContainer.innerText.includes(followsYouText);
if ((followsYou && SKIP_FOLLOWERS) || (!followsYou && SKIP_NON_FOLLOWERS)) return;
var userNameElement = userContainer.querySelector('[href^="/"][role="link"]');
if (!userNameElement) return;
var userName = userNameElement.href.split('/').pop().toLowerCase();
if (SKIP_USERS.includes(userName)) return;
// Add the username and follow-back status to 'userData' if not already present.
if (!userData.some(u => u.Username === userName)) {
userData.push({ Username: userName, FollowsYou: followsYou ? "Yes" : "No" });
console.log(`* Username: ${userName}, Follows You: ${followsYou ? "Yes" : "No"}`);
}
});
}
// Function to automate scrolling and showing usernames.
// This function triggers the 'printUserNames' function and scrolls the window to load new users.
function scrollAndShowUsernames() {
window.scrollTo(0, document.body.scrollHeight);
printUserNames();
setTimeout(scrollAndShowUsernames, MS_PER_CYCLE);
}
// Function to convert the collected user data to CSV format.
function arrayToCSV(objArray) {
var csv = objArray.map(row => Object.values(row).join(',')).join('\n');
return 'Username,FollowsYou\n' + csv;
}
// Function to trigger the download of the CSV file.
// This creates a new Blob with the CSV content and initiates a download action in the browser.
function downloadCSV() {
var csv = arrayToCSV(userData);
var blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'twitter_following.csv';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
// Call the 'scrollAndShowUsernames' function to start the data collection process.
scrollAndShowUsernames();
// Set up an interval to download the CSV file every 80 seconds (80000 milliseconds).
// Each download will have the data collected up to that point in time.
setInterval(downloadCSV, 80000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment