Skip to content
Related Articles

Related Articles

Python – API.show_friendship() in Tweepy
  • Last Updated : 08 Jun, 2020

Twitter is a popular social network where users share messages called tweets. Twitter allows us to mine the data of any user using Twitter API or Tweepy. The data will be tweets extracted from the user. The first thing to do is get the consumer key, consumer secret, access key and access secret from twitter developer available easily for each user. These keys will help the API for authentication.

API.show_friendship()

The show_friendship() method of the API class in Tweepy module is used to get the detailed relationship between two users.

Syntax : API.show_friendship(source_id / source_screen_name, target_id / target_screen_name)

Parameters :

  • source_id : specifies the ID of the user 1.
  • source_screen_name : specifies the screen name of the user 1.
  • target_id : specifies the ID of the user 2.
  • target_screen_name : specifies the screen name of the user 2.

Returns : an object of the class Friendship



Example 1 : Analyzing friendships by using the screen names.

filter_none

edit
close

play_arrow

link
brightness_4
code

# import the module
import tweepy
  
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
  
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
  
# set access to user's access key and access secret 
auth.set_access_token(access_token, access_token_secret)
  
# calling the api 
api = tweepy.API(auth)
  
# screen name of the account 1
source_screen_name = "Twitter"
  
# screen name of the account 2
target_screen_name = "TwitterIndia"
  
# getting the friendship details
friendship = api.show_friendship(source_screen_name = source_screen_name, target_screen_name = target_screen_name)
  
print("Is " + friendship[0].screen_name + " followed by " + friendship[1].screen_name, end = "? : ")
if friendship[0].followed_by == False:
    print("No")
else:
    print("Yes")
  
print("Is " + friendship[0].screen_name + " following " + friendship[1].screen_name, end = "? : ")
if friendship[0].following == False:
    print("No")
else:
    print("Yes")

chevron_right


Output :

Is Twitter followed by TwitterIndia? : Yes
Is Twitter following TwitterIndia? : No

Example 2 : Analyzing friendships by using the user IDs.

filter_none

edit
close

play_arrow

link
brightness_4
code

# user ID of the account 1
source_id = 14230524
  
# user ID of the account 2
target_id = 34507480
  
# getting the friendship details
friendship = api.show_friendship(source_id = source_id, target_id = target_id)
  
print("The sceen name of user 1 is : " + friendship[0].screen_name)
print("The sceen name of user 2 is : " + friendship[1].screen_name)
  
print("Is " + friendship[0].screen_name + " followed by " + friendship[1].screen_name, end = "? : ")
if friendship[0].followed_by == False:
    print("No")
else:
    print("Yes")
  
print("Is " + friendship[0].screen_name + " following " + friendship[1].screen_name, end = "? : ")
if friendship[0].following == False:
    print("No")
else:
    print("Yes")

chevron_right


Output :

The sceen name of user 1 is : ladygaga
The sceen name of user 2 is : ArianaGrande
Is ladygaga followed by ArianaGrande? : Yes
Is ladygaga following ArianaGrande? : No

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up
Recommended Articles
Page :