Skip to content

Commit d775152

Browse files
authored
Merge pull request #7 from twitivity/fix/pagination
fix/pagination
2 parents 6bbea94 + ffa3d5d commit d775152

File tree

2 files changed

+29
-18
lines changed

2 files changed

+29
-18
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
DESCRIPTION: str = "Python Client For Twitter Streaming API v2"
77
URL: str = "https://github.com/twitivity/twitter-stream.py"
88
REQUIRES_PYTHON: str = ">=3.6.0"
9-
VERSION = "0.7.2"
9+
VERSION = "0.7.3"
1010
REQUIRED = ["requests", "requests-oauthlib"]
1111
1212

twitter_stream.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class API:
1313
_product: str = None
1414
_endpoint: str = None
1515
_has_params: bool = None
16+
_pagination: bool = False
1617
_params: dict = {}
1718
_stream: bool = None
1819
_data: dict = None
@@ -72,15 +73,24 @@ def connect(self) -> dict:
7273
if self._has_params:
7374
self._params = self._query()
7475

75-
response = self.api(
76-
method="GET",
77-
endpoint=self._endpoint,
78-
stream=self._stream,
79-
params=self._params,
80-
)
81-
response.raise_for_status()
82-
for response_lines in response.iter_lines():
83-
yield json.loads(response_lines)
76+
data = None
77+
78+
while True:
79+
response = self.api(
80+
method="GET",
81+
endpoint=self._endpoint,
82+
stream=self._stream,
83+
params=self._params,
84+
)
85+
response.raise_for_status()
86+
for response_lines in response.iter_lines():
87+
data = json.loads(response_lines)
88+
yield data
89+
90+
if not self._pagination:
91+
break
92+
self._params["next_token"] = data["meta"]["next_token"]
93+
8494
except Exception as e:
8595
raise e
8696

@@ -222,6 +232,7 @@ class RecentSearch(API):
222232
"start_time",
223233
"until_id",
224234
]
235+
_pagination = True
225236

226237

227238
class TweetLookUp(API):
@@ -278,20 +289,20 @@ def hide_replies(tweet: str, hidden: dict) -> json:
278289
Hides specific tweets from a conversation
279290
:params: tweet: str = url of the tweet to hide
280291
:params: hidden: dict = {"hidden: True}
281-
282-
:returns json
283-
292+
293+
:returns json
294+
284295
Usage:
285-
296+
286297
hide_replies(
287298
tweet = 'https://twitter.com/saadmanrafat_/status/1328288598106443776',
288299
{"hidden: True}
289300
)
290-
291-
response =
301+
302+
response =
292303
{
293304
"data": {
294-
"hidden": true
305+
"hidden": true
295306
}
296307
}
297308
"""
@@ -310,4 +321,4 @@ def hide_replies(tweet: str, hidden: dict) -> json:
310321
auth=auth,
311322
data=json.dumps(hidden),
312323
)
313-
return json.dumps(response.json(), indent=4)
324+
return json.dumps(response.json(), indent=4)

0 commit comments

Comments
 (0)