Skip to content

Instantly share code, notes, and snippets.

@seeya
Last active January 7, 2025 15:00
Show Gist options
  • Select an option

  • Save seeya/4fb9659850aed7c4e4b2fe9ab3add3c1 to your computer and use it in GitHub Desktop.

Select an option

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. Make sure you're at the post page for example https://www.instagram.com/p/abcdefg/
function start() {
let searchText = prompt("Type the first comment in the comment list here");
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 getCommentDiv(commentText) {
let commentDiv = Array.from(document.querySelectorAll("span")).find(e => e.innerText.indexOf(commentText) != -1);
let currentParent = commentDiv;
for(let i=0; i<=15; i++) {
console.log(currentParent);
currentParent = currentParent.parentElement;
if(window.getComputedStyle(currentParent).overflow == 'auto') {
console.log("FOUND")
break;
}
if(i == 15) { alert("Can't find comment section"); }
}
return currentParent;
}
function scrollToBottom(searchText, cb) {
console.log("Search Text", searchText)
let mainDiv = getCommentDiv(searchText);
mainDiv.scrollTo({ top: mainDiv.scrollHeight, behavior: 'smooth' });
let offset = 10; // Give some offset for additional text like "View hidden comments"
if(mainDiv.scrollTop > mainDiv.scrollHeight - mainDiv.offsetHeight - 10) {
console.log("We've reached the end of the comments section!");
cb();
} else {
setTimeout(() => {
scrollToBottom(searchText, cb);
}, 2500)
}
}
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.parentElement.children;
let mapping = {}; // username: { comments: [], extracted: { @username: 1 } }];
// i=0 to start from the first comment
for(let i=0; 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 luckyDrawTemplate = [];
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
luckyDrawTemplate.push(Array(totalPoints).fill(username).join("\n"))
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(",");
}
csvData.push("==== Lucky Draw Template ====")
csvData.push(luckyDrawTemplate.join("\n"))
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment