From 380c79fe71ecbda1aa9c19571b46813ceb94f13a Mon Sep 17 00:00:00 2001 From: rwxd Date: Sun, 25 Dec 2022 11:55:58 +0100 Subject: [PATCH] fix(readwise): trying to fix rate-limit with sleep for Header 'Retry-After' --- wallabag2readwise/readwise.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/wallabag2readwise/readwise.py b/wallabag2readwise/readwise.py index baa1ca2..d41e846 100644 --- a/wallabag2readwise/readwise.py +++ b/wallabag2readwise/readwise.py @@ -5,6 +5,7 @@ from typing import Optional from ratelimit import limits, RateLimitException from backoff import on_exception, expo +from time import sleep from wallabag2readwise.models import Annotation, Entry, ReadwiseBook, ReadwiseHighlight from wallabag2readwise.output import console @@ -33,17 +34,19 @@ def _session(self) -> requests.Session: ) return session - @on_exception(expo, ReadwiseRateLimitException, max_tries=8) @on_exception(expo, RateLimitException, max_tries=8) - @limits(calls=230, period=60) + @limits(calls=240, period=60) def _request( self, method: str, endpoint: str, params: dict = {}, data: dict = {} ) -> requests.Response: url = self.url + endpoint logger.debug(f'Calling "{method}" on "{url}" with params: {params}') response = self._session.request(method, url, params=params, json=data) - if response.status_code == 429: - raise ReadwiseRateLimitException() + while response.status_code == 429: + seconds = int(response.headers['Retry-After']) + logger.warning(f'Rate limited, retrying in {seconds} seconds') + sleep(seconds) + response = self._session.request(method, url, params=params, json=data) response.raise_for_status() return response @@ -69,7 +72,7 @@ def get_books(self, category: str) -> Generator[ReadwiseBook, None, None]: page = 1 page_size = 1000 while True: - data = self.get( + data = self.get_with_limit_19( '/books', {'page': page, 'page_size': page_size, 'category': category}, ).json() @@ -88,7 +91,7 @@ def get_book_highlights( page = 1 page_size = 1000 while True: - data = self.get( + data = self.get_with_limit_19( '/highlights', {'page': page, 'page_size': page_size, 'book_id': book_id}, ).json()