twitterapi.tweets.tweets module

tweets.py: Implementation of Twitter tweets endpoint.

“While there are a variety of different HTTP, selection, and delivery methods that can deliver, publish, and act upon Tweets, this group of REST endpoints simply returns a Tweet or group of Tweets, specified by a Tweet ID. While simple, these endpoints can be used to receive up-to-date details on a Tweet, verify that a Tweet is available, and examine its edit history. These endpoints are also important tools for managing compliance events.” (Source: https://developer.twitter.com/en/docs/twitter-api/tweets/search/introduction)

Find the Open API Spec under: https://api.twitter.com/2/openapi.json

Examples

Get Tweets by ID:

import os
import json
os.environ["BEARER_TOKEN"] = "xxxxxxxxxxx"
from sparta.twitterapi.tweets.tweets import get_tweets_by_id

async for tweet_response in get_tweets_by_id(['1511275800758300675', '1546866845180887040']):
    print(json.dumps(tweet_response.tweet))
    print(json.dumps(tweet_response.includes))
async sparta.twitterapi.tweets.tweets.get_tweets_by_id(ids: List[str]) AsyncGenerator[TweetResponse, None]

Asynchronously retrieves tweets by their IDs.

This function handles the retrieval of tweets from Twitter’s API based on a list of tweet IDs. It respects the rate limiting by utilizing an internal RateLimiter instance. If the rate limit is exceeded, the function will automatically wait until it can proceed with requests.

Parameters:

ids (List[str]) – A list of tweet IDs for which to retrieve tweets. Up to 100 IDs can be included in a single request.

Returns:

An asynchronous generator that yields TweetResponse objects for each tweet.

Return type:

AsyncGenerator[TweetResponse, None]

Raises:

Exception – If an HTTP error occurs that prevents retrieving the tweets.

Yields:

TweetResponse – An object representing the tweet data for each given tweet ID.