Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
corenting committed Jul 4, 2024
1 parent 29a75f2 commit 397bc6c
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ RUN /usr/local/bin/pip install --user \
lxml==5.2.2 \
httptools==0.6.1 \
MarkupSafe==2.1.5 \
pyyaml==6.0.1
pyyaml==6.0.1 \
hiredis==2.3.2

# Dependencies
WORKDIR /app/
Expand Down
3 changes: 1 addition & 2 deletions eddrit/config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
from environs import Env


env = Env()
env.read_env()

DEBUG: bool = env.bool("DEBUG", default=False)
LOG_LEVEL: str = env.str("LOG_LEVEL", default="WARNING")

VALKEY_URL: str = env.str("VALKEY_URL")
VALKEY_URL: str = env.str("VALKEY_URL")
3 changes: 1 addition & 2 deletions eddrit/constants.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
from enum import Enum

REDDIT_BASE_API_URL = "https://oauth.reddit.com"
REDDIT_BASE_API_URL = "https://oauth.reddit.com"
25 changes: 14 additions & 11 deletions eddrit/utils/oauth.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
from datetime import timedelta
import json
import random
from base64 import standard_b64encode
from typing import Any
from uuid import uuid4

import httpx
from loguru import logger
import valkey
from loguru import logger

from eddrit.config import VALKEY_URL
from eddrit.exceptions import RateLimitedError
Expand Down Expand Up @@ -72,6 +71,7 @@
_valkey_lock_key = "oauth_login_lock"
_valkey_headers_key = "oauth_headers"


async def oauth_after_request(api_res: httpx.Response) -> None:
"""
Event hook to:
Expand All @@ -82,7 +82,9 @@ async def oauth_after_request(api_res: httpx.Response) -> None:
raise RateLimitedError()

# Handle rate-limiting
logger.debug(f"x-ratelimit-remaining: {api_res.headers.get("x-ratelimit-remaining")} x-ratelimit-reset: {api_res.headers.get("x-ratelimit-reset")} x-ratelimit-used: {api_res.headers.get("x-ratelimit-used")}")
logger.debug(
f"x-ratelimit-remaining: {api_res.headers.get("x-ratelimit-remaining")} x-ratelimit-reset: {api_res.headers.get("x-ratelimit-reset")} x-ratelimit-used: {api_res.headers.get("x-ratelimit-used")}"
)
rate_limit_remaining = api_res.headers.get("x-ratelimit-remaining")
if rate_limit_remaining:
rate_limit_remaining_value = float(rate_limit_remaining)
Expand All @@ -99,9 +101,7 @@ async def oauth_before_request(
Event hook to add the official Android app headers to the request
"""
# Add the Android official app headers
request.headers.update(
_get_login_headers_from_cache()
)
request.headers.update(_get_login_headers_from_cache())

# For multi subreddits, the user-agent doesn't work so tweak it
if "+" in request.url.path:
Expand All @@ -110,21 +110,22 @@ async def oauth_before_request(
"Android", "Andr\u200boid"
)


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):

# Check if we have login headers in cache
if not valkey_client.exists(_valkey_headers_key):
oauth_login()

headers_str: bytes = valkey_client.get(_valkey_headers_key) # type: ignore
headers_str: bytes = valkey_client.get(_valkey_headers_key) # type: ignore
decoded_headers = json.loads(headers_str.decode())
return decoded_headers


def oauth_login() -> None:
"""
Perform OAuth login to get headers matching the official Android app.
Expand All @@ -142,7 +143,9 @@ def oauth_login() -> None:
"X-Reddit-Device-Id": unique_uuid,
"User-Agent": f"Reddit/{android_app_version}/Android {android_version}",
}
logger.debug(f"Generated headers for official Android app login: {common_headers}")
logger.debug(
f"Generated headers for official Android app login: {common_headers}"
)

# Login
client = httpx.Client() # not async but not supported by cachier
Expand All @@ -157,7 +160,7 @@ def oauth_login() -> None:
)

if res.is_success:
oauth_headers = {
oauth_headers = {
**common_headers,
"Authorization": f"Bearer {res.json()['access_token']}",
"x-reddit-loid": res.headers["x-reddit-loid"],
Expand All @@ -166,7 +169,7 @@ def oauth_login() -> None:
valkey_client.set(
_valkey_headers_key,
json.dumps(oauth_headers),
ex=82800 # 23 hours
ex=82800, # 23 hours
)
else:
logger.debug(
Expand Down

0 comments on commit 397bc6c

Please sign in to comment.