Skip to content

Commit

Permalink
Improve rate limit handling
Browse files Browse the repository at this point in the history
Closes #3
  • Loading branch information
josh authored Jul 13, 2024
1 parent b060adf commit 6c2a312
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions imdb_trakt_sync.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import csv
import json
import logging
from collections.abc import Iterable, Iterator
from dataclasses import dataclass
Expand Down Expand Up @@ -301,6 +302,14 @@ def trakt_session(client_id: str, access_token: str) -> requests.Session:
return session


class TraktRatelimit(TypedDict):
name: str
period: int
limit: int
remaining: int
until: str


def trakt_request(
session: requests.Session,
method: Literal["GET", "POST", "PUT", "DELETE"],
Expand All @@ -316,6 +325,15 @@ def trakt_request(
last_request = now
response = session.request(method, url, **kwargs)
response.raise_for_status()

ratelimit: TraktRatelimit = json.loads(response.headers["x-ratelimit"])
if ratelimit["remaining"] < ratelimit["limit"] * 0.25:
logger.warning("Rate limit < 25%% remaining: %s", ratelimit)
sleep(10)
elif ratelimit["remaining"] < ratelimit["limit"] * 0.10:
logger.error("Rate limit < 10%% remaining: %s", ratelimit)
sleep(60)

return response


Expand Down

0 comments on commit 6c2a312

Please sign in to comment.