Skip to content

Commit 9ca6499

Browse files
author
Aaron Gonzales
committed
updated configuration building logic to have fewer default args and respect numeric values passed from various sources
1 parent d3d694c commit 9ca6499

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

searchtweets/api_utils.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def change_to_count_endpoint(endpoint):
8383
return "https://" + '/'.join(filt_tokens) + '/' + "counts.json"
8484

8585

86-
def gen_rule_payload(pt_rule, results_per_call=500,
86+
def gen_rule_payload(pt_rule, results_per_call=None,
8787
from_date=None, to_date=None, count_bucket=None,
8888
tag=None,
8989
stringify=True):
@@ -93,7 +93,7 @@ def gen_rule_payload(pt_rule, results_per_call=500,
9393
9494
Args:
9595
pt_rule (str): The string version of a powertrack rule,
96-
e.g., "kanye west has:geo". Accepts multi-line strings
96+
e.g., "beyonce has:geo". Accepts multi-line strings
9797
for ease of entry.
9898
results_per_call (int): number of tweets or counts returned per API
9999
call. This maps to the ``maxResults`` search API parameter.
@@ -110,14 +110,16 @@ def gen_rule_payload(pt_rule, results_per_call=500,
110110
Example:
111111
112112
>>> from searchtweets.utils import gen_rule_payload
113-
>>> gen_rule_payload("kanye west has:geo",
113+
>>> gen_rule_payload("beyonce has:geo",
114114
... from_date="2017-08-21",
115115
... to_date="2017-08-22")
116-
'{"query":"kanye west has:geo","maxResults":100,"toDate":"201708220000","fromDate":"201708210000"}'
116+
'{"query":"beyonce has:geo","maxResults":100,"toDate":"201708220000","fromDate":"201708210000"}'
117117
"""
118118

119119
pt_rule = ' '.join(pt_rule.split()) # allows multi-line strings
120-
payload = {"query": pt_rule, "maxResults": results_per_call}
120+
payload = {"query": pt_rule}
121+
if results_per_call is not None and isinstance(results_per_call, int) is True:
122+
payload["maxResults"] = results_per_call
121123
if to_date:
122124
payload["toDate"] = convert_utc_time(to_date)
123125
if from_date:
@@ -149,20 +151,30 @@ def gen_params_from_config(config_dict):
149151
else:
150152
endpoint = config_dict.get("endpoint")
151153

154+
155+
def intify(arg):
156+
if not isinstance(arg, int) and arg is not None:
157+
return int(arg)
158+
else:
159+
return arg
160+
161+
# this parameter comes in as a string when it's parsed
162+
results_per_call = intify(config_dict.get("results_per_call", None))
163+
152164
rule = gen_rule_payload(pt_rule=config_dict["pt_rule"],
153165
from_date=config_dict.get("from_date", None),
154166
to_date=config_dict.get("to_date", None),
155-
results_per_call=int(config_dict.get("results_per_call")),
167+
results_per_call=results_per_call,
156168
count_bucket=config_dict.get("count_bucket", None))
157169

158170
_dict = {"endpoint": endpoint,
159171
"username": config_dict.get("username"),
160172
"password": config_dict.get("password"),
161173
"bearer_token": config_dict.get("bearer_token"),
162174
"rule_payload": rule,
163-
"results_per_file": int(config_dict.get("results_per_file")),
164-
"max_results": int(config_dict.get("max_results")),
165-
"max_pages": config_dict.get("max_pages", None)}
175+
"results_per_file": intify(config_dict.get("results_per_file")),
176+
"max_results": intify(config_dict.get("max_results")),
177+
"max_pages": intify(config_dict.get("max_pages", None))}
166178
return _dict
167179

168180

0 commit comments

Comments
 (0)