Skip to content

Commit

Permalink
fix(readwise): trying to fix rate-limit with sleep for Header 'Retry-…
Browse files Browse the repository at this point in the history
…After'
  • Loading branch information
rwxd committed Dec 25, 2022
1 parent 44f8bc4 commit 380c79f
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions wallabag2readwise/readwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand All @@ -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()
Expand Down

0 comments on commit 380c79f

Please sign in to comment.