File: streaming_api.rst

package info (click to toggle)
twython 3.8.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 600 kB
  • sloc: python: 1,793; makefile: 148; sh: 49
file content (51 lines) | stat: -rw-r--r-- 1,587 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
.. _streaming-api:

Streaming API
=============

This section will cover how to use Twython and interact with the Twitter Streaming API.

Streaming Documentation: https://developer.twitter.com/en/docs/tweets/filter-realtime/guides/streaming-message-types

.. important:: The Streaming API requires that you have OAuth 1 authentication credentials. If you don't have credentials, head over to the :ref:`authentication section <oauth1>` and find out how!

Setting Up Your Streamer
------------------------

.. note:: When stream data is sent back to Twython, we send the data through signals (i.e. ``on_success``, ``on_error``, etc.)

Make sure you import ``TwythonStreamer``

.. code-block:: python

    from twython import TwythonStreamer

Now set up how you want to handle the signals.

.. code-block:: python

    class MyStreamer(TwythonStreamer):
        def on_success(self, data):
            if 'text' in data:
                print(data['text'])

        def on_error(self, status_code, data):
            print(status_code)

            # Want to stop trying to get data because of the error?
            # Uncomment the next line!
            # self.disconnect()

More signals that you can extend on can be found in the Developer Interface section under :ref:`Streaming Interface <streaming_interface>`

Filtering Public Statuses
-------------------------

.. code-block:: python

    stream = MyStreamer(APP_KEY, APP_SECRET,
                        OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
    stream.statuses.filter(track='twitter')

With the code above, data should be flowing in.