From 69f78773a05666430c93eac5ef2eead7f648cc09 Mon Sep 17 00:00:00 2001 From: Lorenz Diener Date: Fri, 25 Nov 2016 23:28:30 +0100 Subject: [PATCH] Requirement and documentation fixes --- docs/index.rst | 7 ++++--- mastodon/Mastodon.py | 25 ++++++++++++++----------- setup.py | 2 +- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 02676a4..5e893fe 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -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 diff --git a/mastodon/Mastodon.py b/mastodon/Mastodon.py index bb16d95..c437618 100644 --- a/mastodon/Mastodon.py +++ b/mastodon/Mastodon.py @@ -97,6 +97,9 @@ class Mastodon: 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() @@ -521,11 +524,11 @@ class Mastodon: 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) @@ -535,10 +538,12 @@ class Mastodon: 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() @@ -546,9 +551,7 @@ class Mastodon: # 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 diff --git a/setup.py b/setup.py index 74292fd..8bc4383 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup(name='Mastodon.py', 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',