Skip to content

Commit

Permalink
chore: generic 403 handling & dependencies update
Browse files Browse the repository at this point in the history
  • Loading branch information
corenting committed Aug 22, 2024
1 parent a800e62 commit f896892
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 289 deletions.
8 changes: 4 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ USER eddrit
# for some architectures directly so that they can be cached.
# To keep in sync with poetry.lock to speedup CI
RUN /usr/local/bin/pip install --user \
uvloop==0.19.0 \
lxml==5.2.2 \
uvloop==0.20.0 \
lxml==5.3.0 \
httptools==0.6.1 \
MarkupSafe==2.1.5 \
pyyaml==6.0.1 \
hiredis==2.3.2
pyyaml==6.0.2 \
libvalkey==4.0.0

# Dependencies
WORKDIR /app/
Expand Down
13 changes: 3 additions & 10 deletions eddrit/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,9 @@ class SubredditNotFoundError(SubredditUnavailableError):
message = "Subreddit not found"


class SubredditIsPrivateError(SubredditUnavailableError):
message = "Subreddit is private"


class SubredditIsQuarantinedError(SubredditUnavailableError):
message = "Subreddit is quarantined"


class SubredditIsBannedError(SubredditUnavailableError):
message = "Subreddit is banned"
class SubredditCannotBeViewedError(SubredditUnavailableError):
def __init__(self, reason: str) -> None:
self.message = f"Subreddit is {reason}"


class RateLimitedError(Exception):
Expand Down
16 changes: 5 additions & 11 deletions eddrit/reddit/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
from eddrit import models
from eddrit.constants import REDDIT_BASE_API_URL
from eddrit.exceptions import (
SubredditIsBannedError,
SubredditIsPrivateError,
SubredditIsQuarantinedError,
SubredditCannotBeViewedError,
SubredditNotFoundError,
UserNotFoundError,
)
Expand Down Expand Up @@ -232,15 +230,11 @@ def _raise_if_subreddit_is_not_available(api_res: httpx.Response) -> None:

# Check for banned subreddits
if api_res.status_code == 404 and json.get("reason") == "banned":
raise SubredditIsBannedError()
raise SubredditCannotBeViewedError("banned")

# Check for quarantined subreddits
if api_res.status_code == 403 and json.get("reason") == "quarantined":
raise SubredditIsQuarantinedError()

# Check for private subreddits
if api_res.status_code == 403 and json.get("reason") == "private":
raise SubredditIsPrivateError()
# Check for subreddits that cannot be viewed (quarantine, privated, gated)
if api_res.status_code == 403 and (reason := json.get("reason")):
raise SubredditCannotBeViewedError(reason)

# Check for subreddit not found
if len(api_res.history) > 0 and api_res.history[0].status_code != 200:
Expand Down
4 changes: 2 additions & 2 deletions eddrit/utils/oauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _get_login_headers_from_cache() -> dict[str, Any]:
Get the login headers from cache
"""
valkey_client = valkey.Valkey(connection_pool=_valkey_connection_pool)
with valkey_client.lock(_valkey_lock_key, blocking_timeout=5):
with valkey_client.lock(_valkey_lock_key, timeout=20, blocking_timeout=5):
# Check if we have login headers in cache
if not valkey_client.exists(_valkey_headers_key):
oauth_login()
Expand All @@ -133,7 +133,7 @@ def oauth_login() -> None:
logger.debug("Performing OAuth login")
valkey_client = valkey.Valkey(connection_pool=_valkey_connection_pool)

with valkey_client.lock(_valkey_lock_key, blocking_timeout=5):
with valkey_client.lock(_valkey_lock_key, timeout=20, blocking_timeout=5):
# Generate identity: unique ID + random user-agent
unique_uuid = str(uuid4())
android_app_version = random.choice(OFFICIAL_ANDROID_APP_VERSIONS) # noqa: S311
Expand Down
Loading

0 comments on commit f896892

Please sign in to comment.