API Reference

The following section outlines the API of pytweet.

Note

This module uses the Python logging module to log to tell you what you are doing wrong, See Logging for more info.

Client

Attributes
class pytweet.Client(bearer_token, *, consumer_key=None, consumer_key_secret=None, access_token=None, access_token_secret=None)

Represent a client that connected to Twitter! This client will interact with other through twitter’s api version 2!

bearer_token: str

The Bearer Token of the app. The most important one, because this make most of the requests for twitter’s api version 2.

consumer_key: Optional[str]

The Consumer Key of the app.

consumer_key_secret: Optional[str]

The Consumer Key Secret of the app.

access_token: Optional[str]

The Access Token of the app.

access_token_secret: Optional[str]

The Access Token Secret of the app.

http: Optional[HTTPClient]

Return a HTTPClient, HTTPClient is responsible for making most of the Requests.

New in version 1.0.0.

property user

Returns the client in user object, return None if access token isn’t specified.

New in version 1.2.0.

Type

User

get_user(user_id)

A function for HTTPClient.fetch_user().

user_id: Union[str, int]

Represent the user id that you wish to get info to, If you dont have it you may use fetch_user_byusername because it only required the user’s username.

This function returns a User object.

New in version 1.0.0.

get_user_by_username(username)

A function for HTTPClient.fetch_user_byusername().

username: str

Represent the user’s username that you wish to get info. A Username usually start with ‘@’ before any letters. If a username named @Jack,then the username argument must be ‘Jack’.

This function returns a User object.

New in version 1.0.0.

get_tweet(tweet_id)

A function for HTTPClient.fetch_tweet().

tweet_id: Union[str, int]

Represent the tweet id that you wish to get info to.

This function returns a Tweet.

New in version 1.0.0.

tweet(text, **kwargs)

Post a tweet directly to twitter from the given parameters.

text: str

The tweets text, it will showup as the main text in a tweet.

New in version 1.1.0.

Commonly used Classes

There a lot of commonly used classes that is used frequently when using PyTweet, and here they are.

class pytweet.User(data, **kwargs)

Represent a user in Twitter. User is an identity in twitter, its very interactive. Can send message, post a tweet, and even send messages to other user through Dms.

x == y
Check if one user id is equal to another.
x != y
Check if one user id is not equal to another.
str(x)
Get the user's name.
property name

Return the user’s name.

Type

str

property username

Return the user’s username, this usually start with ‘@’ follow by their username.

Type

str

property id

Return the user’s id.

Type

int

property bio

Return the user’s bio.

Type

str

property description

an alias to User.bio.

Type

str

Return the user’s profile link

Type

str

Return url where the user put links, return an empty string if there isn’t a url

Type

str

property verified

Return True if the user is verified account, else False.

Type

bool

property protected

Return True if the user is protected, else False.

Type

bool

property avatar_url

Return the user profile image.

Type

Optional[str]

property location

Return the user’s location

Type

str

property created_at

datetime.datetime: Return datetime.datetime object with the user’s account date.

Type

class

property pinned_tweet

Returns the user’s pinned tweet.

Type

Optional[object]

property followers

Returns a list of users who are followers of the specified user ID. Maximum users is 100 users.

Type

List[User]

property following

Returns a list of users that’s followed by the specified user ID. Maximum users is 100 users.

Type

List[User]

property follower_count

Return total of followers that a user has.

Type

int

property following_count

Return total of following that a user has.

Type

int

property tweet_count

Return total of tweet that a user has.

Type

int

property listed_count

Return total of listed that a user has.

Type

int

class pytweet.UserPublicMetrics(data={}, **kwargs)

Represent a PublicMetrics for a User. This PublicMetrics contain public info about the user.

New in version 1.1.0.

property follower_count

Returns total of followers that a user has.

New in version 1.1.0.

Type

int

property following_count

Returns total of following that a user has.

New in version 1.1.0.

Type

int

property tweet_count

Returns total of tweet that a user has.

New in version 1.1.0.

Type

int

property listed_count

Returns total of listed that a user has.

New in version 1.1.0.

Type

int

class pytweet.Tweet(data, **kwargs)

Represent a tweet message from Twitter. A Tweet is any message posted to Twitter which may contain photos, videos, links, and text.

x == y
Check if one tweet id is equal to another.
x != y
Check if one tweet id is not equal to another.
str(x)
Get the Tweet's text.

New in version 1.0.0.

Changed in version 1.2.0.

Inherite Message. The text and ID property is now provided by Message,

like()

RelationLike: Method for liking a tweet.

New in version 1.2.0.

unlike()

RelationLike: Method for unliking a tweet.

New in version 1.2.0.

retweet()

RelationRetweet: Method for retweet a tweet.

New in version 1.2.0.

unretweet()

RelationRetweet: Method for unretweet a tweet.

New in version 1.2.0.

property author

Return a user (object) who posted the tweet.

Type

Optional[User]

property retweetes

Return a list of users that’s retweeted the specified tweet’s id. Maximum users is 100. Return empty list if no one retweeted.

Type

class`Optional[List[User], int]`

property likes

Return a list of users that liked the specified tweet’s id. Maximum users is 100. Return empty list if no one liked.

Type

class`Optional[List[User], int]`

property sensitive

Return True if the tweet is possible sensitive to some users, else False

Type

bool

property created_at

Return a datetime object with the tweet posted age.

Type

datetime.datetime

property source

Return the source of the tweet. e.g if you post a tweet from a website, the source is gonna be ‘Twitter Web App’

Type

str

property reply_setting

Return the reply setting. If everyone can replied, reply_setting return ‘Everyone’.

Type

str

property lang

Return the tweet’s lang, if its english it return en.

Type

str

property conversation_id

Return the tweet’s conversation’s id.

Type

int

Return the tweet’s link.

Type

str

property reply_to

Return the user that you reply with the tweet, a tweet count as reply tweet if the tweet startswith @Username or mention a user.

New in version 1.1.3.

Type

Optional[User]

property mentions

Return the mentioned users, if there isn’t it return None.

New in version 1.1.3.

Type

Optional[List[User]]

property poll

Return a Poll object with the tweet’s poll.

New in version 1.1.0.

Type

Poll

property media

List[Media] -> Return a list of media(s) in a tweet.

New in version 1.1.0.

property embeds

Return a list of Embedded url from that tweet

New in version 1.1.3.

Type

List[Embed]

property type

Return the Message type.

New in version 1.2.0.

Type

MessageTypeEnum

property like_count

Return the total of likes in a tweet.

Type

int

property retweet_count

Return the total of retweetes in a tweet.

Type

int

property reply_count

Return the total of replies in a tweet.

Type

int

property quote_count

Return the total of quotes in a tweet.

Type

int

class pytweet.TweetPublicMetrics(data={}, **kwargs)

Represent a PublicMetrics for a tweet. This PublicMetrics contain public info about the tweet.

New in version 1.1.0.

property like_count

Return total of likes that the tweet has.

New in version 1.1.0.

Type

int

property retweet_count

Return total of retweetes that the tweet has.

New in version 1.1.0.

Type

int

property reply_count

Return total of replies that the tweet has.

New in version 1.1.0.

Type

int

property quote_count

Return total of quotes that the tweet has.

New in version 1.1.0.

Type

int

class pytweet.Message(text, id)

Represents the base Message of all Message types in Twitter, this include DirrectMessage & Tweet

text: Optional[str]

The messages’s text.

id: Union[str, int]

The messages’s ID or event id for DirectMessage.

New in version 1.2.0.

class pytweet.DirectMessage(data, **kwargs)

Represents a Direct Message in Twitter.

New in version 1.2.0.

property event_type

Returns the message event type.

New in version 1.2.0.

Type

MessageEventTypeEnum

property type

Returns the message type.

New in version 1.2.0.

Type

MessageTypesEnum

property author

Returns the author of the message in User object.

New in version 1.2.0.

Type

User

property created_at

Returns the time when the Direct Message event was created.

New in version 1.2.0.

Type

datetime.datetime

property hashtags

Returns the messages’s hashtags.

New in version 1.2.0.

Type

List[str]

property symbol

Returns the messages’s hashtags.

New in version 1.2.0.

Type

List[str]

property mentions

Returns the messages usermetions.

New in version 1.2.0.

Type

List[UserMentions]

property urls

Returns the message’s urls.

New in version 1.2.0.

Type

List[UserMentions]

class pytweet.Media(data)

Represent a Media attachment in a tweet.

x == y
Check if one Media key is equal to another.
x != y
Check if one Media key is not equal to another.
str(x)
Get the media url.

New in version 1.1.0.

property type

Return the media’s type.

New in version 1.1.0.

Type

Optional[str]

property url

Return the media’s url.

New in version 1.1.0.

Type

Optional[str]

property width

the media’s width.

New in version 1.1.0.

Type

Optional[int]

property height

Return the media’s height.

New in version 1.1.0.

Type

int

property media_key

Returns the media’s unique key.

New in version 1.1.0.

Type

Optional[Union[int, str]]

class pytweet.Poll(data)

Represent a Poll attachment in a tweet.

x == y
Check if one Poll's id is equal to another.
x != y
Check if one Poll's id is not equal to another.
len(x)
return how many options in the poll.
bool(x)
return True if the poll is open else it return False.

New in version 1.1.0.

property id

Return the poll’s unique ID.

New in version 1.1.0.

Type

int

property options

Return a list of PollOptions.

New in version 1.1.0.

Type

List[PollOptions]

property voting_status

Return True if the poll is still open for voting, if its closed it return False.

New in version 1.1.0.

Type

bool

property duration

Return the poll duration in seconds.

New in version 1.1.0.

Type

int

property end_date

Return the end date in datetime.datetime object.

New in version 1.1.0.

Type

datetime.datetime

Attributes
class pytweet.PollOptions(options)

Represent the Poll Options, The minimum option of a poll is 2 and maximum is 4.

x == y
Check if one PollOption position is equal to another.
x != y
Check if one PollOption position is not equal to another.
x > y
Check if one PollOption position is higher then to another.
x < y
Check if one PollOption position is less then to another.
x >= y
Check if one PollOption position is higher then equal to another.
x <= y
Check if one PollOption position is less then equal to another.

New in version 1.1.0.

property position

The option’s position.

New in version 1.1.0.

Type

Optional[int]

property label

The option’s label.

New in version 1.1.0.

Type

Optional[int]

property votes

The option’s votes.

New in version 1.1.0.

Type

Optional[int]

Attributes
class pytweet.RelationFollow(data)

Represent the follow relation from a follow & unfollow request.

New in version 1.2.0.

property pending

Check if the relation is pending.

New in version 1.2.0.

Type

bool

property following

Check if the relation is following.

New in version 1.2.0.

Type

bool

property type

Check what relation type it is.

New in version 1.2.0.

Type

RelationTypeEnum

Attributes
class pytweet.RelationLike(data)

Represent the like relation from a like & unlike request.

New in version 1.2.0.

property liked

Return True if user liked the tweet else False.

New in version 1.2.0.

Type

bool

property type

Check what relation type it is.

New in version 1.2.0.

Type

RelationTypeEnum

Attributes
class pytweet.RelationRetweet(data)

Represent the retweet relations from a retweet & unretweet request.

New in version 1.2.0.

property retweeted

Return True if user retweeted the tweet else False.

New in version 1.2.0.

Type

bool

property type

Check what relation type it is.

New in version 1.2.0.

Type

RelationTypeEnum

class pytweet.Embed(data)

Represent the embedded urls in a tweet.

property title

Return the embed’s title

Type

str

property description

Return the embed’s description

Type

str

property start

Return the embed’s url startpoint start

Type

int

property end

Return the embed’s url endpoint.

Type

int

property url

Return the embed’s url

Type

str

property expanded_url

Return the expanded url

Type

str

property display_url

Return the display url

Type

str

property unwound_url

Return the unwound url

Type

str

property images

Return a list of Embed’s Images

New in version 1.1.3.

Type

List[EmbedsImages]

property status_code

Return the embed’s url HTTP status code

Type

int

Attributes
class pytweet.EmbedsImages(data)

Represent the tweets embed images.

property width

Return the image’s width

Type

int

property height

Return the image’s height

Type

int

property url

Return the image’s url

Type

str

Errors

Error raised by pytweet.

exception pytweet.PytweetException(response=None, message='No Error Message Provided')

Exception: This is the base class of all exceptions.

New in version 1.2.0.

exception pytweet.APIException(response=None, message='No Error Message Provided')

PytweetException: Raise When an error is incurred during a request with HTTP Status code 200.

New in version 1.2.0.

exception pytweet.HTTPException(response=None, message='No Error Message Provided')

PytweetException: A custom error that will be raised when ever a request return HTTP status code above 200.

New in version 1.2.0.

exception pytweet.Unauthorized(response=None, message='No Error Message Provided')

HTTPException: Raised when the Credentials you passed is invalid and a request return status code: 401

New in version 1.0.0.

exception pytweet.TooManyRequests(response=None, message='No Error Message Provided')

HTTPException: Raised when ratelimit exceeded and a request return status code: 429

New in version 1.1.0.

exception pytweet.Forbidden(response=None, message=None)

HTTPException: Raised when a request return status code: 403.

New in version 1.2.0.

exception pytweet.NotFound(response=None, message='No Error Message Provided')

This error is returned when a given Tweet, User, etc. does not exist.

New in version 1.0.0.