SteemJS Snippets - Get Reciprocal Followers
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
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
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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