forked from xdevplatform/search-tweets-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
result_stream.py
311 lines (262 loc) · 11.1 KB
/
result_stream.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# -*- coding: utf-8 -*-
# Copyright 2018 Twitter, Inc.
# Licensed under the MIT License
# https://opensource.org/licenses/MIT
"""
This module contains the request handing and actual API wrapping functionality.
Its core method is the ``ResultStream`` object, which takes the API call
arguments and returns a stream of results to the user.
"""
import time
import re
import logging
import requests
from ratelimit import limits, sleep_and_retry
try:
import ujson as json
except ImportError:
import json
from tweet_parser.tweet import Tweet
from .utils import merge_dicts
from .api_utils import infer_endpoint, change_to_count_endpoint
from ._version import VERSION
logger = logging.getLogger(__name__)
def make_session(username=None, password=None, bearer_token=None, extra_headers_dict=None):
"""Creates a Requests Session for use. Accepts a bearer token
for premiums users and will override username and password information if
present.
Args:
username (str): username for the session
password (str): password for the user
bearer_token (str): token for a premium API user.
"""
if password is None and bearer_token is None:
logger.error("No authentication information provided; "
"please check your object")
raise KeyError
session = requests.Session()
session.trust_env = False
headers = {'Accept-encoding': 'gzip',
'User-Agent': 'twitterdev-search-tweets-python/' + VERSION}
if bearer_token:
logger.info("using bearer token for authentication")
headers['Authorization'] = "Bearer {}".format(bearer_token)
session.headers = headers
else:
logger.info("using username and password for authentication")
session.auth = username, password
session.headers = headers
if extra_headers_dict:
headers.update(extra_headers_dict)
return session
def retry(func):
"""
Decorator to handle API retries and exceptions. Defaults to three retries.
Args:
func (function): function for decoration
Returns:
decorated function
"""
def retried_func(*args, **kwargs):
max_tries = 3
tries = 0
while True:
try:
resp = func(*args, **kwargs)
except requests.exceptions.ConnectionError as exc:
exc.msg = "Connection error for session; exiting"
raise exc
except requests.exceptions.HTTPError as exc:
exc.msg = "HTTP error for session; exiting"
raise exc
if resp.status_code != 200 and tries < max_tries:
logger.warning("retrying request; current status code: {}"
.format(resp.status_code))
tries += 1
# mini exponential backoff here.
time.sleep(tries ** 2)
continue
break
if resp.status_code != 200:
error_message = resp.json()["error"]["message"]
logger.error("HTTP Error code: {}: {}".format(resp.status_code, error_message))
logger.error("Rule payload: {}".format(kwargs["rule_payload"]))
raise requests.exceptions.HTTPError
return resp
return retried_func
@sleep_and_retry
@limits(calls=10, period=1)
@limits(calls=30, period=60)
@retry
def request(session, url, rule_payload, **kwargs):
"""
Executes a request with the given payload and arguments.
Args:
session (requests.Session): the valid session object
url (str): Valid API endpoint
rule_payload (str or dict): rule package for the POST. If you pass a
dictionary, it will be converted into JSON.
"""
if isinstance(rule_payload, dict):
rule_payload = json.dumps(rule_payload)
logger.debug("sending request")
result = session.post(url, data=rule_payload, **kwargs)
return result
class ResultStream:
"""
Class to represent an API query that handles two major functionality
pieces: wrapping metadata around a specific API call and automatic
pagination of results.
Args:
username (str): username for enterprise customers
password (str): password for enterprise customers
bearer_token (str): bearer token for premium users
endpoint (str): API endpoint; see your console at developer.twitter.com
rule_payload (json or dict): payload for the post request
max_results (int): max number results that will be returned from this
instance. Note that this can be slightly lower than the total returned
from the API call - e.g., setting ``max_results = 10`` would return
ten results, but an API call will return at minimum 100 results.
tweetify (bool): If you are grabbing tweets and not counts, use the
tweet parser library to convert each raw tweet package to a Tweet
with lazy properties.
max_requests (int): A hard cutoff for the number of API calls this
instance will make. Good for testing in sandbox premium environments.
extra_headers_dict (dict): custom headers to add
Example:
>>> rs = ResultStream(**search_args, rule_payload=rule, max_pages=1)
>>> results = list(rs.stream())
"""
# leaving this here to have an API call counter for ALL objects in your
# session, helping with usage of the convenience functions in the library.
session_request_counter = 0
def __init__(self, endpoint, rule_payload, username=None, password=None,
bearer_token=None, extra_headers_dict=None, max_results=500,
tweetify=True, max_requests=None, **kwargs):
self.username = username
self.password = password
self.bearer_token = bearer_token
self.extra_headers_dict = extra_headers_dict
if isinstance(rule_payload, str):
rule_payload = json.loads(rule_payload)
self.rule_payload = rule_payload
self.tweetify = tweetify
# magic number of max tweets if you pass a non_int
self.max_results = (max_results if isinstance(max_results, int)
else 10 ** 15)
self.total_results = 0
self.n_requests = 0
self.session = None
self.current_tweets = None
self.next_token = None
self.stream_started = False
self._tweet_func = Tweet if tweetify else lambda x: x
# magic number of requests!
self.max_requests = (max_requests if max_requests is not None
else 10 ** 9)
self.endpoint = (change_to_count_endpoint(endpoint)
if infer_endpoint(rule_payload) == "counts"
else endpoint)
# validate_count_api(self.rule_payload, self.endpoint)
def stream(self):
"""
Main entry point for the data from the API. Will automatically paginate
through the results via the ``next`` token and return up to ``max_results``
tweets or up to ``max_requests`` API calls, whichever is lower.
Usage:
>>> result_stream = ResultStream(**kwargs)
>>> stream = result_stream.stream()
>>> results = list(stream)
>>> # or for faster usage...
>>> results = list(ResultStream(**kwargs).stream())
"""
self.init_session()
self.check_counts()
self.execute_request()
self.stream_started = True
while True:
for tweet in self.current_tweets:
if self.total_results >= self.max_results:
break
yield self._tweet_func(tweet)
self.total_results += 1
if self.next_token and self.total_results < self.max_results and self.n_requests <= self.max_requests:
self.rule_payload = merge_dicts(self.rule_payload,
{"next": self.next_token})
logger.info("paging; total requests read so far: {}"
.format(self.n_requests))
self.execute_request()
else:
break
logger.info("ending stream at {} tweets".format(self.total_results))
self.current_tweets = None
self.session.close()
def init_session(self):
"""
Defines a session object for passing requests.
"""
if self.session:
self.session.close()
self.session = make_session(self.username,
self.password,
self.bearer_token,
self.extra_headers_dict)
def check_counts(self):
"""
Disables tweet parsing if the count API is used.
"""
if "counts" in re.split("[/.]", self.endpoint):
logger.info("disabling tweet parsing due to counts API usage")
self._tweet_func = lambda x: x
def execute_request(self):
"""
Sends the request to the API and parses the json response.
Makes some assumptions about the session length and sets the presence
of a "next" token.
"""
if self.n_requests % 20 == 0 and self.n_requests > 1:
logger.info("refreshing session")
self.init_session()
resp = request(session=self.session,
url=self.endpoint,
rule_payload=self.rule_payload)
self.n_requests += 1
ResultStream.session_request_counter += 1
resp = json.loads(resp.content.decode(resp.encoding))
self.next_token = resp.get("next", None)
self.current_tweets = resp["results"]
def __repr__(self):
repr_keys = ["username", "endpoint", "rule_payload",
"tweetify", "max_results"]
str_ = json.dumps(dict([(k, self.__dict__.get(k)) for k in repr_keys]),
indent=4)
str_ = "ResultStream: \n\t" + str_
return str_
def collect_results(rule, max_results=500, result_stream_args=None):
"""
Utility function to quickly get a list of tweets from a ``ResultStream``
without keeping the object around. Requires your args to be configured
prior to using.
Args:
rule (str): valid powertrack rule for your account, preferably
generated by the `gen_rule_payload` function.
max_results (int): maximum number of tweets or counts to return from
the API / underlying ``ResultStream`` object.
result_stream_args (dict): configuration dict that has connection
information for a ``ResultStream`` object.
Returns:
list of results
Example:
>>> from searchtweets import collect_results
>>> tweets = collect_results(rule,
max_results=500,
result_stream_args=search_args)
"""
if result_stream_args is None:
logger.error("This function requires a configuration dict for the "
"inner ResultStream object.")
raise KeyError
rs = ResultStream(rule_payload=rule,
max_results=max_results,
**result_stream_args)
return list(rs.stream())