Skip to content

Instantly share code, notes, and snippets.

@osamaishtiaq
Last active December 6, 2022 15:30
Show Gist options
  • Save osamaishtiaq/40a629733bfb9dd4391a25776ae484f4 to your computer and use it in GitHub Desktop.
Save osamaishtiaq/40a629733bfb9dd4391a25776ae484f4 to your computer and use it in GitHub Desktop.
/**
*
* @param pageName - Page name you want to check
* @returns Object { followers, followings, accountsNotFollowBack }
*/
async function getListOfPeopleWhoNotFollow(pageName) {
let username = pageName; // your page name, mine is oisee_gallery
try {
console.log("Fetching data....");
let res = await fetch(`https://www.instagram.com/${username}/?__a=1`)
res = await res.json()
let userId = res.graphql.user.id
console.log("Getting followers");
let after = null,
has_next = true
while (has_next) {
await fetch(`https://www.instagram.com/graphql/query/?query_hash=c76146de99bb02f6415203be841dd25a&variables=` + encodeURIComponent(JSON.stringify({
id: userId,
include_reel: true,
fetch_mutual: true,
first: 50,
after: after
}))).then(res => res.json()).then(res => {
has_next = res.data.user.edge_followed_by.page_info.has_next_page
after = res.data.user.edge_followed_by.page_info.end_cursor
followers = followers.concat(res.data.user.edge_followed_by.edges.map(({
node
}) => {
return node.username
}))
})
}
console.log("Getting followings");
has_next = true
after = null
while (has_next) {
await fetch(`https://www.instagram.com/graphql/query/?query_hash=d04b0a864b4b54837c0d870b0e77e076&variables=` + encodeURIComponent(JSON.stringify({
id: userId,
include_reel: true,
fetch_mutual: true,
first: 50,
after: after
}))).then(res => res.json()).then(res => {
has_next = res.data.user.edge_follow.page_info.has_next_page
after = res.data.user.edge_follow.page_info.end_cursor
followings = followings.concat(res.data.user.edge_follow.edges.map(({
node
}) => {
return node.username
}))
})
}
console.log("Gettings accounts that do not follow back...");
accountsNotFollowBack = followings.filter(fname => !followers.includes(fname));
console.log('accounts that don\'t follow back: ', accountsNotFollowBack);
return {
followers,
followings,
accountsNotFollowBack
};
} catch (err) {
console.log('Invalid username')
}
}
async function StartProcess() {
try {
console.log("Starting the process");
// select following button to open the dialog
var xpath = "//div[text()=' following']";
var matchingElement = document.evaluate(xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
matchingElement.click();
await new Promise(r => setTimeout(r, 2000));
let followingList = await getFollowings();
let unfollowersLeft = accountsNotFollowBack.map(x => x.trim());
while (unfollowersLeft.length > 0) {
const currUserToUnfollow = unfollowersLeft[0];
console.log("Currently trying to unfollow: ", currUserToUnfollow);
if (followingList[currUserToUnfollow]) {
followingList[currUserToUnfollow].click();
await new Promise(r => setTimeout(r, 1500));
const unfollowConfirmBtn = document.querySelector('.aOOlW.-Cab_');
// sleep for some time
await new Promise(r => setTimeout(r, 1500));
unfollowConfirmBtn.click();
const unfollowedUser = unfollowersLeft.shift();
console.log("Unfollowed :", unfollowedUser);
await new Promise(r => setTimeout(r, 2000));
} else {
await ScrollDown();
followingList = await getFollowings();
}
console.log("People remaining: ", unfollowersLeft.length);
}
} catch (err) {
console.log("Error while unfollowing: ", err);
}
}
async function getFollowings() {
try {
const followingListItems = document.querySelector('div[aria-label="Following"]').childNodes[0].childNodes[0].childNodes[2].childNodes[0].childNodes[0].childNodes;
let followingsDict = {};
for (let li of followingListItems) {
const innerButtonElem = li.childNodes[0].childNodes[1].childNodes[0];
let username = '';
try {
userName = li.childNodes[0].childNodes[1].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[0].innerText;
} catch {
userName = li.childNodes[0].childNodes[0].childNodes[1].childNodes[0].childNodes[0].childNodes[0].childNodes[0].innerText;
}
username = username.trim();
followingsDict[userName] = innerButtonElem;
}
return followingsDict;
} catch (err) {
console.error("Error occurred while getting new elements from dialog list. Err: ", err);
}
}
async function ScrollDown() {
try {
console.log("Finding more accounts to unfollow...");
await new Promise(r => setTimeout(r, 2500));
const scroll_stuff = document.querySelector('div[aria-label="Following"]').childNodes[0].childNodes[0].childNodes[2];
scroll_stuff.scrollTop = scroll_stuff.scrollHeight;
} catch (err) {
console.error("Error occurred while ScrollDown() Errr: ", err);
}
}
// Bismillah
const { followers, followings, accountsNotFollowBack } = await getListOfPeopleWhoNotFollow('oisee_gallery');
await StartProcess();
@djkepa
Copy link

djkepa commented Dec 6, 2022

Getting error:
Uncaught TypeError: Cannot destructure property 'followers' of '(intermediate value)' as it is undefined.
at :143:9

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment