Add rate limit delay support
This limits number of requests allowed to be performed in sequence
MODIFY
plex_trakt_sync/decorators/rate_limit.py
plex_trakt_sync/decorators/rate_limit.py
from functools import wraps
-from time import sleep
+from time import sleep, time
from trakt.errors import RateLimitException
-
from plex_trakt_sync.logging import logging
+last_time = time()
+
+
+# https://trakt.docs.apiary.io/#introduction/rate-limiting
+def rate_limit(retries=5, delay=None):
+ """
+
+ :param retries: number of retries
+ :param delay: delay in sec between trakt requests to respect rate limit
+ :return:
+ """
+
+ def respect_trakt_rate():
+ if delay is None:
+ return
+ diff_time = time() - last_time
+ if diff_time < delay:
+ wait = delay - diff_time
+ logging.warning(
+ f'Sleeping for {wait:.3f} seconds'
+ )
+ sleep(wait)
-def rate_limit(retries=5):
def decorator(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
retry = 0
while True:
try:
+ respect_trakt_rate()
return fn(*args, **kwargs)
except RateLimitException as e:
if retry == retries:

Add rate limit delay support

This limits number of requests allowed to be performed in sequence