-
-
Save AkashMV/4605b63410510cb20b0df11ebbe21485 to your computer and use it in GitHub Desktop.
Download a JSON List of people you follow on twitter
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
//there are issues with how we manage to skip pixels. | |
//accounts are getting skipped. | |
let following = []; // Initialize an empty array to hold all elements of people you follow | |
let formattedFollowers = [] | |
const scrollInterval = 2000; | |
const scrollStep = 2000; // Pixels to scroll on each step | |
let previousFollowingCount = 0; | |
let unchangedCount = 0; | |
const scrollToEndIntervalID = setInterval(() => { | |
window.scrollBy(0, scrollStep); | |
const currentFollowingCount = following.length; | |
if (currentFollowingCount === previousFollowingCount) { | |
unchangedCount++; | |
if (unchangedCount >= 2) { // Stop if the count has not changed 5 times | |
console.log('Scraping complete'); | |
console.log('Total following scraped: ', following.length); | |
console.log('Downloading following list as JSON...'); | |
clearInterval(scrollToEndIntervalID); // Stop scrolling | |
observer.disconnect(); // Stop observing DOM changes | |
followingFormatter(following) | |
downloadFollowingsasJSON(formattedFollowers); // Download the follower list as a JSON file | |
} | |
} else { | |
unchangedCount = 0; // Reset counter if new followings were added | |
} | |
previousFollowingCount = currentFollowingCount; // Update previous count for the next check | |
}, scrollInterval); | |
function updateFollowing() { | |
document.querySelectorAll('section.css-175oi2r [data-testid="UserCell"]').forEach(followingElement => { | |
const followingText = followingElement.innerText; // Extract text content | |
console.log(followingText) | |
if (!following.includes(followingText)) { // Check if the following's text is not already in the array | |
following.push(followingText); // Add new following's text to the array | |
console.log("followings scraped: ", following.length) | |
} | |
}); | |
} | |
// Initially populate the followings array | |
updateFollowing(); | |
const followingFormatter = (entries) => { | |
entries.forEach(entry => { | |
const parts = entry.split('\n'); | |
// Extract components | |
const firstName = parts[0]; | |
const userId = parts[1]; | |
const followingStatus = parts[2]; | |
const bio = parts[3]; | |
// Create object | |
const gd = { | |
firstname: firstName, | |
userId: userId, | |
following: followingStatus, | |
bio: bio | |
} | |
formattedFollowers.push(gd); | |
}) | |
} | |
// Create a MutationObserver to observe changes in the DOM | |
const observer = new MutationObserver(mutations => { | |
mutations.forEach(mutation => { | |
if (mutation.addedNodes.length) { | |
updateFollowing(); // Call updateFollowing whenever new nodes are added to the DOM | |
} | |
}); | |
}); | |
// Start observing the document body for child list changes | |
observer.observe(document.body, { childList: true, subtree: true }); | |
function downloadFollowingsasJSON(followingArray) { | |
const jsonData = JSON.stringify(followingArray); // Convert the array to JSON | |
const blob = new Blob([jsonData], { type: 'application/json' }); | |
const url = URL.createObjectURL(blob); | |
const link = document.createElement('a'); | |
link.href = url; | |
link.download = 'following.json'; // Specify the file name | |
document.body.appendChild(link); // Append the link to the document | |
link.click(); // Programmatically click the link to trigger the download | |
document.body.removeChild(link); // Clean up and remove the link | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment