Mehmet Kagan Kayaalp
Mehmet Kagan Kayaalp

Reputation: 565

Checking friendship in Tweepy

I am trying to find, do I follow someone or not. I realized that although it is written in official Tweepy document, I cannot use API.exists_friendship anymore. Therefore, I tried to use API.show_friendship(source_id/source_screen_name, target_id/target_screen_name) and as the document said it returns me friendship object. (<tweepy.models.Friendship object at 0x105cd0890>, <tweepy.models.Friendship object at 0x105cd0ed0>)

When I write screen_names = [user.screen_name for user in connection.show_friendship(target_id=someone_id)] it returns my_username and username for someone_id.

Can anyone tell me how can I use it properly? or is there any other method that simply returns me True/False because I just want to know do I follow him/her or not.

Upvotes: 0

Views: 3190

Answers (4)

daveaneo
daveaneo

Reputation: 29

def unfollow_non_followers():
    friends = api.friends_ids()
    me = api.me()

    followers = api.followers_ids(me)

    non_followers = [x for x in friends if x not in followers]

    # unfollow all non-follow backs
    for id in non_followers:

        api.destroy_friendship(id)

        # Display unfollowed user, pause for API limitations (valid or not?)
        user = api.get_user(id)
        print(f'No longer following {user.name}')
        time.sleep(3)

Upvotes: 0

venkatesh venka
venkatesh venka

Reputation: 1

i am using below code this works fine .. if(api.show_friendship(source_screen_name='venky6amesh', target_screen_name='vag')): print("the user is not a friend of ") try: api.create_friendship(j) print('success') except tweepy.TweepError as e: print('error') print(e.message[0]['code']) else: print("user is a friend of ",j)

Upvotes: 0

Jzbach
Jzbach

Reputation: 368

The method show_friendship returns a tuple which is a bit difficult to understand when you try to see if a specific user is following you and there is another way to see at once all the followers that are not following you. However, if you still want to check a single connection, I found that when you try the following:

status = api.show_friendship(A,B)
print(status[0].following, status[1].following)

If you get the output as True False it means that he/she is not following you. Other cases are:

  1. False True = you are being followed by him/her.
  2. False False = there is no connection between you two.

However, as I said above, you can check all the people that is not following you with a single snippet of code. As posted in another similar question, you can find the code at https://stackoverflow.com/a/45776597/4091473

Hope it helps!

Upvotes: 4

Mehmet Kagan Kayaalp
Mehmet Kagan Kayaalp

Reputation: 565

Actually I do not like the answer but for now I used;

me = api.me()
user = api.get_user(username)

for f in user.friends():
    if me.name in f.name:
        print "FOLLOWING"
    else:
        print "NOT FOLLOWING"

I think there should be (and there is) an optimum solution (that's why I asked) about it and probably when my number of followings increases, the process of it will be longer. If you know better/optimum solution, please let me know.

Upvotes: 1

Related Questions