Skip to content

Commit d52a24f

Browse files
authored
Merge pull request #34 from twitterdev/arg_handling_updates
Arg handling updates
2 parents e138bdb + 9ca6499 commit d52a24f

File tree

4 files changed

+37
-22
lines changed

4 files changed

+37
-22
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

searchtweets/result_stream.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class ResultStream:
155155
session_request_counter = 0
156156

157157
def __init__(self, endpoint, rule_payload, username=None, password=None,
158-
bearer_token=None, max_results=1000,
158+
bearer_token=None, max_results=500,
159159
tweetify=True, max_requests=None, **kwargs):
160160

161161
self.username = username

searchtweets/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
def take(n, iterable):
3131
"""Return first n items of the iterable as a list.
32-
Originaly found in the Python itertools documentation.
32+
Originally found in the Python itertools documentation.
3333
3434
Args:
3535
n (int): number of items to return

tools/search_tweets.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,13 @@ def parse_cmd_args():
8989

9090
argparser.add_argument("--results-per-call",
9191
dest="results_per_call",
92-
default=100,
9392
help="Number of results to return per call "
9493
"(default 100; max 500) - corresponds to "
9594
"'maxResults' in the API")
9695

9796
argparser.add_argument("--max-results", dest="max_results",
98-
default=500,
9997
type=int,
100-
help="Maximum number of Tweets or Counts to return for this "
101-
"session (defaults to 500)")
98+
help="Maximum number of Tweets or Counts to return for this session")
10299

103100
argparser.add_argument("--max-pages",
104101
dest="max_pages",
@@ -108,7 +105,7 @@ def parse_cmd_args():
108105
"use for this session.")
109106

110107
argparser.add_argument("--results-per-file", dest="results_per_file",
111-
default=0,
108+
default=None,
112109
type=int,
113110
help="Maximum tweets to save per file.")
114111

@@ -137,6 +134,10 @@ def parse_cmd_args():
137134
return argparser
138135

139136

137+
def _filter_sensitive_args(dict_):
138+
sens_args = ("password", "consumer_key", "consumer_secret", "bearer_token")
139+
return {k: v for k, v in dict_.items() if k not in sens_args}
140+
140141
def main():
141142
args_dict = vars(parse_cmd_args().parse_args())
142143
if args_dict.get("debug") is True:
@@ -149,8 +150,8 @@ def main():
149150
else:
150151
configfile_dict = {}
151152

152-
logger.debug("config file dict:")
153-
logger.debug(json.dumps(configfile_dict, indent=4))
153+
logger.debug("config file ({}) arguments sans sensitive args:".format(args_dict["config_filename"]))
154+
logger.debug(json.dumps(_filter_sensitive_args(configfile_dict), indent=4))
154155

155156
creds_dict = load_credentials(filename=args_dict["credential_file"],
156157
account_type=args_dict["account_type"],
@@ -163,24 +164,26 @@ def main():
163164
dict_filter(args_dict),
164165
dict_filter(creds_dict))
165166

166-
logger.debug("combined dict (cli, config, creds):")
167-
logger.debug(json.dumps(config_dict, indent=4))
167+
logger.debug("combined dict (cli, config, creds) sans password:")
168+
logger.debug(json.dumps(_filter_sensitive_args(config_dict), indent=4))
168169

169170
if len(dict_filter(config_dict).keys() & REQUIRED_KEYS) < len(REQUIRED_KEYS):
170171
print(REQUIRED_KEYS - dict_filter(config_dict).keys())
171172
logger.error("ERROR: not enough arguments for the program to work")
172173
sys.exit(1)
173174

174175
stream_params = gen_params_from_config(config_dict)
176+
logger.debug("full arguments passed to the ResultStream object sans password")
177+
logger.debug(json.dumps(_filter_sensitive_args(stream_params), indent=4))
175178

176179
rs = ResultStream(tweetify=False, **stream_params)
177180

178181
logger.debug(str(rs))
179182

180183
if config_dict.get("filename_prefix") is not None:
181184
stream = write_result_stream(rs,
182-
filename_prefix=config_dict["filename_prefix"],
183-
results_per_file=config_dict["results_per_file"])
185+
filename_prefix=config_dict.get("filename_prefix"),
186+
results_per_file=config_dict.get("results_per_file"))
184187
else:
185188
stream = rs.stream()
186189

0 commit comments

Comments
 (0)