Part 15: How To Check If An Account Is Following Back And Retrieve Mutual Followers/Following Between Two Accounts
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:
- Part 0: How To Install Steem-python, The Official Steem Library For Python
- Part 1: How To Configure The Steempy CLI Wallet And Upvote An Article With Steem-Python
Filtering
- Part 2: How To Stream And Filter The Blockchain Using Steem-Python
- Part 6: How To Automatically Reply To Mentions Using Steem-Python
Voting
- Part 3: Creating A Dynamic Autovoter That Runs 24/7
- Part 4: How To Follow A Voting Trail Using Steem-Python
- Part 8: How To Create Your Own Upvote Bot Using Steem-Python
Posting
- Part 5: Post An Article Directly To The Steem Blockchain And Automatically Buy Upvotes From Upvote Bots
- Part 7: How To Schedule Posts And Manually Upvote Posts For A Variable Voting Weight With Steem-Python
Constructing
Rewards
- Part 9: How To Calculate A Post's Total Rewards Using Steem-Python
- Part 12: How To Estimate Curation Rewards Using Steem-Python
- Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python
Transfers
- Part 11: How To Build A List Of Transfers And Broadcast These In One Transaction With Steem-Python
- Part 13: Upvote Posts In Batches Based On Current Voting Power With Steem-Python
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
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
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