Skip to content

Commit

Permalink
Remove Stream and AsyncStream
Browse files Browse the repository at this point in the history
  • Loading branch information
Harmon758 committed Mar 17, 2023
1 parent d2a6b83 commit c8a02eb
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 700 deletions.
12 changes: 0 additions & 12 deletions docs/asyncstream.rst

This file was deleted.

4 changes: 0 additions & 4 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,6 @@ Examples

.. literalinclude:: ../examples/API_v1/rate_limit_handling.py

.. tab:: Streaming

.. literalinclude:: ../examples/API_v1/streaming.py

.. tab:: API v2

.. tabs::
Expand Down
38 changes: 0 additions & 38 deletions docs/extended_tweets.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,6 @@ contain all entities. Additionally, the Status object will have a
identifying the inclusive start and exclusive end of the displayable content
of the Tweet.

Streaming
=========

By default, the Status objects from streams may contain an ``extended_tweet``
attribute representing the equivalent field in the raw data/payload for the
Tweet. This attribute/field will only exist for extended Tweets, containing a
dictionary of sub-fields. The ``full_text`` sub-field/key of this dictionary
will contain the full, untruncated text of the Tweet, and the ``entities``
sub-field/key will contain the full set of entities. If there are extended
entities, the ``extended_entities`` sub-field/key will contain the full set of
those. Additionally, the ``display_text_range`` sub-field/key will contain an
array of two Unicode code point indices, identifying the inclusive start and
exclusive end of the displayable content of the Tweet.

Handling Retweets
=================

Expand All @@ -83,12 +69,6 @@ containing the full text of the Retweet. However, since the
itself a Status object, the ``full_text`` attribute of the Retweeted Status
object can be used instead.

This also applies similarly to Status objects/payloads that are Retweets from
streams. The dictionary from the ``extended_tweet`` attribute/field contains a
``full_text`` sub-field/key that may be truncated with an ellipsis character.
Instead, the ``extended_tweet`` attribute/field of the Retweeted Status (from
the ``retweeted_status`` attribute/field) can be used.

Examples
========

Expand All @@ -104,24 +84,6 @@ full text of the Retweeted Tweet::

If ``status`` is a Retweet, ``status.full_text`` could be truncated.

This Status event handler for a :class:`Stream` prints the full text of the
Tweet, or if it's a Retweet, the full text of the Retweeted Tweet::

def on_status(self, status):
if hasattr(status, "retweeted_status"): # Check if Retweet
try:
print(status.retweeted_status.extended_tweet["full_text"])
except AttributeError:
print(status.retweeted_status.text)
else:
try:
print(status.extended_tweet["full_text"])
except AttributeError:
print(status.text)

If ``status`` is a Retweet, it will not have an ``extended_tweet`` attribute,
and ``status.text`` could be truncated.

.. rubric:: Footnotes

.. [#] https://twittercommunity.com/t/upcoming-changes-to-simplify-replies-and-links-in-tweets/67497
Expand Down
15 changes: 1 addition & 14 deletions docs/faq.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Why am I encountering a 403 Forbidden error with :class:`API`?
--------------------------------------------------------------

If you have Essential access to the Twitter API, you won't be able to access
Twitter API v1.1. This includes all :class:`API` methods and :class:`Stream`.
Twitter API v1.1. This includes all :class:`API` methods.

You can use Twitter API v2 with :class:`Client` or apply for Elevated access.

Expand Down Expand Up @@ -86,19 +86,6 @@ This is because :ref:`Tweepy v4.0.0 <Version 4.0.0 (2021-09-25)>` removed

Instead, you can use :meth:`API.verify_credentials`.

Where did ``StreamListener`` go?
--------------------------------

If you're attempting to import ``StreamListener`` with Tweepy v4, you'll get an
:class:`AttributeError` about ``tweepy`` not having a ``StreamListener``
attribute.

This is because :ref:`Tweepy v4.0.0 <Version 4.0.0 (2021-09-25)>` merged
``StreamListener`` into :class:`Stream`.

To use Tweepy v4, you'll need to update your code to subclass :class:`Stream`
instead.

Twitter API v2
==============

Expand Down
2 changes: 0 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ Contents:
:caption: Twitter API v1.1 Reference

api.rst
stream.rst
asyncstream.rst
exceptions.rst
v1_models.rst
v1_pagination.rst
Expand Down
12 changes: 0 additions & 12 deletions docs/stream.rst

This file was deleted.

100 changes: 21 additions & 79 deletions docs/streaming.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,60 +15,17 @@ results in a low-latency delivery mechanism that can support very high
throughput. For further information, see
https://developer.twitter.com/en/docs/tutorials/consuming-streaming-data

:class:`Stream` allows `filtering <v1.1 filtering_>`_ and
`sampling <v1.1 sampling_>`_ of realtime Tweets using Twitter API v1.1.
The Twitter API v1.1 streaming endpoints, `statuses/filter`_ and
`statuses/sample`_, have been deprecated and retired.

:class:`StreamingClient` allows `filtering <v2 filtering_>`_ and
`sampling <v2 sampling_>`_ of realtime Tweets using Twitter API v2.

.. _v1.1 filtering: https://developer.twitter.com/en/docs/twitter-api/v1/tweets/filter-realtime/overview
.. _v1.1 sampling: https://developer.twitter.com/en/docs/twitter-api/v1/tweets/sample-realtime/overview
.. _statuses/filter: https://twittercommunity.com/t/announcing-the-deprecation-of-v1-1-statuses-filter-endpoint/182960
.. _statuses/sample: https://twittercommunity.com/t/deprecation-announcement-removing-compliance-messages-from-statuses-filter-and-retiring-statuses-sample-from-the-twitter-api-v1-1/170500
.. _v2 filtering: https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/introduction
.. _v2 sampling: https://developer.twitter.com/en/docs/twitter-api/tweets/volume-streams/introduction

Using :class:`Stream`
=====================

To use :class:`Stream`, an instance of it needs to be initialized with Twitter
API credentials (Consumer Key, Consumer Secret, Access Token, Access Token
Secret)::

import tweepy

stream = tweepy.Stream(
"Consumer Key here", "Consumer Secret here",
"Access Token here", "Access Token Secret here"
)

Then, :meth:`Stream.filter` or :meth:`Stream.sample` can be used to connect to
and run a stream::

stream.filter(track=["Tweepy"])

Data received from the stream is passed to :meth:`Stream.on_data`. This method
handles sending the data to other methods based on the message type. For
example, if a Tweet is received from the stream, the raw data is sent to
:meth:`Stream.on_data`, which constructs a :class:`Status` object and passes it
to :meth:`Stream.on_status`. By default, the other methods, besides
:meth:`Stream.on_data`, that receive the data from the stream, simply log the
data received, with the :ref:`logging level <python:levels>` dependent on the
type of the data.

To customize the processing of the stream data, :class:`Stream` needs to be
subclassed. For example, to print the IDs of every Tweet received::

class IDPrinter(tweepy.Stream):

def on_status(self, status):
print(status.id)


printer = IDPrinter(
"Consumer Key here", "Consumer Secret here",
"Access Token here", "Access Token Secret here"
)
printer.sample()

Using :class:`StreamingClient`
==============================

Expand Down Expand Up @@ -122,57 +79,42 @@ subclassed. For example, to print the IDs of every Tweet received::

Threading
=========
:meth:`Stream.filter`, :meth:`Stream.sample`, :meth:`StreamingClient.filter`,
and :meth:`StreamingClient.sample` all have a ``threaded`` parameter. When set
to ``True``, the stream will run in a separate
:meth:`StreamingClient.filter` and :meth:`StreamingClient.sample` have a
``threaded`` parameter. When set to ``True``, the stream will run in a separate
:ref:`thread <python:thread-objects>`, which is returned by the call to the
method. For example::

thread = stream.filter(follow=[1072250532645998596], threaded=True)

or::

thread = streaming_client.sample(threaded=True)

Handling Errors
===============
Both :class:`Stream` and :class:`StreamingClient` have multiple methods to
handle errors during streaming.
:class:`StreamingClient` has multiple methods to handle errors during
streaming.

:meth:`Stream.on_closed` / :meth:`StreamingClient.on_closed` is called when the
stream is closed by Twitter.
:meth:`StreamingClient.on_closed` is called when the stream is closed by
Twitter.

:meth:`Stream.on_connection_error` /
:meth:`StreamingClient.on_connection_error` is called when the stream
encounters a connection error.

:meth:`Stream.on_request_error` / :meth:`StreamingClient.on_request_error` is
called when an error is encountered while trying to connect to the stream.
:meth:`StreamingClient.on_request_error` is called when an error is encountered
while trying to connect to the stream.

When these errors are encountered and ``max_retries``, which defaults to
infinite, hasn't been exceeded yet, the :class:`Stream` /
:class:`StreamingClient` instance will attempt to reconnect the stream after an
appropriate amount of time. By default, both versions of all three of these
methods log an error. To customize that handling, they can be overridden in a
subclass::

class ConnectionTester(tweepy.Stream):

def on_connection_error(self):
self.disconnect()

::
infinite, hasn't been exceeded yet, the :class:`StreamingClient` instance will
attempt to reconnect the stream after an appropriate amount of time. By
default, all three of these methods log an error. To customize that handling,
they can be overridden in a subclass::

class ConnectionTester(tweepy.StreamingClient):

def on_connection_error(self):
self.disconnect()

:meth:`Stream.on_request_error` / :meth:`StreamingClient.on_request_error` is
also passed the HTTP status code that was encountered. The HTTP status codes
reference for the Twitter API can be found at
:meth:`StreamingClient.on_request_error` is also passed the HTTP status code
that was encountered. The HTTP status codes reference for the Twitter API can
be found at
https://developer.twitter.com/en/support/twitter-api/error-troubleshooting.

:meth:`Stream.on_exception` / :meth:`StreamingClient.on_exception` is called
when an unhandled exception occurs. This is fatal to the stream, and by
default, an exception is logged.
:meth:`StreamingClient.on_exception` is called when an unhandled exception
occurs. This is fatal to the stream, and by default, an exception is logged.
22 changes: 0 additions & 22 deletions examples/API_v1/streaming.py

This file was deleted.

2 changes: 1 addition & 1 deletion tweepy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from tweepy.poll import Poll
from tweepy.space import Space
from tweepy.streaming import (
Stream, StreamingClient, StreamResponse, StreamRule
StreamingClient, StreamResponse, StreamRule
)
from tweepy.tweet import ReferencedTweet, Tweet
from tweepy.user import User
Expand Down
2 changes: 1 addition & 1 deletion tweepy/asynchronous/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@

from tweepy.asynchronous.client import AsyncClient
from tweepy.asynchronous.pagination import AsyncPaginator
from tweepy.asynchronous.streaming import AsyncStream, AsyncStreamingClient
from tweepy.asynchronous.streaming import AsyncStreamingClient

0 comments on commit c8a02eb

Please sign in to comment.