Forked from jalbam/unfollow-non-followers-twitter.js
Last active
February 26, 2024 16:54
-
-
Save roshanmirajkar/afbefda0bd127e0c3df42ac80d6eae6f to your computer and use it in GitHub Desktop.
Code that prints who follows you back from your Following, and saves to CSV file. Client-side. Works as of Dec, 2023.
This file contains 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
/* | |
Collect and export a list of Twitter usernames you're following, along with follow-back status, to a CSV file. | |
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' after the set time. | |
* Ensure to comply with Twitter's Terms of Service when using scripts. | |
*/ | |
// Language settings for the script | |
var LANGUAGE = "EN"; | |
var WORDS = { | |
EN: { | |
followsYouText: "Follows you" // Text indicating if someone follows you | |
}, | |
// Add additional languages here | |
}; | |
// Timing setting for data collection cycles (in milliseconds) | |
var MS_PER_CYCLE = 10; | |
// Settings to filter which users to include in the CSV export | |
var PRINT_FOLLOW_INFORMATION = true; // Include follow-back information in output | |
var SKIP_FOLLOWERS = false; // Exclude users who follow you from the output | |
var SKIP_NON_FOLLOWERS = false; // Exclude users who don't follow you from the output | |
var SKIP_USERS = [ // List of 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 usernames to lowercase for case-insensitive comparison | |
SKIP_USERS = SKIP_USERS.map(function(value) { return value.toLowerCase(); }); | |
// Array to store user data for CSV output | |
var userData = []; | |
// Collect usernames and follow-back status | |
function printUserNames() { | |
var followsYouText = WORDS[LANGUAGE].followsYouText; | |
var userContainers = document.querySelectorAll('[data-testid=UserCell]'); | |
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; | |
if (!userData.some(u => u.Username === userName)) { | |
userData.push({ Username: userName, FollowsYou: followsYou ? "Yes" : "No" }); | |
console.log(`* Username: ${userName}, Follows You: ${followsYou ? "Yes" : "No"}`); | |
} | |
}); | |
} | |
// Automatically scroll and show usernames | |
function scrollAndShowUsernames() { | |
window.scrollTo(0, document.body.scrollHeight); | |
printUserNames(); | |
setTimeout(scrollAndShowUsernames, MS_PER_CYCLE); | |
} | |
// Convert 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; | |
} | |
// Trigger the download of the CSV file | |
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); | |
} | |
// Begin the scrolling and username collection process | |
scrollAndShowUsernames(); | |
// Schedule the CSV download after a set period of time (in milliseconds) | |
setTimeout(downloadCSV, 80000); // 80 seconds delay before download |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment