Requirement and documentation fixes

This commit is contained in:
Lorenz Diener 2016-11-25 23:28:30 +01:00
pare 3ce225dbef
commit 69f78773a0
S'han modificat 3 arxius amb 19 adicions i 15 eliminacions

Veure arxiu

@ -41,9 +41,10 @@ node running Mastodon.
A note about rate limits A note about rate limits
------------------------ ------------------------
Mastodons API rate limits per IP. Mastodon.py has three modes for dealing Mastodons API rate limits per IP. By default, the limit is 150 requests per 5 minute
with rate limiting that you can pass to the constructor, "throw", "wait" time slow. This can differ from instance to instance and is subject to change.
and "pace", "wait" being the default. 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 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 a request hits the rate limit, it simply throws a MastodonRateLimitError. This is

Veure arxiu

@ -97,6 +97,9 @@ class Mastodon:
self.ratelimit_lastcall = time.time() self.ratelimit_lastcall = time.time()
self.ratelimit_pacefactor = ratelimit_pacefactor 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): if os.path.isfile(self.client_id):
with open(self.client_id, 'r') as secret_file: with open(self.client_id, 'r') as secret_file:
self.client_id = secret_file.readline().rstrip() 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)) raise MastodonAPIError("Could not parse response as JSON, respose code was " + str(response_object.status_code))
# Handle rate limiting # Handle rate limiting
try: if 'X-RateLimit-Remaining' in response_object.headers and do_ratelimiting:
if 'X-RateLimit-Remaining' in response_object.headers and do_ratelimiting: self.ratelimit_remaining = int(response_object.headers['X-RateLimit-Remaining'])
self.ratelimit_remaining = int(response_object.headers['X-RateLimit-Remaining']) self.ratelimit_limit = int(response_object.headers['X-RateLimit-Limit'])
self.ratelimit_limit = int(response_object.headers['X-RateLimit-Limit'])
try:
ratelimit_reset_datetime = dateutil.parser.parse(response_object.headers['X-RateLimit-Reset']) ratelimit_reset_datetime = dateutil.parser.parse(response_object.headers['X-RateLimit-Reset'])
self.ratelimit_reset = self.__datetime_to_epoch(ratelimit_reset_datetime) self.ratelimit_reset = self.__datetime_to_epoch(ratelimit_reset_datetime)
@ -535,10 +538,12 @@ class Mastodon:
server_time_diff = time.time() - server_time server_time_diff = time.time() - server_time
self.ratelimit_reset += server_time_diff self.ratelimit_reset += server_time_diff
self.ratelimit_lastcall = time.time() self.ratelimit_lastcall = time.time()
except:
raise MastodonRatelimitError("Rate limit time calculations failed.")
if "error" in response and response["error"] == "Throttled": if "error" in response and response["error"] == "Throttled":
if self.ratelimit_method == "throw": if self.ratelimit_method == "throw":
raise MastodonRatelimitError("Hit rate limit.") raise MastodonRatelimitError("Hit rate limit.")
if self.ratelimit_method == "wait" or self.ratelimit_method == "pace": if self.ratelimit_method == "wait" or self.ratelimit_method == "pace":
to_next = self.ratelimit_reset - time.time() to_next = self.ratelimit_reset - time.time()
@ -546,9 +551,7 @@ class Mastodon:
# As a precaution, never sleep longer than 5 minutes # As a precaution, never sleep longer than 5 minutes
to_next = min(to_next, 5 * 60) to_next = min(to_next, 5 * 60)
time.sleep(to_next) time.sleep(to_next)
request_complete = False request_complete = False
except:
raise MastodonRatelimitError("Rate limit time calculations failed.")
return response return response

Veure arxiu

@ -4,7 +4,7 @@ setup(name='Mastodon.py',
version='1.0.1', version='1.0.1',
description='Python wrapper for the Mastodon API', description='Python wrapper for the Mastodon API',
packages=['mastodon'], packages=['mastodon'],
install_requires=['requests'], install_requires=['requests', 'dateutil'],
url='https://github.com/halcy/Mastodon.py', url='https://github.com/halcy/Mastodon.py',
author='Lorenz Diener', author='Lorenz Diener',
author_email='lorenzd+mastodonpypypi@gmail.com', author_email='lorenzd+mastodonpypypi@gmail.com',