Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit e8c1084

Browse files
committed
add wrapper for Stream in API v2
1 parent 96e33e9 commit e8c1084

4 files changed

Lines changed: 55 additions & 10 deletions

File tree

README

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ The list of most accessible functions is listed at:
3737

3838
**[https://developer.twitter.com/en/docs/api-reference-index](https://developer.twitter.com/en/docs/api-reference-index)**
3939

40+
4041
Examples with Twitter API v2:
41-
_____________________________
42+
+++++++++++++++++++++++++++++
4243

4344
```python
4445
from twitter import *
@@ -76,10 +77,35 @@ t.tweets(
7677
"poll.fields": "duration_minutes,end_datetime,id,options,voting_status"
7778
})
7879

80+
# The Stream also has its own wrapper:
81+
ts = TwitterStream2(auth=OAuth2(bearer_token=BEARER_TOKEN))
82+
83+
# Then calling it behaves like python generators. For instance the sample stream:
84+
for tweet in ts.tweets.sample.stream():
85+
print(tweet)
86+
87+
# The same applies to the regular filter stream:
88+
for tweet in ts.tweets.search.stream():
89+
print(tweet)
90+
91+
# Although it does not anymore take any filter argument and needs to be
92+
# controlled separately using the regular Twitter class.
93+
# To know which rules apply, just call:
94+
t.tweets.search.stream.rules()
95+
# And to add new rules, send POST json queries to the same route:
96+
t.tweets.search.stream.rules(_json={"add": [{"value": "python"}]})
97+
98+
# Then call the stream as described above:
99+
for tweet in ts.tweets.search.stream():
100+
print(tweet)
101+
102+
# Get more details on rules possibilities and syntax in the officiel documentation:
103+
# https://developer.twitter.com/en/docs/twitter-api/tweets/filtered-stream/api-reference/post-tweets-search-stream-rules
104+
79105
```
80106

81107
Examples with Twitter API Standard v1.1:
82-
________________________________________
108+
++++++++++++++++++++++++++++++++++++++++
83109

84110
```python
85111
from twitter import *

twitter/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from .oauth2 import (
1717
OAuth2, read_bearer_token_file, write_bearer_token_file,
1818
__doc__ as oauth2_doc)
19-
from .stream import TwitterStream
19+
from .stream import TwitterStream, TwitterStream2
2020
from .oauth_dance import oauth_dance, oauth2_dance
2121

2222
__doc__ = __doc__ or ""
@@ -78,6 +78,7 @@
7878
"TwitterHTTPError",
7979
"TwitterResponse",
8080
"TwitterStream",
81+
"TwitterStream2",
8182
"UserPassAuth",
8283
"write_bearer_token_file",
8384
"write_token_file",

twitter/api.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -622,17 +622,15 @@ def __init__(
622622
class Twitter2(Twitter):
623623

624624
def __init__(
625-
self, secure=True, auth=None, retry=False, verify_context=True):
626-
627-
Twitter.__init__(
628625
self,
629626
domain="api.twitter.com",
630627
api_version="2",
631628
format="",
632-
auth=auth,
633-
retry=retry,
634-
secure=secure,
635-
verify_context=verify_context)
629+
secure=True, auth=None, retry=False, verify_context=True):
630+
631+
Twitter.__init__(
632+
self, domain=domain, api_version=api_version, format=format,
633+
auth=auth, retry=retry, secure=secure, verify_context=verify_context)
636634

637635

638636
__all__ = ["Twitter", "Twitter2", "TwitterError", "TwitterHTTPError", "TwitterResponse"]

twitter/stream.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,23 @@ def _handle_response(self, req, uri, arg_data, _timeout=None):
278278
callable_cls=TwitterStreamCall,
279279
secure=secure, uriparts=uriparts, timeout=timeout, gzip=False,
280280
retry=False, verify_context=verify_context)
281+
282+
class TwitterStream2(TwitterCall):
283+
284+
def __init__(self, domain="api.twitter.com", secure=True, auth=None,
285+
api_version='2', block=True, timeout=None,
286+
heartbeat_timeout=90.0, verify_context=True):
287+
uriparts = (str(api_version),)
288+
289+
class TwitterStream2Call(TwitterCall):
290+
def _handle_response(self, req, uri, arg_data, _timeout=None):
291+
return handle_stream_response(
292+
req, uri, arg_data, block,
293+
_timeout or timeout, heartbeat_timeout, verify_context)
294+
295+
TwitterCall.__init__(
296+
self, auth=auth, format="", domain=domain,
297+
callable_cls=TwitterStream2Call,
298+
secure=secure, uriparts=uriparts, timeout=timeout, gzip=False,
299+
retry=False, verify_context=verify_context)
300+

0 commit comments

Comments
 (0)