Skip to content

Commit

Permalink
Requirement and documentation fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
halcy committed Nov 25, 2016
1 parent 3ce225d commit 69f7877
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
7 changes: 4 additions & 3 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ node running Mastodon.

A note about rate limits
------------------------
Mastodons API rate limits per IP. Mastodon.py has three modes for dealing
with rate limiting that you can pass to the constructor, "throw", "wait"
and "pace", "wait" being the default.
Mastodons API rate limits per IP. By default, the limit is 150 requests per 5 minute
time slow. This can differ from instance to instance and is subject to change.
Mastodon.py has three modes for dealing with rate limiting that you can pass to
the constructor, "throw", "wait" and "pace", "wait" being the default.

In "throw" mode, Mastodon.py makes no attempt to stick to rate limits. When
a request hits the rate limit, it simply throws a MastodonRateLimitError. This is
Expand Down
25 changes: 14 additions & 11 deletions mastodon/Mastodon.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ def __init__(self, client_id, client_secret = None, access_token = None, api_bas
self.ratelimit_lastcall = time.time()
self.ratelimit_pacefactor = ratelimit_pacefactor

if not ratelimit_method in ["throw", "wait", "pace"]:
raise MastodonIllegalArgumentError("Invalid ratelimit method.")

if os.path.isfile(self.client_id):
with open(self.client_id, 'r') as secret_file:
self.client_id = secret_file.readline().rstrip()
Expand Down Expand Up @@ -521,11 +524,11 @@ def __api_request(self, method, endpoint, params = {}, files = {}, do_ratelimiti
raise MastodonAPIError("Could not parse response as JSON, respose code was " + str(response_object.status_code))

# Handle rate limiting
try:
if 'X-RateLimit-Remaining' in response_object.headers and do_ratelimiting:
self.ratelimit_remaining = int(response_object.headers['X-RateLimit-Remaining'])
self.ratelimit_limit = int(response_object.headers['X-RateLimit-Limit'])
if 'X-RateLimit-Remaining' in response_object.headers and do_ratelimiting:
self.ratelimit_remaining = int(response_object.headers['X-RateLimit-Remaining'])
self.ratelimit_limit = int(response_object.headers['X-RateLimit-Limit'])

try:
ratelimit_reset_datetime = dateutil.parser.parse(response_object.headers['X-RateLimit-Reset'])
self.ratelimit_reset = self.__datetime_to_epoch(ratelimit_reset_datetime)

Expand All @@ -535,20 +538,20 @@ def __api_request(self, method, endpoint, params = {}, files = {}, do_ratelimiti
server_time_diff = time.time() - server_time
self.ratelimit_reset += server_time_diff
self.ratelimit_lastcall = time.time()

if "error" in response and response["error"] == "Throttled":
if self.ratelimit_method == "throw":
raise MastodonRatelimitError("Hit rate limit.")
except:
raise MastodonRatelimitError("Rate limit time calculations failed.")

if "error" in response and response["error"] == "Throttled":
if self.ratelimit_method == "throw":
raise MastodonRatelimitError("Hit rate limit.")

if self.ratelimit_method == "wait" or self.ratelimit_method == "pace":
to_next = self.ratelimit_reset - time.time()
if to_next > 0:
# As a precaution, never sleep longer than 5 minutes
to_next = min(to_next, 5 * 60)
time.sleep(to_next)
request_complete = False
except:
raise MastodonRatelimitError("Rate limit time calculations failed.")
request_complete = False

return response

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
version='1.0.1',
description='Python wrapper for the Mastodon API',
packages=['mastodon'],
install_requires=['requests'],
install_requires=['requests', 'dateutil'],
url='https://github.com/halcy/Mastodon.py',
author='Lorenz Diener',
author_email='lorenzd+mastodonpypypi@gmail.com',
Expand Down

0 comments on commit 69f7877

Please sign in to comment.