AsyncStreamingClient

class tweepy.asynchronous.AsyncStreamingClient(bearer_token, *, return_type=Response, wait_on_rate_limit=False, max_retries=inf, proxy=None)

Stream realtime Tweets asynchronously with Twitter API v2

New in version 4.10.

Parameters
  • bearer_token (str) – Twitter API Bearer Token

  • return_type (type[dict | requests.Response | Response]) – Type to return from requests to the API

  • wait_on_rate_limit (bool) – Whether or not to wait before retrying when a rate limit is encountered. This applies to requests besides those that connect to a stream (see max_retries).

  • max_retries (int | None) – Number of times to attempt to (re)connect the stream.

  • proxy (str | None) – URL of the proxy to use when connecting to the stream

session

Aiohttp client session used to connect to the API

Type

aiohttp.ClientSession | None

task

The task running the stream

Type

asyncio.Task | None

user_agent

User agent used when connecting to the API

Type

str

async add_rules(add, *, dry_run)

This function is a coroutine.

Add rules to filtered stream.

Parameters
  • add (list[StreamRule] | StreamRule) – Specifies the operation you want to perform on the rules.

  • dry_run (bool) – Set to true to test the syntax of your rule without submitting it. This is useful if you want to check the syntax of a rule before removing one or more of your existing rules.

Returns

Return type

dict | requests.Response | Response

References

https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/post-tweets-search-stream-rules

async delete_rules(ids, *, dry_run)

This function is a coroutine.

Delete rules from filtered stream.

Parameters
  • ids (int | str | list[int | str | StreamRule] | StreamRule) – Array of rule IDs, each one representing a rule already active in your stream. IDs must be submitted as strings.

  • dry_run (bool) – Set to true to test the syntax of your rule without submitting it. This is useful if you want to check the syntax of a rule before removing one or more of your existing rules.

Returns

Return type

dict | requests.Response | Response

References

https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/post-tweets-search-stream-rules

filter(*, backfill_minutes=None, expansions=None, media_fields=None, place_fields=None, poll_fields=None, tweet_fields=None, user_fields=None)

Streams Tweets in real-time based on a specific set of filter rules.

If you are using the academic research product track, you can connect up to two redundant connections to maximize your streaming up-time.

The Tweets returned by this endpoint count towards the Project-level Tweet cap.

Parameters
  • backfill_minutes (int | None) –

    By passing this parameter, you can request up to five (5) minutes worth of streaming data that you might have missed during a disconnection to be delivered to you upon reconnection. The backfilled Tweets will automatically flow through the reconnected stream, with older Tweets generally being delivered before any newly matching Tweets. You must include a whole number between 1 and 5 as the value to this parameter.

    This feature will deliver duplicate Tweets, meaning that if you were disconnected for 90 seconds, and you requested two minutes of backfill, you will receive 30 seconds worth of duplicate Tweets. Due to this, you should make sure your system is tolerant of duplicate data.

    This feature is currently only available to the Academic Research product track.

  • expansions (list[str] | str) – expansions Parameter

  • media_fields (list[str] | str) – media_fields

  • place_fields (list[str] | str) – place_fields

  • poll_fields (list[str] | str) – poll_fields

  • tweet_fields (list[str] | str) – tweet_fields

  • user_fields (list[str] | str) – user_fields

Raises

TweepyException – When the stream is already connected

Returns

The task running the stream

Return type

asyncio.Task

References

https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream

async get_rules(*, ids)

This function is a coroutine.

Return a list of rules currently active on the streaming endpoint, either as a list or individually.

Parameters

ids (list[str] | str) – Comma-separated list of rule IDs. If omitted, all rules are returned.

Returns

Return type

dict | requests.Response | Response

References

https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/get-tweets-search-stream-rules

sample(*, backfill_minutes=None, expansions=None, media_fields=None, place_fields=None, poll_fields=None, tweet_fields=None, user_fields=None)

Streams about 1% of all Tweets in real-time.

If you are using the academic research product track, you can connect up to two redundant connections to maximize your streaming up-time.

Parameters
  • backfill_minutes (int | None) –

    By passing this parameter, you can request up to five (5) minutes worth of streaming data that you might have missed during a disconnection to be delivered to you upon reconnection. The backfilled Tweets will automatically flow through the reconnected stream, with older Tweets generally being delivered before any newly matching Tweets. You must include a whole number between 1 and 5 as the value to this parameter.

    This feature will deliver duplicate Tweets, meaning that if you were disconnected for 90 seconds, and you requested two minutes of backfill, you will receive 30 seconds worth of duplicate Tweets. Due to this, you should make sure your system is tolerant of duplicate data.

    This feature is currently only available to the Academic Research product track.

  • expansions (list[str] | str) – expansions Parameter

  • media_fields (list[str] | str) – media_fields

  • place_fields (list[str] | str) – place_fields

  • poll_fields (list[str] | str) – poll_fields

  • tweet_fields (list[str] | str) – tweet_fields

  • user_fields (list[str] | str) – user_fields

Raises

TweepyException – When the stream is already connected

Returns

The task running the stream

Return type

asyncio.Task

References

https://developer.twitter.com/en/docs/twitter-api/tweets/volume-streams/api-reference/get-tweets-sample-stream

async on_data(raw_data)

This function is a coroutine.

This is called when raw data is received from the stream. This method handles sending the data to other methods.

Parameters

raw_data (JSON) – The raw data from the stream

References

https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/integrate/consuming-streaming-data

async on_tweet(tweet)

This function is a coroutine.

This is called when a Tweet is received.

Parameters

tweet (Tweet) – The Tweet received

async on_includes(includes)

This function is a coroutine.

This is called when includes are received.

Parameters

includes (dict) – The includes received

async on_errors(errors)

This function is a coroutine.

This is called when errors are received.

Parameters

errors (dict) – The errors received

async on_matching_rules(matching_rules)

This function is a coroutine.

This is called when matching rules are received.

Parameters

matching_rules (list[StreamRule]) – The matching rules received

async on_response(response)

This function is a coroutine.

This is called when a response is received.

Parameters

response (StreamResponse) – The response received

disconnect()

Disconnect the stream

async on_closed(resp)

This function is a coroutine.

This is called when the stream has been closed by Twitter.

Parameters

response (aiohttp.ClientResponse) – The response from Twitter

async on_connect()

This function is a coroutine.

This is called after successfully connecting to the streaming API.

async on_connection_error()

This function is a coroutine.

This is called when the stream connection errors or times out.

async on_disconnect()

This function is a coroutine.

This is called when the stream has disconnected.

async on_exception(exception)

This function is a coroutine.

This is called when an unhandled exception occurs.

Parameters

exception (Exception) – The unhandled exception

async on_keep_alive()

This function is a coroutine.

This is called when a keep-alive signal is received.

async on_request_error(status_code)

This function is a coroutine.

This is called when a non-200 HTTP status code is encountered.

Parameters

status_code (int) – The HTTP status code encountered