Skip to content

Instantly share code, notes, and snippets.

@adminy
Last active January 18, 2023 04:49
Show Gist options
  • Save adminy/5e80c40592b135e6d7fd8e6bd88c825a to your computer and use it in GitHub Desktop.
Save adminy/5e80c40592b135e6d7fd8e6bd88c825a to your computer and use it in GitHub Desktop.
const randInt = (min, max) => Math.floor(Math.random() * (max - min + 1) + min) // min and max included
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
const followersElement = getFollowersElementWithUsername(getUsername())
followersElement.click()
function getUsername () {
const pageTitleElement = document.getElementsByTagName('h2')[0]
if (!pageTitleElement) throw new Error('No title to get username from')
return pageTitleElement.innerHTML
}
function getFollowersElementWithUsername (username) {
const followersElement = document.querySelectorAll('a[href="/' + username + '/following/"]')[0]
if (!followersElement) throw new Error('No followers element was found')
return followersElement
}
const friends = new Set(['user1', 'user2'])
const unfollows = {}
let tries = 0
let globalCount = 0
const hasMoreToLoad = ul => ul.childElementCount !== globalCount || ul.children[ul.childElementCount - 1].firstChild.getAttribute('data-visualcompletion') === 'loading-state'
const populateUnfollowsPool = () => {
const ul = document.querySelectorAll('ul')[1].firstChild
const lis = [...ul.querySelectorAll('li')]
.map(li => ({ button: li.querySelector('button'), name: (li.querySelector('a') || { href: '' }).href.split('/').slice(-2)[0] }))
.filter(({ button, name }) => button && button.textContent === 'Following' && name && !friends.has(name) && !unfollows[name])
lis.forEach(({ button, name }) => (unfollows[name] = { button, name }))
globalCount = ul.childElementCount
ul.children[globalCount - 1].scrollIntoView(true)
console.log('elements', globalCount, 'unfollows', Object.keys(unfollows).length, 'tries', tries)
setTimeout(() => hasMoreToLoad(ul) || tries++ < 3 ? populateUnfollowsPool() : finishedCollection(), randInt(848, 2004))
}
setTimeout(populateUnfollowsPool, 3000)
async function finishedCollection () {
console.log('!!! Collection Finished')
const toUnfollow = Object.values(unfollows)
const total = toUnfollow.length
for (let i = 0; i < total; i++) {
const { button, name } = toUnfollow[i]
button.scrollIntoView(true)
button.click()
await sleep(randInt(50, 200))
const unfollow = [...document.querySelectorAll('button')].find(button => button.textContent === 'Unfollow')
unfollow && unfollow.click()
console.log('Unfollowed ', name, (i / total * 100).toFixed(2), '%')
await sleep(i % 8 ? randInt(120, 800) : randInt(1, 10) * 60 * 1000)
}
}
@paulchrisluke
Copy link

thank you for the rapid response! on home page Uncaught Error: No title to get username from

image

@adminy
Copy link
Author

adminy commented Sep 24, 2021

Turn off mobile view. This script is intended for the web view. By home page I mean the account home page.

so instagram.com/username

@congaterori
Copy link

8a2b7db607f6.js:79 POST https://www.instagram.com/logging/falco net::ERR_BLOCKED_BY_CLIENT

@adminy
Copy link
Author

adminy commented Oct 29, 2021

Network add blocker? Or adblocker?

@Nomarc13
Copy link

Nomarc13 commented Nov 1, 2021

Are you able to add a way to set the unfollows pool instead of calculating it? I have 5k+ accounts to unfollow. I want to do batches of however many unfollows Insta allows per day (or per hour?); not sure on the exact number.

@adminy
Copy link
Author

adminy commented Nov 1, 2021

Unfortunately this is a crappy script that manipulates the dom. What you want to be doing ideally is to interact with the API directly.

That would allow you to break the pipeline in two. All you will need to know is the account id. The rest could be recreated by the session things like token and other instagram required params.

Problems with proprietary API's is that they change often and it can be quite hard to keep up with the reverse engineering. I'm guessing this is why nobody made a chrome extension for this already or an npm package.

There is one here that queries all followers here
But have a look for the project's active issues

(Might I suggest you create a new account instead? @Nomark13)

@jagdishlove
Copy link

Can you make a script to find all who didn't follow back, I just need a list so that I can put that list in another script where I can easily unfollow all, or just usernames are also enough, or if you know anything regarding this please let me know.

@boxxxxy
Copy link

boxxxxy commented Dec 19, 2021

image

Gathers accounts perfectly but only unfollows one, then slowly goes through accounts with the 0.01% 0.02% etc but doesn't unfollow the rest. Or sometimes it has this error linked above. Is it a problem on my end

@adminy
Copy link
Author

adminy commented Dec 19, 2021

Interesting. I've for some reason cannot access instagram from the computer anymore because password reset page does not work for me and I can't actually try the script anymore ...

It doesn't look like a serious problem though. Yeah, Instagram Will be slow 😅 what's worse is their rate limiting is not quite easy to pin down anymore.

@adminy
Copy link
Author

adminy commented Dec 19, 2021

Since there is a lot of people having the same issue, I might look into the API itself to speed up this unfollow process.

@boxxxxy
Copy link

boxxxxy commented Dec 19, 2021

It's now unfollowing correctly after a few refreshes and script restarts, the api limit keep getting hit but after 5 mins it'll unfollow a couple more so it's ok!

@koolamusic
Copy link

Thanks for making this.

@mxxnshade
Copy link

keep getting
`Vev1H1eefJG.js?_nc_x=Ij3Wp8lg5Kz:59 ErrorUtils caught an error:

Cannot read properties of undefined (reading 'scrollIntoView')

Subsequent non-fatal errors won't be logged; see https://fburl.com/debugjs.`

@mikeknapp
Copy link

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