Reputation: 217
I have the following code, where I'm trying to unfollow anyone who isn't following me. I'm currently just seeing if a username is in the followers list, and if not, unfollowing them.
followers = []
for follower in tweepy.Cursor(api.followers).items():
followers.append(follower.screen_name)
for friend in tweepy.Cursor(api.friends).items():
if friend.screen_name in followers:
continue
else:
api.destroy_friendship(friend.screen_name)
It's somewhat tedious, so I'd like to make it shorter. I think I'm supposed to use either api.show_friendships
or api.lookup_friendships
, but I'm not entirely sure how to do that.
How would I go about fixing this?
Upvotes: 1
Views: 1052
Reputation: 217
I used the following code:
my_screen_name = api.me().screen_name
status = api.show_friendship(source_screen_name = friend.screen_name, target_screen_name = my_screen_name)
if status[0].following:
print(f'{friend.screen_name} follows {my_screen_name}.')
else:
api.destroy_friendship(friend.screen_name)
Upvotes: 1
Reputation: 13983
You can use lookup_friendships
to confirm who is following you
screen_names = ["user1", "user2", "user3"]
api = tweepy.API(auth, wait_on_rate_limit=True)
ret = api.lookup_friendships(screen_names = screen_names)
for rel in ret:
# name:user1 isFollowedBy? False
print(f'name:{rel.screen_name} isFollowedBy? {rel.is_followed_by} ')
The output is the a list of Relationship
objects where you can see who is a follower (see example above) and who you are following
Upvotes: 2