Created
September 3, 2023 06:45
-
-
Save seeya/4fb9659850aed7c4e4b2fe9ab3add3c1 to your computer and use it in GitHub Desktop.
This script uses DevTools console to extract all comments from an Instagram post. It consolidates the comments, grouping each comment with its user and extracting the username found in each comment.
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
function scrollToBottom(searchText, cb) { | |
console.log("Search Text", searchText) | |
let mainDiv = Array.from(document.querySelectorAll("span")).find(e => e.innerText.indexOf(searchText) != -1).parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement; | |
console.log(mainDiv) | |
mainDiv.scrollTo({ top: mainDiv.scrollHeight, behavior: 'smooth' }); | |
if(mainDiv.scrollTop === mainDiv.scrollHeight - mainDiv.offsetHeight) { | |
console.log("We've reached the end of the comments section!"); | |
cb(); | |
} else { | |
setTimeout(() => { | |
scrollToBottom(searchText, cb); | |
}, 1800) | |
} | |
} | |
function start(searchText) { | |
scrollToBottom(searchText, () => { | |
console.log("Gonna iterate through each comment now and create a mapping..."); | |
let mapping = findFirstComment(searchText); | |
let csvData = convertToCSV(mapping); | |
downloadBlob(csvData, "consolidated.csv", 'text/csv;charset=utf-8;'); | |
}); | |
} | |
function findFirstComment(commentText) { | |
console.log("Finding the comment div...") | |
let commentDiv = Array.from(document.querySelectorAll("span")).find(e => e.innerText.indexOf(commentText) != -1); | |
let commentLists = commentDiv.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.parentElement.children; | |
let mapping = {}; // username: { comments: [], extracted: { @username: 1 } }]; | |
// We start from 1 as the first element as it's the user's post description | |
for(let i=1; i<commentLists.length; i++) { | |
let commentBox = commentLists[i]; | |
let comment = commentBox.innerText; // "username\n \n23h\ncomment\n1 like\nReply" | |
comment = comment.split("\n"); // ['username', ' ', '23h', 'comment', '1 like', 'Reply'] | |
// If the comment length is 5 >= , then it's a comment | |
if (comment.length >= 5) { | |
const [username, empty, commentedAt, content] = comment; | |
if(!mapping[username]) { | |
mapping[username] = { | |
comments: [], | |
extracted: {} | |
}; | |
} | |
// Add the comment to the mapping | |
mapping[username].comments.push(content) | |
// Add the extracted usernames to the mapping | |
let usernamesInComment = content.match(/\@\w+/g); | |
if(!usernamesInComment) continue; | |
for(let x=0; x<usernamesInComment.length; x++) { | |
const u = usernamesInComment[x]; | |
if(!mapping[username].extracted[u]) { | |
mapping[username].extracted[u] = 1; | |
} else { | |
mapping[username].extracted[u] += 1; | |
} | |
} | |
} | |
} | |
console.log(mapping); | |
return mapping; | |
} | |
function convertToCSV(mapping) { | |
console.log("Converting to CSV...") | |
let csvData = []; | |
let headers = ["Total Points", "Username", "Comment", "Extracted"]; | |
csvData.push(headers); | |
// { username: { comments: [], extracted: { @username: 1 } } }; | |
for(let username in mapping) { | |
let totalPoints = 0; | |
let user = mapping[username]; | |
let uniqueUsers = Object.keys(user.extracted); | |
totalPoints = uniqueUsers.length | |
csvData.push([totalPoints, username, user.comments ? `"${user.comments.join("\n")}"` : "", uniqueUsers ? uniqueUsers.join(" ") : ""]) | |
} | |
csvData.sort((a, b) => b[0] - a[0]) | |
for(let i=0; i<csvData.length; i++) { | |
csvData[i] = csvData[i].join(","); | |
} | |
return csvData.join("\n"); | |
} | |
function downloadBlob(content, filename, contentType) { | |
console.log("downloading the csv") | |
var blob = new Blob([content], { type: contentType }); | |
var url = URL.createObjectURL(blob); | |
var pom = document.createElement('a'); | |
pom.href = url; | |
pom.setAttribute('download', filename); | |
pom.click(); | |
} | |
start("<CHANGE THIS - Enter any text in the comment section, try to be unique as it will find the container used to store the comments>") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment