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¶
- defget_tweet
- defget_user
- defget_user_by_username
- deftweet
- 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
- 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 username¶
Return the user’s username, this usually start with ‘@’ follow by their username.
- Type
- property link¶
Return url where the user put links, return an empty string if there isn’t a url
- Type
- property created_at¶
datetime.datetime: Return datetime.datetime object with the user’s account date.
- Type
class
- 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]
- 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 following_count¶
Returns total of following that a user has.
New in version 1.1.0.
- Type
- 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 byMessage
,- 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 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.
- 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.
- property sensitive¶
Return True if the tweet is possible sensitive to some users, else False
- Type
- property created_at¶
Return a datetime object with the tweet posted age.
- Type
- 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
- property reply_setting¶
Return the reply setting. If everyone can replied, reply_setting return ‘Everyone’.
- Type
- 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 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
- 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 retweet_count¶
Return total of retweetes that the tweet has.
New in version 1.1.0.
- Type
- 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 created_at¶
Returns the time when the Direct Message event was created.
New in version 1.2.0.
- Type
- 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 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 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
- property end_date¶
Return the end date in datetime.datetime object.
New in version 1.1.0.
- Type
datetime.datetime
- 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]
- class pytweet.RelationFollow(data)¶
Represent the follow relation from a follow & unfollow request.
New in version 1.2.0.
- property type¶
Check what relation type it is.
New in version 1.2.0.
- Type
RelationTypeEnum
- class pytweet.RelationLike(data)¶
Represent the like relation from a like & unlike request.
New in version 1.2.0.
- property type¶
Check what relation type it is.
New in version 1.2.0.
- Type
RelationTypeEnum
- 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
- 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 images¶
Return a list of Embed’s Images
New in version 1.1.3.
- Type
List[EmbedsImages]
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: 401New 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: 429New 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.