diff options
author | Lorenz Diener <[email protected]> | 2016-11-25 23:28:30 +0100 |
---|---|---|
committer | Lorenz Diener <[email protected]> | 2016-11-25 23:28:30 +0100 |
commit | 69f78773a05666430c93eac5ef2eead7f648cc09 (patch) | |
tree | 3be8b01140f8bfa52dd70972ac1c243fb97a2959 | |
parent | 3ce225dbeff8dc81539108cad376c0ab7db2125f (diff) | |
download | mastodon.py-69f78773a05666430c93eac5ef2eead7f648cc09.tar.gz |
Requirement and documentation fixes
-rw-r--r-- | docs/index.rst | 7 | ||||
-rw-r--r-- | mastodon/Mastodon.py | 25 | ||||
-rw-r--r-- | 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. | |||
41 | 41 | ||
42 | A note about rate limits | 42 | A note about rate limits |
43 | ------------------------ | 43 | ------------------------ |
44 | Mastodons API rate limits per IP. Mastodon.py has three modes for dealing | 44 | Mastodons API rate limits per IP. By default, the limit is 150 requests per 5 minute |
45 | with rate limiting that you can pass to the constructor, "throw", "wait" | 45 | time slow. This can differ from instance to instance and is subject to change. |
46 | and "pace", "wait" being the default. | 46 | Mastodon.py has three modes for dealing with rate limiting that you can pass to |
47 | the constructor, "throw", "wait" and "pace", "wait" being the default. | ||
47 | 48 | ||
48 | In "throw" mode, Mastodon.py makes no attempt to stick to rate limits. When | 49 | In "throw" mode, Mastodon.py makes no attempt to stick to rate limits. When |
49 | a request hits the rate limit, it simply throws a MastodonRateLimitError. This is | 50 | 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: | |||
97 | self.ratelimit_lastcall = time.time() | 97 | self.ratelimit_lastcall = time.time() |
98 | self.ratelimit_pacefactor = ratelimit_pacefactor | 98 | self.ratelimit_pacefactor = ratelimit_pacefactor |
99 | 99 | ||
100 | if not ratelimit_method in ["throw", "wait", "pace"]: | ||
101 | raise MastodonIllegalArgumentError("Invalid ratelimit method.") | ||
102 | |||
100 | if os.path.isfile(self.client_id): | 103 | if os.path.isfile(self.client_id): |
101 | with open(self.client_id, 'r') as secret_file: | 104 | with open(self.client_id, 'r') as secret_file: |
102 | self.client_id = secret_file.readline().rstrip() | 105 | self.client_id = secret_file.readline().rstrip() |
@@ -521,11 +524,11 @@ class Mastodon: | |||
521 | raise MastodonAPIError("Could not parse response as JSON, respose code was " + str(response_object.status_code)) | 524 | raise MastodonAPIError("Could not parse response as JSON, respose code was " + str(response_object.status_code)) |
522 | 525 | ||
523 | # Handle rate limiting | 526 | # Handle rate limiting |
524 | try: | 527 | if 'X-RateLimit-Remaining' in response_object.headers and do_ratelimiting: |
525 | if 'X-RateLimit-Remaining' in response_object.headers and do_ratelimiting: | 528 | self.ratelimit_remaining = int(response_object.headers['X-RateLimit-Remaining']) |
526 | self.ratelimit_remaining = int(response_object.headers['X-RateLimit-Remaining']) | 529 | self.ratelimit_limit = int(response_object.headers['X-RateLimit-Limit']) |
527 | self.ratelimit_limit = int(response_object.headers['X-RateLimit-Limit']) | ||
528 | 530 | ||
531 | try: | ||
529 | ratelimit_reset_datetime = dateutil.parser.parse(response_object.headers['X-RateLimit-Reset']) | 532 | ratelimit_reset_datetime = dateutil.parser.parse(response_object.headers['X-RateLimit-Reset']) |
530 | self.ratelimit_reset = self.__datetime_to_epoch(ratelimit_reset_datetime) | 533 | self.ratelimit_reset = self.__datetime_to_epoch(ratelimit_reset_datetime) |
531 | 534 | ||
@@ -535,10 +538,12 @@ class Mastodon: | |||
535 | server_time_diff = time.time() - server_time | 538 | server_time_diff = time.time() - server_time |
536 | self.ratelimit_reset += server_time_diff | 539 | self.ratelimit_reset += server_time_diff |
537 | self.ratelimit_lastcall = time.time() | 540 | self.ratelimit_lastcall = time.time() |
538 | 541 | except: | |
539 | if "error" in response and response["error"] == "Throttled": | 542 | raise MastodonRatelimitError("Rate limit time calculations failed.") |
540 | if self.ratelimit_method == "throw": | 543 | |
541 | raise MastodonRatelimitError("Hit rate limit.") | 544 | if "error" in response and response["error"] == "Throttled": |
545 | if self.ratelimit_method == "throw": | ||
546 | raise MastodonRatelimitError("Hit rate limit.") | ||
542 | 547 | ||
543 | if self.ratelimit_method == "wait" or self.ratelimit_method == "pace": | 548 | if self.ratelimit_method == "wait" or self.ratelimit_method == "pace": |
544 | to_next = self.ratelimit_reset - time.time() | 549 | to_next = self.ratelimit_reset - time.time() |
@@ -546,9 +551,7 @@ class Mastodon: | |||
546 | # As a precaution, never sleep longer than 5 minutes | 551 | # As a precaution, never sleep longer than 5 minutes |
547 | to_next = min(to_next, 5 * 60) | 552 | to_next = min(to_next, 5 * 60) |
548 | time.sleep(to_next) | 553 | time.sleep(to_next) |
549 | request_complete = False | 554 | request_complete = False |
550 | except: | ||
551 | raise MastodonRatelimitError("Rate limit time calculations failed.") | ||
552 | 555 | ||
553 | return response | 556 | return response |
554 | 557 | ||
@@ -4,7 +4,7 @@ setup(name='Mastodon.py', | |||
4 | version='1.0.1', | 4 | version='1.0.1', |
5 | description='Python wrapper for the Mastodon API', | 5 | description='Python wrapper for the Mastodon API', |
6 | packages=['mastodon'], | 6 | packages=['mastodon'], |
7 | install_requires=['requests'], | 7 | install_requires=['requests', 'dateutil'], |
8 | url='https://github.com/halcy/Mastodon.py', | 8 | url='https://github.com/halcy/Mastodon.py', |
9 | author='Lorenz Diener', | 9 | author='Lorenz Diener', |
10 | author_email='[email protected]', | 10 | author_email='[email protected]', |