Skip to content

Method: Timelines

Matthew R. DeVerna edited this page Aug 28, 2021 · 6 revisions

Using the user timelines endpoints, you can download the most recent tweets composed by or mentioning a specific user ID.

Pull tweets composed by a specific user_id (Tweet Timeline - get_tweet_timeline())

import osometweet

# Initialize the OSoMeTweet object
bearer_token = "YOUR_TWITTER_BEARER_TOKEN"
oauth2 = osometweet.OAuth2(bearer_token=bearer_token)
ot = osometweet.OsomeTweet(oauth2)

# Call the function to get "@jack"'s 10 most recent tweets
# The endpoint only supports user id, we pass the id of @jack to the method
response = ot.get_tweet_timeline('12')

Pull tweets mentioning a specific user_id (Mentions Timeline - get_mentions_timeline())

import osometweet

# Initialize the OSoMeTweet object
bearer_token = "YOUR_TWITTER_BEARER_TOKEN"
oauth2 = osometweet.OAuth2(bearer_token=bearer_token)
ot = osometweet.OsomeTweet(oauth2)

# Call the function to get "@jack"'s 10 most recent mentions
# The endpoint only supports user id, we pass the id of @jack to the method
response = ot.get_mentions_timeline('12')

Specifying the number of tweets returned

By default, 10 tweets are returned in each query. However, you can request up to 100 at a time using the max_results parameter. This has large implications with respect to query limits (i.e. how many tweets you can get with the same number of requests). Here is an example:

Call the function to get "jack"'s 100 most recent followers

response = ot.get_tweet_timeline('12', max_results = 100)

Note: The same method applies for the get_metions_timeline() method

Pagination

For each user ID, Twitter allows you to request up to 3,200 of the most recent tweets, and up to 800 of the most recent tweets mentioning a user. Since you can only request (at most) 100 tweets at a time, you will need to utilize the pagination_token returned in the meta-data of the response. For example, to get the 200 most recent tweets you can do the following

Call the function to get "jack"'s 100 most recent tweets

response = ot.get_tweet_timeline('12', max_results = 100)

Call the function again to get the next 100 tweets:

response_2 = ot.get_tweet_timeline('12', pagination_token=response['meta']['next_token'], max_results = 100)

Note: The same approach can be utilized for the get_metions_timeline() method

Fields and Expansions

Including expansions and fields works exactly the same as other methods. See the Method: Specifying fields and expansions page for details.

Optional Paramters

To understand the optional paramters, please utilize Twitter's documentation (linked to at the top of this page) or reference the docstring of each method by calling help(ot.get_tweet_timeline()) and/or help(ot.get_mentions_timeline()) or pressing Shift + Tab to print/show the doc string of a method while using Jupyter Notebooks.