Part 15: How To Check If An Account Is Following Back And Retrieve Mutual Followers/Following Between Two Accounts

in #utopian-io7 years ago (edited)

steem-python.png

This tutorial is part of a series where different aspects of programming with steem-python are explained. Links to the other tutorials can be found in the curriculum section below. This part will look further to the Account class. You will learn how to retrieve followers and following for an account. Further comparisons between accounts will be made to display relations.


What will I learn

  • Retrieve list of followers/following
  • Check if following follows back
  • Check for mutual followers/following between two accounts

Requirements

  • Python3.6
  • steem-python

Difficulty

  • Basic

Tutorial

Setup

Download the files from Github. There are two files check_following.py and compare.py. check_following.py takes 1 account while compare.py takes 2 accounts.

Run scripts as following:
> python check_following account
> python compare account1 account2

Retrieve list of followers/following

The account class contains two functions that deal with followers and following. These functions can be used to retrieve the current list of followers/following.

def get_followers(self):
    return [x['follower'] for x in self._get_followers(direction="follower")]

def get_following(self):
    return [x['following'] for x in self._get_followers(direction="following")]

Create an account object from the username for which you want to retrieve the list of followers/following.

from steem.account import Account

account = Account(username)

list_followers = account.get_followers()
list_following = account.get_following

Check if following follows back

To check if an user that the account is following is following the account back, first the following account list is retrieved. Then for each following a second following list is retrieved and this is then checked for the original account.

for following in Account(account).get_following():
    print ('\n' + following)
    if account in Account(following).get_following():
        print  ('Follow back: Yes')
    else:
        print  ('Follow back: No')

Check for mutual followers/following between two accounts

To check for mutual followers/following between two accounts first the necessary lists have to be retrieved. Then for each account in the list this has to be checked with the corresponding list from the other account. In case of a match this is added to a list, which is printed at the end.

mutual_following = []

for following in account2_following:
    if following in account1_following:
        mutual_following.append(following)

The runtime of the script varies hugely depending on the amount of followers/following each account has. For this reason the data is loaded at the beginning while all the comparisons are done in memory.

Running the script

Running check_following.py simply prints out a each account that is being followed and also states if this account is following the user back.

$ python check_following.py juliank

acidyo
Follow back: Yes

acromott
Follow back: Yes

adonisabril
Follow back: Yes



check_following.py compares the followers and following of two accounts and prints out a report of mutual relationships.

Account1: juliank
Account2: amosbastian

Follow back: Yes
Mutual following: 5
['acidyo', 'emrebeyler', 'oups', 'photocontests', 'steempytutorials']
Mutual followers: 51
['a-0-0', 'a-0-1', 'a-a-a', 'amrizal', 'andre-verbrick', 'andrikyokyo', 'aninhasalegre', 'aqim', 'atenean101', 'azharie', 'barranger', 'byresteem', 'elfexor', 'emrebeyler', 'eva.milli', 'eventta', 'fadhlon', 'faustino', 'fujaxxi', 'gamemusic', 'hendrasaputra', 'hugewhale', 'ionutciobanu', 'ishotz', 'jackmun', 'khalidniazi', 'lautenglye', 'mahamudul', 'meansunlare', 'melindaa', 'mishakhan93', 'munaa', 'nilesh.katuwal', 'obaidur9595', 'petlover', 'reddyyeswanth', 'sam-muffins', 'schlijk', 'setianyareza', 'shaunven', 'siralex', 'steem-trending', 'sulemna', 'talhadogan', 'technogadgets', 'umairkhalid', 'vaansteam', 'viking-ventures', 'xvz', 'yaminhi', 'zeldam']

Curriculum

Set up:
Filtering
Voting
Posting
Constructing
Rewards
Transfers

The code for this tutorial can be found on GitHub!

This tutorial was written by @juliank in conjunction with @amosbastian.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

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

Mucho gracias!

Awesome! Thanks for this, I'm slowly learning Python so this helps. I also did the mutual followers thingy tutorial on my blog with SteemJS if you're interested.
Here's the link to it
https://steemit.com/utopian-io/@julienbh/steemjs-snippets-get-reciprocal-followers

Waiting for your next post!

Ah cool I should look at it just to see how different it is. Just there is so much stuff to do that I cannot get myself to do so

Yes it's crazy the possibilities. There's also Steem SQL which opens up even more stuff to do. I have a Chrome extension project up right now maybe we could collaborate!

@steempytutorials, Contribution to open source project, I like you and upvote.

I will download and see if i can run it..

UPVOTED & RESTEEMED

Great contribution

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

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

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.16
TRX 0.16
JST 0.030
BTC 58237.47
ETH 2479.72
USDT 1.00
SBD 2.38