aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenz Diener <[email protected]>2016-11-25 23:28:30 +0100
committerLorenz Diener <[email protected]>2016-11-25 23:28:30 +0100
commit69f78773a05666430c93eac5ef2eead7f648cc09 (patch)
tree3be8b01140f8bfa52dd70972ac1c243fb97a2959
parent3ce225dbeff8dc81539108cad376c0ab7db2125f (diff)
downloadmastodon.py-69f78773a05666430c93eac5ef2eead7f648cc09.tar.gz
Requirement and documentation fixes
-rw-r--r--docs/index.rst7
-rw-r--r--mastodon/Mastodon.py25
-rw-r--r--setup.py2
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
42A note about rate limits 42A note about rate limits
43------------------------ 43------------------------
44Mastodons API rate limits per IP. Mastodon.py has three modes for dealing 44Mastodons API rate limits per IP. By default, the limit is 150 requests per 5 minute
45with rate limiting that you can pass to the constructor, "throw", "wait" 45time slow. This can differ from instance to instance and is subject to change.
46and "pace", "wait" being the default. 46Mastodon.py has three modes for dealing with rate limiting that you can pass to
47the constructor, "throw", "wait" and "pace", "wait" being the default.
47 48
48In "throw" mode, Mastodon.py makes no attempt to stick to rate limits. When 49In "throw" mode, Mastodon.py makes no attempt to stick to rate limits. When
49a request hits the rate limit, it simply throws a MastodonRateLimitError. This is 50a 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
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',
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]',
Powered by cgit v1.2.3 (git 2.41.0)