Skip to content

Commit 807ffce

Browse files
committed
Add a query limit runtime modifier. (4.4.0)
The query_limit kwarg may now be used to add a cap to the number of queries OGRe sends to public APIs.
1 parent a9cfc94 commit 807ffce

File tree

6 files changed

+51
-11
lines changed

6 files changed

+51
-11
lines changed

docs/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@
5353
# built documents.
5454
#
5555
# The short X.Y version.
56-
version = '4.3'
56+
version = '4.4'
5757
# The full version, including alpha/beta/rc tags.
58-
release = '4.3.0'
58+
release = '4.4.0'
5959

6060
# The language for content autogenerated by Sphinx. Refer to documentation
6161
# for a list of supported languages.

docs/tutorial.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ Alternatively, environment variables are checked when keys are unspecified.
124124
> --quantity 50 --location 37.781157 -122.398720 1 km
125125
126126
.. seealso:: For a description of all available command line arguments, run:
127-
127+
128128
.. code-block:: bash
129129
130130
$ python -m ogre --help

ogre/Twitter.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -270,29 +270,36 @@ def twitter(
270270
log.setLevel(logging.INFO)
271271
log.info(qid+" Request: Twitter")
272272

273-
if not kinds or remaining < 1:
273+
maximum_queries = kwargs.get("query_limit")
274+
if maximum_queries is None:
275+
maximum_queries = 450 # Twitter allows 450 queries every 15 minutes.
276+
277+
if not kinds or remaining < 1 or maximum_queries < 1:
274278
log.info(qid+" Success: No results were requested.")
275279
return []
276280

277281
api = kwargs.get("api", Twython)(
278282
keychain["consumer_key"],
279283
access_token=keychain["access_token"]
280284
)
285+
281286
limits = api.get_application_rate_limit_status()
282-
maximum_queries = 450 # Twitter allows 450 queries every 15 minutes.
283287
try:
284-
maximum_queries =\
285-
int(limits["resources"]["search"]["/search/tweets"]["remaining"])
286-
if maximum_queries < 1:
288+
limit = int(
289+
limits["resources"]["search"]["/search/tweets"]["remaining"]
290+
)
291+
if limit < 1:
287292
log.info(qid+" Failure: Queries are being limited.")
288293
else:
289-
log.debug(qid+" Status: "+str(maximum_queries)+" queries remain.")
294+
log.debug(qid+" Status: "+str(limit)+" queries remain.")
295+
if limit < maximum_queries:
296+
maximum_queries = limit
290297
except KeyError:
291298
log.warn(qid+" Unobtainable Rate Limit")
292299
total = remaining
293300

294301
collection = []
295-
for query in range(0, maximum_queries):
302+
for query in range(maximum_queries):
296303
count = min(remaining, 100) # Twitter accepts a max count of 100.
297304
try:
298305
results = api.search(

ogre/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@
1212

1313
from ogre.api import OGRe
1414

15-
__version__ = "4.3.0"
15+
__version__ = "4.4.0"

ogre/__main__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ def main():
7878
action="store_true",
7979
default=False
8080
)
81+
parser.add_argument(
82+
"--limit",
83+
help="Specify a query limit.",
84+
default=None
85+
)
8186
parser.add_argument(
8287
"--strict",
8388
help="Ensure resulting media is specifically requested.",
@@ -104,6 +109,8 @@ def main():
104109
if args.interval is not None:
105110
args.interval[0] = float(args.interval[0])
106111
args.interval[1] = float(args.interval[1])
112+
if args.limit is not None:
113+
args.limit = int(args.limit)
107114

108115
print json.dumps(
109116
OGRe(args.keys).fetch(
@@ -113,6 +120,7 @@ def main():
113120
quantity=args.quantity,
114121
location=args.location,
115122
interval=args.interval,
123+
query_limit=args.limit,
116124
secure=args.insecure,
117125
strict_media=args.strict
118126
),

ogre/test/test_Twitter.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,31 @@ def test_twitter(self):
308308
keyword="test",
309309
quantity=0,
310310
test=True,
311+
test_message="Query Limit Fail-Fast",
312+
api=self.api["regular"],
313+
network=self.network["regular"]
314+
),
315+
[]
316+
)
317+
self.assertEqual(self.api["regular"].call_count, 0)
318+
self.assertEqual(
319+
self.api["regular"]().get_application_rate_limit_status.call_count,
320+
0
321+
)
322+
self.assertEqual(self.api["regular"]().search.call_count, 0)
323+
self.assertEqual(self.network["regular"].call_count, 0)
324+
325+
# Allowing < 1 query fails fast.
326+
self.api["regular"].reset_mock()
327+
self.network["regular"].reset_mock()
328+
self.assertEqual(
329+
twitter(
330+
keys=self.retriever.keychain[
331+
self.retriever.keyring["twitter"]
332+
],
333+
keyword="test",
334+
query_limit=0,
335+
test=True,
311336
test_message="Quantity Fail-Fast",
312337
api=self.api["regular"],
313338
network=self.network["regular"]

0 commit comments

Comments
 (0)