davidarroyo1234 / InstagramUnfollowers

Check if people follows you back on Instagram.
MIT License
2.78k stars 261 forks source link

Exclude accounts from automatic unfollow #12

Closed shiner66 closed 2 years ago

shiner66 commented 2 years ago

Would it be possibile to exclude some accounts I don't want to be auto-unfollowed? E.g. pages and such

davidarroyo1234 commented 2 years ago

Would it be possibile to exclude some accounts I don't want to be auto-unfollowed? E.g. pages and such

I could customize the script a little bit to not unfollow certain users etc.. or to not unfollow account with higher follower count than X. If you are ok with that I will post here a modified script.

shiner66 commented 2 years ago

Yeah, both solutions would actually be appreciated for multiple use cases.

Thank you!

davidarroyo1234 commented 2 years ago

Yeah, both solutions would actually be appreciated for multiple use cases. Thank you!

I was only able to do it the first way, with a blacklist.

All the people in this blacklist wont be unfollowed.

To add some username to the blacklist just follow the example of username1,username2 etc..

To execute the script copy the code as always and paste it on the console.

(REMEMBER TO PUT THE USERNAME NOT THE NAME)

const ACCOUNT_USERNAME_BLACKLIST = [
    "username1",
    "username2",
    "username3",
]

function getCookie(name) {
    const value = `; ${document.cookie}`;
    const parts = value.split(`; ${name}=`);
    if (parts.length === 2) return parts.pop().split(';').shift();
}

function sleep(ms) {
    return new Promise((resolve) => {
        setTimeout(resolve, ms);
    });
}

function afterUrlGenerator(nextCode) {
    return `https://www.instagram.com/graphql/query/?query_hash=3dec7e2c57367ef3da3d987d89f9dbc8&variables={"id":"${ds_user_id}","include_reel":"true","fetch_mutual":"false","first":"24","after":"${nextCode}"}`;
}

function unfollowUserUrlGenerator(idToUnfollow) {
    return `https://www.instagram.com/web/friendships/${idToUnfollow}/unfollow/`;
}

let csrftoken = getCookie("csrftoken");
let ds_user_id = getCookie("ds_user_id");
let initialURL = `https://www.instagram.com/graphql/query/?query_hash=3dec7e2c57367ef3da3d987d89f9dbc8&variables={"id":"${ds_user_id}","include_reel":"true","fetch_mutual":"false","first":"24"}`;

let doNext = true;
let followedPeople;
let filteredList = [];
let getUnfollowCounter = 0;
let scrollCicle = 0;

startScript();

async function startScript() {
    while (doNext) {
        let receivedData
        try {
            receivedData = await fetch(initialURL).then(res => res.json());
        } catch (e) {
            continue;
        }

        if (!followedPeople) {
            followedPeople = receivedData.data.user.edge_follow.count;
        }

        doNext = receivedData.data.user.edge_follow.page_info.has_next_page;
        initialURL = afterUrlGenerator(receivedData.data.user.edge_follow.page_info.end_cursor);
        getUnfollowCounter += receivedData.data.user.edge_follow.edges.length;

        receivedData.data.user.edge_follow.edges.forEach(x => {
            if (!x.node.follows_viewer) {
                filteredList.push(x.node);
            }
        })

        console.clear();
        console.log(`%c Progress ${getUnfollowCounter}/${followedPeople} (${parseInt((getUnfollowCounter / followedPeople) * 100)}%)`, 'background: #222; color: #bada55;font-size: 35px;');
        console.log(`%c This users don't follow you (Still in progress)`, 'background: #222; color: #FC4119;font-size: 13px;');

        filteredList.forEach(x => {
            console.log(x.username);
        })

        await sleep(Math.floor(Math.random() * (1000 - 600)) + 1000);
        scrollCicle++;
        if (scrollCicle > 6) {
            scrollCicle = 0;
            console.log(`%c Sleeping 10 secs to prevent getting temp blocked`, 'background: #222; color: ##FF0000;font-size: 35px;');

            await sleep(10000);
        }
    }

    console.clear();

    console.log(`%c ${filteredList.length} users don't follow you`, 'background: #222; color: #bada55;font-size: 25px;');

    filteredList.forEach(x => {
        console.log(x.username);
    });

    wantUnfollow = confirm("Do you want to unfollow this people we listed?");

    if (wantUnfollow) {
        let counter = 0;
        unfollowSleepCounter = 0;
        for (const x of filteredList) {
            if (x.username && !ACCOUNT_USERNAME_BLACKLIST.includes(x.username)) {
                try {
                    await fetch(unfollowUserUrlGenerator(x.id), {
                        "headers": {
                            "content-type": "application/x-www-form-urlencoded",
                            "x-csrftoken": csrftoken,
                        },
                        "method": "POST",
                        "mode": "cors",
                        "credentials": "include"
                    });
                } catch (e) {
                }

                await sleep(Math.floor(Math.random() * (6000 - 4000)) + 4000);
                counter++;
                unfollowSleepCounter++;
                if (unfollowSleepCounter >= 5) {
                    console.log(`%c Sleeping 5 minutes to prevent getting temp blocked`, 'background: #222; color: ##FF0000;font-size: 35px;');
                    unfollowSleepCounter = 0;
                    await sleep(300000)
                }
                console.log(`Unfollowed ${counter}/${filteredList.length}`)
            }
        }
        console.log(`%c All DONE!`, 'background: #222; color: #bada55;font-size: 25px;');
    } else {
        console.log(`%c All DONE!`, 'background: #222; color: #bada55;font-size: 25px;');
    }
}
davidarroyo1234 commented 2 years ago

I'll close this issue for now. If you have any other question feel free to reopen it.