Skip to content

Commit a242a44

Browse files
author
Aaron Gonzales
committed
updated endpoint handling to be done in the ResultStream object and simplify credentials
1 parent 74b6e58 commit a242a44

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

twittersearch/api_utils.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import json
1616

1717
__all__ = ["gen_rule_payload", "gen_params_from_config", "load_credentials",
18+
"infer_endpoint",
1819
"validate_count_api", "GNIP_RESP_CODES", "change_to_count_endpoint"]
1920

2021
logger = logging.getLogger(__name__)
@@ -192,6 +193,15 @@ def gen_params_from_config(config_dict):
192193
return _dict
193194

194195

196+
def infer_endpoint(rule_payload):
197+
"""
198+
Infer which endpoint should be used for a given rule payload.
199+
"""
200+
bucket = (rule_payload if isinstance(rule_payload, dict)
201+
else json.loads(rule_payload)).get("bucket")
202+
return "counts" if bucket else "search"
203+
204+
195205
def validate_count_api(rule_payload, endpoint):
196206
"""
197207
Ensures that the counts api is set correctly in a payload.
@@ -230,11 +240,13 @@ def load_credentials(filename=None, account_type=None):
230240
default '~/.twitter_keys.yaml'
231241
account_type (str): pass your account type, "premium" or "enterprise"
232242
233-
returns: tuple of (dict, dict), both search_api args and count_api args.
243+
Returns: dict of your access credentials.
234244
235245
Example:
236246
>>> from twittersearch.api_utils import load_credentials
237-
>>> search_args, count_args = load_credentials(account_type="enterprise")
247+
>>> search_args = load_credentials(account_type="premium")
248+
>>> search_args.keys()
249+
dict_keys(['bearer_token', 'endpoint'])
238250
239251
"""
240252
if account_type is None or account_type not in {"premium", "enterprise"}:
@@ -251,6 +263,4 @@ def load_credentials(filename=None, account_type=None):
251263
search_args = {"username": search_creds["username"],
252264
"password": search_creds["password"],
253265
"endpoint": search_creds["endpoint"]}
254-
count_args = {**search_args,
255-
**{"endpoint": change_to_count_endpoint(search_args["endpoint"])}}
256-
return search_args, count_args
266+
return search_args

twittersearch/result_stream.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@ def __init__(self, endpoint, rule_payload, username=None, password=None,
155155
self.username = username
156156
self.password = password
157157
self.bearer_token = bearer_token
158-
self.endpoint = endpoint
159158
if isinstance(rule_payload, str):
160159
rule_payload = json.loads(rule_payload)
161160
self.rule_payload = rule_payload
@@ -174,7 +173,10 @@ def __init__(self, endpoint, rule_payload, username=None, password=None,
174173
# magic number of requests!
175174
self.max_requests = (max_requests if max_requests is not None
176175
else 10 ** 9)
177-
validate_count_api(self.rule_payload, self.endpoint)
176+
self.endpoint = (change_to_count_endpoint(endpoint)
177+
if infer_endpoint(rule_payload) == "counts"
178+
else endpoint)
179+
# validate_count_api(self.rule_payload, self.endpoint)
178180

179181
def stream(self):
180182
"""
@@ -287,7 +289,7 @@ def collect_results(rule, max_results=500, result_stream_args=None):
287289
"inner ResultStream object.")
288290
raise KeyError
289291

290-
rs = ResultStream(**result_stream_args,
291-
rule_payload=rule,
292-
max_results=max_results)
292+
rs = ResultStream(rule_payload=rule,
293+
max_results=max_results,
294+
**result_stream_args)
293295
return list(rs.stream())

0 commit comments

Comments
 (0)