diff options
Diffstat (limited to 'mastodon')
-rw-r--r-- | mastodon/Mastodon.py | 25 |
1 files changed, 14 insertions, 11 deletions
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 | ||