SteemJS Snippets - Get Reciprocal Followers

in #utopian-io7 years ago (edited)

I wanted to get the list of the users following me that I also follow. This is a script I made to accomplish that. You're free to add to it :)

function getReciprocalFollowers (username) {

        /**
     * Get reciprocal followers of a user
     * @param {String} username - username of the new account
     * @return {Array} reciprocal usernames
     */

    return new Promise(function(resolve, reject) {
        steem.api.getFollowers(username,
            '',
            'blog',
            1000,
            function(err, result) {

                var reciprocal = [];
                result.forEach(function(e) {
                    var getFollowing = new Promise(function(resolve, reject) {
                        steem.api.getFollowing(username,e.follower,'blog',1,
                            function(err, r) {
                                if (r[0] && e.follower == r[0].following)
                                    resolve(r[0].following);
                                resolve(null);
                            });
                    });
                    getFollowing.then(function(e) {
                        if (e)
                            reciprocal.push(e);
                    });
                });

                resolve(reciprocal);

            });
    });

}

So first we call the getFollowers Api to get our followers, the "1000" is the max number of followers. In my case I hardcoded it to 1000, but it could be more or you could loop if you have millions of followers.

Then for each of these followers, we try to find if that follower follows us. If yes, we resolve with the username, else we resolve with null.

The promise returns the username that we push to an array.

The array is then returned to the caller of getReciprocalFollowers() in the form of a promise.

How to call method

function Main() {
    getReciprocalFollowers('julienbh').then(function(e) {
        console.log(e);
    });
}

Since we have a promise, we need to use the then pattern. Here I just log to the console the array of users, but you can now do what you want with this :)


Julie xx



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you so much for the snippet.
I myself have been working on an application to be hosted on steemit.

No problem!

Never in a million years i thought of you as a nerd..
damn.. you just got sexier..

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Congratulations! This post has been upvoted from the communal account, @minnowsupport, by JulieNBH from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, theprophet0, someguy123, neoxian, followbtcnews/crimsonclad, and netuoso. The goal is to help Steemit grow by supporting Minnows and creating a social network. Please find us in the Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP. Be sure to leave at least 50SP undelegated on your account.

Hey @julienbh I am @utopian-io. I have just upvoted you!

Achievements

  • This is your first accepted contribution here in Utopian. Welcome!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.19
TRX 0.14
JST 0.030
BTC 61238.36
ETH 3278.38
USDT 1.00
SBD 2.46