Skip to content
Related Articles

Related Articles

Python – Media object in Tweepy
  • Last Updated : 21 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.

Media

The Media object in Tweepy module contains the information about a media file uploaded on Twitter.

Here are the list of attributes in the Media object :

  • media_id : The ID of the media object.
  • media_id_str : The ID of the media object as a string.
  • size : The size of the media object in bytes.
  • expires_after_secs : The time in seconds after which the media object expires.
  • image_type : The type of image.
  • w : The width of the media object.
  • h : The height of the media object.

Example : We will use media_upload() method to upload and fetch the media object.

Consider the following image :






# 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)
  
# uploading the media and fetching the Media object
media = api.media_upload("gfg.png")
  
# printing the information
print("The media_id is : " + str(media.media_id))
print("The media_id_string is : " + media.media_id_string)
print("The size is : " + str(media.size))
print("The expires_after_secs is : " + str(media.expires_after_secs))
print("The image_type is : " + str(media.image["image_type"]))
print("The w is : " + str(media.image["w"]))
print("The h is : " + str(media.image["h"]))

Output :

The media_id is : 1273526215773573121
The media_id_string is : 1273526215773573121
The size is : 3346
The expires_after_secs is : 86400
The image_type is : image/png
The w is : 225
The h is : 225

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 :